Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
// ============================================================================
// Copyright (c) 2017-2021 Nawapunth Manusitthipol (NawaMan - http://nawaman.net).
// ----------------------------------------------------------------------------
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// ============================================================================
package functionalj.list.doublelist;
import static functionalj.lens.Access.theDouble;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.OptionalDouble;
import java.util.Random;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.DoubleBinaryOperator;
import java.util.function.DoubleConsumer;
import java.util.function.DoubleFunction;
import java.util.function.DoublePredicate;
import java.util.function.DoubleSupplier;
import java.util.function.DoubleToIntFunction;
import java.util.function.DoubleToLongFunction;
import java.util.function.DoubleUnaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;
import functionalj.function.DoubleComparator;
import functionalj.function.Func;
import functionalj.function.aggregator.DoubleAggregation;
import functionalj.function.aggregator.DoubleAggregationToBoolean;
import functionalj.function.aggregator.DoubleAggregationToDouble;
import functionalj.function.aggregator.DoubleAggregationToInt;
import functionalj.function.aggregator.DoubleAggregationToLong;
import functionalj.list.AsFuncList;
import functionalj.list.FuncList;
import functionalj.list.FuncList.Mode;
import functionalj.list.ImmutableFuncList;
import functionalj.list.intlist.AsIntFuncList;
import functionalj.list.intlist.IntFuncList;
import functionalj.list.longlist.AsLongFuncList;
import functionalj.list.longlist.LongFuncList;
import functionalj.result.NoMoreResultException;
import functionalj.result.Result;
import functionalj.stream.StreamPlus;
import functionalj.stream.SupplierBackedIterator;
import functionalj.stream.doublestream.DoubleIterable;
import functionalj.stream.doublestream.DoubleIteratorPlus;
import functionalj.stream.doublestream.DoubleStreamPlus;
import functionalj.stream.doublestream.DoubleStreamPlusHelper;
import functionalj.stream.doublestream.GrowOnlyDoubleArray;
import functionalj.stream.intstream.IntStreamPlus;
import functionalj.stream.longstream.LongStreamPlus;
import functionalj.stream.markers.Eager;
import functionalj.stream.markers.Sequential;
import functionalj.stream.markers.Terminal;
import functionalj.tuple.DoubleDoubleTuple;
import functionalj.tuple.IntDoubleTuple;
import lombok.val;
import nullablej.nullable.Nullable;
public interface DoubleFuncList
extends
AsDoubleFuncList,
DoubleIterable,
DoublePredicate,
DoubleFuncListWithCombine,
DoubleFuncListWithFilter,
DoubleFuncListWithFlatMap,
DoubleFuncListWithLimit,
DoubleFuncListWithMap,
DoubleFuncListWithMapFirst,
DoubleFuncListWithMapGroup,
DoubleFuncListWithMapMulti,
DoubleFuncListWithMapThen,
DoubleFuncListWithMapToMap,
DoubleFuncListWithMapToTuple,
DoubleFuncListWithMapWithIndex,
DoubleFuncListWithModify,
DoubleFuncListWithPeek,
DoubleFuncListWithPipe,
DoubleFuncListWithSegment,
DoubleFuncListWithSort,
DoubleFuncListWithSplit {
/** Throw a no more element exception. This is used for generator. */
public static double noMoreElement() throws NoMoreResultException {
SupplierBackedIterator.noMoreElement();
return Double.NaN;
}
/** Returns an empty IntFuncList. */
public static ImmutableDoubleFuncList empty() {
return ImmutableDoubleFuncList.empty();
}
/** Returns an empty DoubleFuncList. */
public static ImmutableDoubleFuncList emptyList() {
return ImmutableDoubleFuncList.empty();
}
/** Returns an empty DoubleFuncList. */
public static ImmutableDoubleFuncList emptyDoubleList() {
return ImmutableDoubleFuncList.empty();
}
/** Create a FuncList from the given doubles. */
public static ImmutableDoubleFuncList of(double ... data) {
return ImmutableDoubleFuncList.of(data);
}
/** Create a FuncList from the given doubles. */
public static ImmutableDoubleFuncList AllOf(double ... data) {
return ImmutableDoubleFuncList.of(data);
}
/** Create a FuncList from the given doubles. */
public static ImmutableDoubleFuncList doubles(double ... data) {
return ImmutableDoubleFuncList.of(data);
}
/** Create a FuncList from the given doubles. */
public static ImmutableDoubleFuncList doubleList(double ... data) {
return ImmutableDoubleFuncList.of(data);
}
/** Create a FuncList from the given doubles. */
@SafeVarargs
public static ImmutableDoubleFuncList ListOf(double ... data) {
return ImmutableDoubleFuncList.of(data);
}
/** Create a FuncList from the given doubles. */
@SafeVarargs
public static ImmutableDoubleFuncList listOf(double ... data) {
return ImmutableDoubleFuncList.of(data);
}
//-- From --
/** Create a FuncList from the given doubles. */
public static ImmutableDoubleFuncList from(double[] datas) {
return new ImmutableDoubleFuncList(datas, datas.length);
}
/** Create a FuncList from the given collection. */
public static ImmutableDoubleFuncList from(Collection data, double valueForNull) {
DoubleStream doubleStream
= StreamPlus.from(data.stream())
.fillNull ((Double)valueForNull)
.mapToDouble(theDouble);
Mode mode = (data instanceof FuncList) ? ((FuncList)data).mode() : Mode.lazy;
return ImmutableDoubleFuncList.from(mode, doubleStream);
}
/** Create a FuncList from the given FuncList. */
public static DoubleFuncList from(Mode mode, AsDoubleFuncList asFuncList) {
val funcList = asFuncList.asDoubleFuncList();
return funcList.toMode(mode);
}
/** Create a FuncList from the given stream. */
public static DoubleFuncList from(DoubleStream stream) {
return new StreamBackedDoubleFuncList(stream);
}
/**
* Create a FuncList from the given supplier of stream.
*
* The provided stream should produce the same sequence of values.
**/
public static DoubleFuncList from(Supplier supplier) {
return new DoubleFuncListDerived(() -> DoubleStreamPlus.from(supplier.get()));
}
// == Create ==
/** Returns the infinite streams of zeroes. */
public static DoubleFuncList zeroes() {
return zeroes(Integer.MAX_VALUE);
}
/** Returns a list that contains zeroes. */
public static DoubleFuncList zeroes(int count) {
return DoubleFuncList.from(() -> DoubleStreamPlus.zeroes(count));
}
/** Returns the list of ones. */
public static DoubleFuncList ones() {
return ones(Integer.MAX_VALUE);
}
/** Returns a list that contains ones. */
public static DoubleFuncList ones(int count) {
return DoubleFuncList.from(() -> DoubleStreamPlus.ones(count));
}
/** Create a list that is the repeat of the given array of data. */
public static DoubleFuncList repeat(double ... data) {
return DoubleFuncList.from(() -> DoubleStreamPlus.repeat(data));
}
/** Create a list that is the repeat of the given array of data. */
public static DoubleFuncList cycle(double ... data) {
return DoubleFuncList.from(() -> DoubleStreamPlus.cycle(data));
}
/** Create a list that is the repeat of the given array of data. */
public static DoubleFuncList repeat(DoubleFuncList data) {
return DoubleFuncList.from(() -> DoubleStreamPlus.repeat(data));
}
/** Create a list that is the repeat of the given array of data. */
public static DoubleFuncList cycle(DoubleFuncList data) {
return DoubleFuncList.from(() -> DoubleStreamPlus.cycle(data));
}
/** Create a list that for a loop with the number of time given - the value is the index of the loop. */
public static DoubleFuncList loop() {
return DoubleFuncList.from(() -> DoubleStreamPlus.loop());
}
/** Create a list that for a loop with the number of time given - the value is the index of the loop. */
public static DoubleFuncList loop(int times) {
return DoubleFuncList.from(() -> DoubleStreamPlus.loop(times));
}
public static DoubleFuncList loopBy(double step) {
return DoubleFuncList.from(() -> DoubleStreamPlus.loopBy(step));
}
public static DoubleFuncList loopBy(double step, int times) {
return DoubleFuncList.from(() -> DoubleStreamPlus.loopBy(step, times));
}
public static DoubleFuncList infinite() {
return DoubleFuncList.from(() -> DoubleStreamPlus.infinite());
}
public static DoubleFuncList naturalNumbers() {
return DoubleFuncList.from(() -> DoubleStreamPlus.naturalNumbers());
}
public static DoubleFuncList naturalNumbers(int count) {
return DoubleFuncList.from(() -> DoubleStreamPlus.naturalNumbers(count));
}
/** Returns the infinite streams of wholes numbers -- 0, 1, 2, 3, .... */
public static DoubleFuncList wholeNumbers() {
return DoubleFuncList.from(() -> DoubleStreamPlus.wholeNumbers());
}
/** Create a FuncList that for a loop with the number of time given - the value is the index of the loop. */
public static DoubleFuncList wholeNumbers(int count) {
return DoubleFuncList.from(() -> DoubleStreamPlus.wholeNumbers(count));
}
/** Create a FuncList that for a loop with the number of time given - the value is the index of the loop. */
public static DoubleFuncList range(double startInclusive, double endExclusive) {
return DoubleFuncList.from(() -> DoubleStreamPlus.range(startInclusive, endExclusive));
}
/** Create a StreamPlus that for a loop from the start value inclusively by the given step. */
public static DoubleFuncList stepFrom(double startInclusive, double step) {
return DoubleFuncList.from(() -> DoubleStreamPlus.stepFrom(startInclusive, step));
}
//-- Concat + Combine --
/** Concatenate all the given streams. */
public static DoubleFuncList concat(DoubleFuncList ... lists) {
return combine(lists);
}
/**
* Concatenate all the given lists.
*
* This method is the alias of {@link FuncList#concat(FuncList...)}
* but allowing static import without colliding with {@link String#concat(String)}.
**/
public static DoubleFuncList combine(DoubleFuncList ... lists) {
ImmutableFuncList listOfList = FuncList.listOf(lists);
return listOfList.flatMapToDouble(Func.itself());
}
// TODO - Rethink ... as this will generate un-repeatable stream.
// we may want to do cache here.
/**
* Create a FuncList from the supplier of suppliers.
* The supplier will be repeatedly asked for value until NoMoreResultException is thrown.
**/
public static DoubleFuncList generate(Supplier suppliers) {
return DoubleFuncList.from(() -> {
val generator = suppliers.get();
return DoubleStreamPlus.generate(generator);
});
}
/**
* Create a list from the supplier of suppliers.
* The supplier will be repeatedly asked for value until NoMoreResultException is thrown.
**/
public static DoubleFuncList generateWith(Supplier suppliers) {
return generate(suppliers);
}
/**
* Create a list by apply the compounder to the seed over and over.
*
* For example: let say seed = 1 and f(x) = x*2.
* The result stream will be:
* 1 <- seed,
* 2 <- (1*2),
* 4 <- ((1*2)*2),
* 8 <- (((1*2)*2)*2),
* 16 <- ((((1*2)*2)*2)*2)
* ...
*
* Note: this is an alias of compound()
**/
public static DoubleFuncList iterate(
double seed,
DoubleUnaryOperator compounder) {
return DoubleFuncList.from(() -> DoubleStreamPlus.iterate(seed, compounder));
}
public static DoubleFuncList iterate(
double seed,
DoubleAggregationToDouble aggregation) {
return DoubleFuncList.from(() -> DoubleStreamPlus.iterate(seed, aggregation));
}
/**
* Create a list by apply the compounder to the seed over and over.
*
* For example: let say seed = 1 and f(x) = x*2.
* The result stream will be:
* 1 <- seed,
* 2 <- (1*2),
* 4 <- ((1*2)*2),
* 8 <- (((1*2)*2)*2),
* 16 <- ((((1*2)*2)*2)*2)
* ...
*
* Note: this is an alias of iterate()
**/
public static DoubleFuncList compound(
double seed,
DoubleUnaryOperator compounder) {
return DoubleFuncList.from(() -> DoubleStreamPlus.compound(seed, compounder));
}
public static DoubleFuncList compound(
double seed,
DoubleAggregationToDouble aggregation) {
return DoubleFuncList.from(() -> DoubleStreamPlus.compound(seed, aggregation));
}
/**
* Create a list by apply the compounder to the seeds over and over.
*
* For example: let say seed1 = 1, seed2 = 1 and f(a,b) = a+b.
* The result stream will be:
* 1 <- seed1,
* 1 <- seed2,
* 2 <- (1+1),
* 3 <- (1+2),
* 5 <- (2+3),
* 8 <- (5+8)
* ...
*
* Note: this is an alias of compound()
**/
public static DoubleFuncList iterate(
double seed1,
double seed2,
DoubleBinaryOperator compounder) {
return DoubleFuncList.from(() -> DoubleStreamPlus.iterate(seed1, seed2, compounder));
}
/**
* Create a list by apply the compounder to the seeds over and over.
*
* For example: let say seed1 = 1, seed2 = 1 and f(a,b) = a+b.
* The result stream will be:
* 1 <- seed1,
* 1 <- seed2,
* 2 <- (1+1),
* 3 <- (1+2),
* 5 <- (2+3),
* 8 <- (5+8)
* ...
*
* Note: this is an alias of iterate()
**/
public static DoubleFuncList compound(
double seed1,
double seed2,
DoubleBinaryOperator compounder) {
return iterate(seed1, seed2, compounder);
}
// == Zip ==
/**
* Create a FuncList by combining elements together into a FuncList of tuples.
* Only elements with pair will be combined. If this is not desirable, use FuncList1.zip(FuncList2).
*
* For example:
* list1 = [A, B, C, D, E]
* list2 = [1, 2, 3, 4]
*
* The result stream = [(A,1), (B,2), (C,3), (D,4)].
**/
public static FuncList zipOf(
AsDoubleFuncList list1,
AsDoubleFuncList list2) {
return FuncList.from(() -> {
return DoubleStreamPlus.zipOf(
list1.doubleStream(),
list2.doubleStream());
});
}
/**
* Create a FuncList by combining elements together using the merger function and collected into the result stream.
* Only elements with pair will be combined. If this is not desirable, use FuncList1.zip(FuncList2).
*
* For example:
* list1 = [A, B, C, D, E]
* list2 = [1, 2, 3, 4]
* merger = a + "+" + b
*
* The result stream = ["A+1", "B+2", "C+3", "D+4"].
**/
public static FuncList zipOf(
AsDoubleFuncList list1, double defaultValue1,
AsDoubleFuncList list2, double defaultValue2) {
return FuncList.from(() -> {
return DoubleStreamPlus.zipOf(
list1.doubleStream(), defaultValue1,
list2.doubleStream(), defaultValue2);
});
}
/**
* Zip integers from two IntFuncLists and combine it into another object.
* The result stream has the size of the shortest FuncList.
*/
public static DoubleFuncList zipOf(
AsDoubleFuncList list1,
AsDoubleFuncList list2,
DoubleBinaryOperator merger) {
return DoubleFuncList.from(() -> {
return DoubleStreamPlus.zipOf(
list1.doubleStream(),
list2.doubleStream(),
merger);
});
}
/**
* Zip integers from an int stream and another object stream and combine it into another object.
* The result stream has the size of the shortest stream.
*/
public static DoubleFuncList zipOf(
AsDoubleFuncList list1, double defaultValue1,
AsDoubleFuncList list2, double defaultValue2,
DoubleBinaryOperator merger) {
return DoubleFuncList.from(() -> {
return DoubleStreamPlus.zipOf(
list1.doubleStream(), defaultValue1,
list2.doubleStream(), defaultValue2,
merger);
});
}
// -- Builder --
/** Create a new FuncList. */
public static DoubleFuncListBuilder newListBuilder() {
return new DoubleFuncListBuilder();
}
/** Create a new list builder. */
public static DoubleFuncListBuilder newBuilder() {
return new DoubleFuncListBuilder();
}
/** Create a new FuncList. */
public static DoubleFuncListBuilder newDoubleListBuilder() {
return new DoubleFuncListBuilder();
}
// == Core ==
/** Return the stream of data behind this IntFuncList. */
public DoubleStreamPlus doubleStream();
/** Return the stream of data behind this IntFuncList. */
public default DoubleStreamPlus doubleStreamPlus() {
return doubleStream();
}
/** Return the this as a FuncList. */
@Override
public default DoubleFuncList asDoubleFuncList() {
return this;
}
//-- Derive --
/** Create a FuncList from the given FuncList. */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static