T
- component type of this Treepublic abstract class Tree<T> extends java.lang.Object implements Traversable<T>, java.io.Serializable
Modifier and Type | Class and Description |
---|---|
static class |
Tree.Empty<T>
Deprecated.
will be removed from the public API
|
static class |
Tree.Node<T>
Deprecated.
will be removed from the public API
|
static class |
Tree.Order
Tree traversal order.
|
Modifier and Type | Method and Description |
---|---|
int |
branchCount()
Counts the number of branches of this tree.
|
static <T,ID> List<Tree.Node<T>> |
build(java.lang.Iterable<? extends T> source,
java.util.function.Function<? super T,? extends ID> idMapper,
java.util.function.Function<? super T,? extends ID> parentMapper)
Build a
List with roots of Tree from flat source. |
<R> Tree<R> |
collect(PartialFunction<? super T,? extends R> partialFunction)
Collects all elements that are in the domain of the given
partialFunction by mapping the elements to type R . |
static <T> java.util.stream.Collector<T,java.util.ArrayList<T>,Tree<T>> |
collector()
Returns a
Collector which may be used in conjunction with
Stream.collect(java.util.stream.Collector) to obtain a Tree . |
Seq<T> |
distinct()
Returns a new version of this which contains no duplicates.
|
Seq<T> |
distinctBy(java.util.Comparator<? super T> comparator)
Returns a new version of this which contains no duplicates.
|
<U> Seq<T> |
distinctBy(java.util.function.Function<? super T,? extends U> keyExtractor)
Returns a new version of this which contains no duplicates.
|
abstract java.lang.String |
draw()
Creates a neat 2-dimensional drawing of a tree.
|
Seq<T> |
drop(int n)
Drops the first n elements of this or all elements, if this length < n.
|
Seq<T> |
dropRight(int n)
Drops the last n elements of this or all elements, if this length < n.
|
Seq<T> |
dropUntil(java.util.function.Predicate<? super T> predicate)
Drops elements until the predicate holds for the current element.
|
Seq<T> |
dropWhile(java.util.function.Predicate<? super T> predicate)
Drops elements while the predicate holds for the current element.
|
static <T> Tree.Empty<T> |
empty()
Returns the singleton empty tree.
|
static <T> Tree<T> |
fill(int n,
java.util.function.Supplier<? extends T> s)
Returns a Tree containing
n values supplied by a given Supplier s . |
static <T> Tree<T> |
fill(int n,
T element)
Returns a Tree containing
n times the given element |
Seq<T> |
filter(java.util.function.Predicate<? super T> predicate)
Returns a new traversable consisting of all elements which satisfy the given predicate.
|
Seq<T> |
filterNot(java.util.function.Predicate<? super T> predicate)
Returns a new traversable consisting of all elements which do not satisfy the given predicate.
|
<U> Tree<U> |
flatMap(java.util.function.Function<? super T,? extends java.lang.Iterable<? extends U>> mapper)
FlatMaps this Traversable.
|
<U> U |
foldRight(U zero,
java.util.function.BiFunction<? super T,? super U,? extends U> f)
Folds this elements from the right, starting with
zero and successively calling combine . |
abstract List<Tree.Node<T>> |
getChildren()
Returns the children of this tree.
|
abstract T |
getValue()
Gets the value of this tree.
|
<C> Map<C,Seq<T>> |
groupBy(java.util.function.Function<? super T,? extends C> classifier)
Groups this elements by classifying the elements.
|
Iterator<Seq<T>> |
grouped(int size)
Groups this
Traversable into fixed size blocks. |
boolean |
hasDefiniteSize()
Checks if this Traversable is known to have a finite size.
|
T |
head()
Returns the first element of a non-empty Traversable.
|
Seq<T> |
init()
Dual of Traversable.tail(), returning all elements except the last.
|
Option<Seq<T>> |
initOption()
Dual of Traversable.tailOption(), returning all elements except the last as
Option . |
boolean |
isAsync()
A
Tree is computed synchronously. |
boolean |
isBranch()
Checks if this Tree is a branch.
|
boolean |
isDistinct()
Checks if this Traversable may consist of distinct elements only.
|
boolean |
isLazy()
A
Tree is computed eagerly. |
abstract boolean |
isLeaf()
Checks if this Tree is a leaf.
|
boolean |
isSequential()
Checks if the elements of this Traversable appear in encounter order.
|
boolean |
isTraversableAgain()
Checks if this Traversable can be repeatedly traversed.
|
Iterator<T> |
iterator()
An iterator by means of head() and tail().
|
Iterator<T> |
iterator(Tree.Order order)
Traverses this tree values in a specific
Tree.Order . |
int |
leafCount()
Counts the number of leaves of this tree.
|
<U> Tree<U> |
map(java.util.function.Function<? super T,? extends U> mapper)
Maps the elements of this
Traversable to elements of a new type preserving their order, if any. |
static <T> Tree<T> |
narrow(Tree<? extends T> tree)
Narrows a widened
Tree<? extends T> to Tree<T>
by performing a type-safe cast. |
int |
nodeCount()
Counts the number of nodes (i.e.
|
static <T> Tree<T> |
of(T... values)
Creates a Tree of the given elements.
|
static <T> Tree.Node<T> |
of(T value)
Returns a new Node containing the given value and having no children.
|
static <T> Tree.Node<T> |
of(T value,
java.lang.Iterable<Tree.Node<T>> children)
Returns a new Node containing the given value and having the given children.
|
static <T> Tree.Node<T> |
of(T value,
Tree.Node<T>... children)
Returns a new Node containing the given value and having the given children.
|
static <T> Tree<T> |
ofAll(java.lang.Iterable<? extends T> iterable)
Creates a Tree of the given elements.
|
static <T> Tree<T> |
ofAll(java.util.stream.Stream<? extends T> javaStream)
Creates a Tree that contains the elements of the given
Stream . |
Tree<T> |
orElse(java.lang.Iterable<? extends T> other)
Returns this
Traversable if it is nonempty, otherwise return the alternative. |
Tree<T> |
orElse(java.util.function.Supplier<? extends java.lang.Iterable<? extends T>> supplier)
Returns this
Traversable if it is nonempty, otherwise return the result of evaluating supplier. |
Tuple2<Seq<T>,Seq<T>> |
partition(java.util.function.Predicate<? super T> predicate)
Creates a partition of this
Traversable by splitting this elements in two in distinct traversables
according to a predicate. |
Tree<T> |
peek(java.util.function.Consumer<? super T> action)
Performs the given
action on the first element if this is an eager implementation. |
static <T> Tree.Node<T> |
recurse(T seed,
java.util.function.Function<? super T,? extends java.lang.Iterable<? extends T>> descend)
Recursively builds a non-empty
Tree , starting with the given seed value and proceeding in depth-first order. |
Tree<T> |
replace(T currentElement,
T newElement)
Replaces the first occurrence (if exists) of the given currentElement with newElement.
|
Tree<T> |
replaceAll(T currentElement,
T newElement)
Replaces all occurrences of the given currentElement with newElement.
|
Seq<T> |
retainAll(java.lang.Iterable<? extends T> elements)
Keeps all occurrences of the given elements from this.
|
Seq<T> |
scan(T zero,
java.util.function.BiFunction<? super T,? super T,? extends T> operation)
Computes a prefix scan of the elements of the collection.
|
<U> Seq<U> |
scanLeft(U zero,
java.util.function.BiFunction<? super U,? super T,? extends U> operation)
Produces a collection containing cumulative results of applying the
operator going left to right.
|
<U> Seq<U> |
scanRight(U zero,
java.util.function.BiFunction<? super T,? super U,? extends U> operation)
Produces a collection containing cumulative results of applying the
operator going right to left.
|
Iterator<Seq<T>> |
slideBy(java.util.function.Function<? super T,?> classifier)
Slides a non-overlapping window of a variable size over this
Traversable . |
Iterator<Seq<T>> |
sliding(int size)
Slides a window of a specific
size and step size 1 over this Traversable by calling
Traversable.sliding(int, int) . |
Iterator<Seq<T>> |
sliding(int size,
int step)
Slides a window of a specific
size and step size over this Traversable . |
Tuple2<Seq<T>,Seq<T>> |
span(java.util.function.Predicate<? super T> predicate)
Returns a tuple where the first element is the longest prefix of elements that satisfy the given
predicate and the second element is the remainder. |
java.lang.String |
stringPrefix()
Returns the name of this Value type, which is used by toString().
|
static <T> Tree<T> |
tabulate(int n,
java.util.function.Function<? super java.lang.Integer,? extends T> f)
Returns a Tree containing
n values of a given Function f
over a range of integer values from 0 to n - 1 . |
Seq<T> |
tail()
Drops the first element of a non-empty Traversable.
|
Option<Seq<T>> |
tailOption()
Drops the first element of a non-empty Traversable and returns an
Option . |
Seq<T> |
take(int n)
Takes the first n elements of this or all elements, if this length < n.
|
Seq<T> |
takeRight(int n)
Takes the last n elements of this or all elements, if this length < n.
|
Seq<T> |
takeUntil(java.util.function.Predicate<? super T> predicate)
Takes elements until the predicate holds for the current element.
|
Seq<T> |
takeWhile(java.util.function.Predicate<? super T> predicate)
Takes elements while the predicate holds for the current element.
|
abstract java.lang.String |
toLispString()
Creates a Lisp-like representation of this
Tree . |
<U> U |
transform(java.util.function.Function<? super Tree<T>,? extends U> f)
Transforms this
Tree . |
Seq<Tree.Node<T>> |
traverse()
Traverses this tree in
Tree.Order.PRE_ORDER . |
Seq<Tree.Node<T>> |
traverse(Tree.Order order)
Traverses this tree in a specific order.
|
Seq<T> |
values()
Traverses this tree values in
Tree.Order.PRE_ORDER . |
Seq<T> |
values(Tree.Order order)
Traverses this tree values in a specific order.
|
<U> Tree<Tuple2<T,U>> |
zip(java.lang.Iterable<? extends U> that)
Returns a traversable formed from this traversable and another Iterable collection by combining
corresponding elements in pairs.
|
<U> Tree<Tuple2<T,U>> |
zipAll(java.lang.Iterable<? extends U> that,
T thisElem,
U thatElem)
Returns a traversable formed from this traversable and another Iterable by combining corresponding elements in
pairs.
|
<U,R> Tree<R> |
zipWith(java.lang.Iterable<? extends U> that,
java.util.function.BiFunction<? super T,? super U,? extends R> mapper)
Returns a traversable formed from this traversable and another Iterable collection by mapping elements.
|
Tree<Tuple2<T,java.lang.Integer>> |
zipWithIndex()
Zips this traversable with its indices.
|
<U> Tree<U> |
zipWithIndex(java.util.function.BiFunction<? super T,? super java.lang.Integer,? extends U> mapper)
Returns a traversable formed from this traversable and another Iterable collection by mapping elements.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
arrangeBy, average, containsAll, count, equals, existsUnique, find, findLast, fold, foldLeft, forEachWithIndex, get, hashCode, headOption, isEmpty, isOrdered, isSingleValued, last, lastOption, length, max, maxBy, maxBy, min, minBy, minBy, mkCharSeq, mkCharSeq, mkCharSeq, mkString, mkString, mkString, narrow, nonEmpty, product, reduce, reduceLeft, reduceLeftOption, reduceOption, reduceRight, reduceRightOption, single, singleOption, size, spliterator, sum, toLinkedMap, toMap, toSortedMap, toSortedMap, unzip, unzip3
collect, collect, contains, corresponds, eq, exists, forAll, forEach, getOrElse, getOrElse, getOrElseThrow, getOrElseTry, getOrNull, narrow, out, out, stderr, stdout, toArray, toCharSeq, toCompletableFuture, toEither, toEither, toInvalid, toInvalid, toJavaArray, toJavaArray, toJavaArray, toJavaCollection, toJavaList, toJavaList, toJavaMap, toJavaMap, toJavaMap, toJavaOptional, toJavaParallelStream, toJavaSet, toJavaSet, toJavaStream, toLeft, toLeft, toLinkedMap, toLinkedMap, toLinkedSet, toList, toMap, toMap, toOption, toPriorityQueue, toPriorityQueue, toQueue, toRight, toRight, toSet, toSortedMap, toSortedMap, toSortedMap, toSortedMap, toSortedSet, toSortedSet, toStream, toString, toTree, toTree, toTry, toTry, toValid, toValid, toValidation, toValidation, toVector
public static <T> java.util.stream.Collector<T,java.util.ArrayList<T>,Tree<T>> collector()
Collector
which may be used in conjunction with
Stream.collect(java.util.stream.Collector)
to obtain a Tree
.T
- Component type of the Tree.public static <T> Tree.Empty<T> empty()
T
- Type of tree values.public static <T> Tree<T> narrow(Tree<? extends T> tree)
Tree<? extends T>
to Tree<T>
by performing a type-safe cast. This is eligible because immutable/read-only
collections are covariant.T
- Component type of the Tree
.tree
- An Tree
.tree
instance as narrowed type Tree<T>
.public static <T> Tree.Node<T> of(T value)
T
- Value typevalue
- A value@SafeVarargs public static <T> Tree.Node<T> of(T value, Tree.Node<T>... children)
T
- Value typevalue
- A valuechildren
- The child nodes, possibly emptypublic static <T> Tree.Node<T> of(T value, java.lang.Iterable<Tree.Node<T>> children)
T
- Value typevalue
- A valuechildren
- The child nodes, possibly empty@SafeVarargs public static <T> Tree<T> of(T... values)
T
- Component type of the List.values
- Zero or more values.java.lang.NullPointerException
- if values
is nullpublic static <T> Tree<T> ofAll(java.lang.Iterable<? extends T> iterable)
If the given iterable is a tree, it is returned as result. if the iteration order of the elements is stable.
T
- Component type of the List.iterable
- An Iterable of elements.java.lang.NullPointerException
- if elements
is nullpublic static <T> Tree<T> ofAll(java.util.stream.Stream<? extends T> javaStream)
Stream
.T
- Component type of the Stream.javaStream
- A Stream
public static <T> Tree<T> tabulate(int n, java.util.function.Function<? super java.lang.Integer,? extends T> f)
n
values of a given Function f
over a range of integer values from 0 to n - 1
.T
- Component type of the Treen
- The number of elements in the Treef
- The Function computing element valuesf(0),f(1), ..., f(n - 1)
java.lang.NullPointerException
- if f
is nullpublic static <T> Tree<T> fill(int n, java.util.function.Supplier<? extends T> s)
n
values supplied by a given Supplier s
.T
- Component type of the Treen
- The number of elements in the Trees
- The Supplier computing element valuesn
, where each element contains the result supplied by s
.java.lang.NullPointerException
- if s
is nullpublic static <T> Tree<T> fill(int n, T element)
n
times the given element
T
- Component type of the Treen
- The number of elements in the Treeelement
- The elementn
, where each element is the given element
.public static <T> Tree.Node<T> recurse(T seed, java.util.function.Function<? super T,? extends java.lang.Iterable<? extends T>> descend)
Tree
, starting with the given seed
value and proceeding in depth-first order.
The children of a node are created by
descend
function to the node valueExample:
// = (1 (2 4 5) 3)
Tree.recurse(1, i ->
(i == 1) ? List.of(2, 3) :
(i == 2) ? List.(4, 5) :
List.empty()
).toLispString();
T
- Value typeseed
- The start value for the Treedescend
- A function to calculate the child valuesTree
instancejava.lang.NullPointerException
- if descend
is nullpublic static <T,ID> List<Tree.Node<T>> build(java.lang.Iterable<? extends T> source, java.util.function.Function<? super T,? extends ID> idMapper, java.util.function.Function<? super T,? extends ID> parentMapper)
List
with roots of Tree
from flat source.
parentMapper
must return null
for root element.
// = [(1, null, "I"), (2, 1, "II"), (3, 1, "III"), (4, 2, "IV"), (5, 2, "V")]
List<MenuItem> items = ...; // MenuItem(id, parentId, label)
// I
// / \
// II III
// /\
// IV V
Tree<MenuItem> menu = Tree.build(items, MenuItem::getId, MenuItem::getParentId);
T
- Value typeID
- Id typesource
- Flat sourceidMapper
- A mapper from source item to unique identifier of that itemparentMapper
- A mapper from source item to unique identifier of parent item. Need return null for root itemsList
instance with non-empty Tree
instancesjava.lang.NullPointerException
- if source
, idMapper
or parentMapper
is nullpublic final <R> Tree<R> collect(PartialFunction<? super T,? extends R> partialFunction)
Traversable
partialFunction
by mapping the elements to type R
.
More specifically, for each of this elements in iteration order first it is checked
partialFunction.isDefinedAt(element)
If the elements makes it through that filter, the mapped instance is added to the result collection
R newElement = partialFunction.apply(element)
Note:If this Traversable
is ordered (i.e. extends Ordered
,
the caller of collect
has to ensure that the elements are comparable (i.e. extend Comparable
).collect
in interface Traversable<T>
R
- The new element typepartialFunction
- A function that is not necessarily defined of all elements of this traversable.Traversable
instance containing elements of type R
public abstract T getValue()
java.lang.UnsupportedOperationException
- if this tree is emptypublic abstract List<Tree.Node<T>> getChildren()
public abstract boolean isLeaf()
public final boolean isBranch()
public final boolean isAsync()
Tree
is computed synchronously.public final boolean isDistinct()
Traversable
isDistinct
in interface Traversable<T>
public final boolean isLazy()
Tree
is computed eagerly.public final boolean isSequential()
Traversable
isSequential
in interface Traversable<T>
public final Iterator<T> iterator(Tree.Order order)
Tree.Order
.order
- A traversal orderpublic abstract java.lang.String toLispString()
Tree
.Tree
as Lisp-string, i.e. represented as list of lists.public final <U> U transform(java.util.function.Function<? super Tree<T>,? extends U> f)
Tree
.U
- Type of transformation resultf
- A transformationU
java.lang.NullPointerException
- if f
is nullpublic final Seq<Tree.Node<T>> traverse()
Tree.Order.PRE_ORDER
.public final Seq<Tree.Node<T>> traverse(Tree.Order order)
order
- the tree traversal orderjava.lang.NullPointerException
- if order is nullpublic final Seq<T> values()
Tree.Order.PRE_ORDER
.
Syntactic sugar for traverse().map(Node::getValue)
.public final Seq<T> values(Tree.Order order)
traverse(order).map(Node::getValue)
.order
- the tree traversal orderjava.lang.NullPointerException
- if order is nullpublic final int branchCount()
public final int leafCount()
public final int nodeCount()
public final Seq<T> distinct()
Traversable
equals
.distinct
in interface Traversable<T>
Traversable
containing this elements without duplicatespublic final Seq<T> distinctBy(java.util.Comparator<? super T> comparator)
Traversable
comparator
.distinctBy
in interface Traversable<T>
comparator
- A comparatorTraversable
containing this elements without duplicatespublic final <U> Seq<T> distinctBy(java.util.function.Function<? super T,? extends U> keyExtractor)
Traversable
equals
.
The elements of the result are determined in the order of their occurrence - first match wins.
distinctBy
in interface Traversable<T>
U
- key typekeyExtractor
- A key extractorTraversable
containing this elements without duplicatespublic final Seq<T> drop(int n)
Traversable
drop
in interface Traversable<T>
n
- The number of elements to drop.public final Seq<T> dropRight(int n)
Traversable
dropRight
in interface Traversable<T>
n
- The number of elements to drop.public final Seq<T> dropUntil(java.util.function.Predicate<? super T> predicate)
Traversable
dropUntil
in interface Traversable<T>
predicate
- A condition tested subsequently for this elements.public final Seq<T> dropWhile(java.util.function.Predicate<? super T> predicate)
Traversable
Note: This is essentially the same as dropUntil(predicate.negate())
.
It is intended to be used with method references, which cannot be negated directly.
dropWhile
in interface Traversable<T>
predicate
- A condition tested subsequently for this elements.public final Seq<T> filter(java.util.function.Predicate<? super T> predicate)
Traversable
filter
in interface Traversable<T>
predicate
- A predicatepublic final Seq<T> filterNot(java.util.function.Predicate<? super T> predicate)
Traversable
The default implementation is equivalent to
filter(predicate.negate()
filterNot
in interface Traversable<T>
predicate
- A predicatepublic final <U> Tree<U> flatMap(java.util.function.Function<? super T,? extends java.lang.Iterable<? extends U>> mapper)
Traversable
flatMap
in interface Traversable<T>
U
- The resulting component type.mapper
- A mapperpublic final <U> U foldRight(U zero, java.util.function.BiFunction<? super T,? super U,? extends U> f)
Traversable
zero
and successively calling combine
.
Example:
// = "!cba"
List("a", "b", "c").foldRight("!", (x, xs) -> xs + x)
public final <C> Map<C,Seq<T>> groupBy(java.util.function.Function<? super T,? extends C> classifier)
Traversable
groupBy
in interface Traversable<T>
C
- classified class typeclassifier
- A function which classifies elements into classesTraversable.arrangeBy(Function)
public final Iterator<Seq<T>> grouped(int size)
Traversable
Traversable
into fixed size blocks.
Let length be the length of this Iterable. Then grouped is defined as follows:
this.isEmpty()
, the resulting Iterator
is empty.size <= length
, the resulting Iterator
will contain length / size
blocks of size
size
and maybe a non-empty block of size length % size
, if there are remaining elements.size > length
, the resulting Iterator
will contain one block of size length
.
[].grouped(1) = []
[].grouped(0) throws
[].grouped(-1) throws
[1,2,3,4].grouped(2) = [[1,2],[3,4]]
[1,2,3,4,5].grouped(2) = [[1,2],[3,4],[5]]
[1,2,3,4].grouped(5) = [[1,2,3,4]]
Please note that grouped(int)
is a special case of Traversable.sliding(int, int), i.e.
grouped(size)
is the same as sliding(size, size)
.grouped
in interface Traversable<T>
size
- a positive block sizepublic final boolean hasDefiniteSize()
Traversable
This method should be implemented by classes only, i.e. not by interfaces.
hasDefiniteSize
in interface Traversable<T>
public final T head()
Traversable
head
in interface Traversable<T>
public final Seq<T> init()
Traversable
init
in interface Traversable<T>
public final Option<Seq<T>> initOption()
Traversable
Option
.initOption
in interface Traversable<T>
Some(traversable)
or None
if this is empty.public final boolean isTraversableAgain()
Traversable
This method should be implemented by classes only, i.e. not by interfaces.
isTraversableAgain
in interface Traversable<T>
public final Iterator<T> iterator()
Traversable
public final <U> Tree<U> map(java.util.function.Function<? super T,? extends U> mapper)
Traversable
Traversable
to elements of a new type preserving their order, if any.public final Tree<T> orElse(java.lang.Iterable<? extends T> other)
Traversable
Traversable
if it is nonempty, otherwise return the alternative.orElse
in interface Traversable<T>
other
- An alternative Traversable
Traversable
if it is nonempty, otherwise return the alternative.public final Tree<T> orElse(java.util.function.Supplier<? extends java.lang.Iterable<? extends T>> supplier)
Traversable
Traversable
if it is nonempty, otherwise return the result of evaluating supplier.orElse
in interface Traversable<T>
supplier
- An alternative Traversable
supplierTraversable
if it is nonempty, otherwise return the result of evaluating supplier.public final Tuple2<Seq<T>,Seq<T>> partition(java.util.function.Predicate<? super T> predicate)
Traversable
Traversable
by splitting this elements in two in distinct traversables
according to a predicate.partition
in interface Traversable<T>
predicate
- A predicate which classifies an element if it is in the first or the second traversable.Traversable
contains all elements that satisfy the given predicate
, the second Traversable
contains all elements that don't. The original order of elements is preserved.public final Tree<T> peek(java.util.function.Consumer<? super T> action)
Value
action
on the first element if this is an eager implementation.
Performs the given action
on all elements (the first immediately, successive deferred),
if this is a lazy implementation.public final Tree<T> replace(T currentElement, T newElement)
Traversable
replace
in interface Traversable<T>
currentElement
- An element to be substituted.newElement
- A replacement for currentElement.public final Tree<T> replaceAll(T currentElement, T newElement)
Traversable
replaceAll
in interface Traversable<T>
currentElement
- An element to be substituted.newElement
- A replacement for currentElement.public final Seq<T> retainAll(java.lang.Iterable<? extends T> elements)
Traversable
retainAll
in interface Traversable<T>
elements
- Elements to be kept.public final Seq<T> scan(T zero, java.util.function.BiFunction<? super T,? super T,? extends T> operation)
Traversable
scan
in interface Traversable<T>
zero
- neutral element for the operator opoperation
- the associative operator for the scanpublic final <U> Seq<U> scanLeft(U zero, java.util.function.BiFunction<? super U,? super T,? extends U> operation)
Traversable
scanLeft
in interface Traversable<T>
U
- the type of the elements in the resulting collectionzero
- the initial valueoperation
- the binary operator applied to the intermediate result and the elementpublic final <U> Seq<U> scanRight(U zero, java.util.function.BiFunction<? super T,? super U,? extends U> operation)
Traversable
scanRight
in interface Traversable<T>
U
- the type of the elements in the resulting collectionzero
- the initial valueoperation
- the binary operator applied to the intermediate result and the elementpublic final Iterator<Seq<T>> slideBy(java.util.function.Function<? super T,?> classifier)
Traversable
Traversable
.
Each window contains elements with the same class, as determined by classifier
. Two consecutive
values in this Traversable
will be in the same window only if classifier
returns equal
values for them. Otherwise, the values will constitute the last element of the previous window and the
first element of the next window.
Examples:
[].slideBy(Function.identity()) = []
[1,2,3,4,4,5].slideBy(Function.identity()) = [[1],[2],[3],[4,4],[5]]
[1,2,3,10,12,5,7,20,29].slideBy(x -> x/10) = [[1,2,3],[10,12],[5,7],[20,29]]
slideBy
in interface Traversable<T>
classifier
- A function which classifies elements into classespublic final Iterator<Seq<T>> sliding(int size)
Traversable
size
and step size 1 over this Traversable
by calling
Traversable.sliding(int, int)
.sliding
in interface Traversable<T>
size
- a positive window sizepublic final Iterator<Seq<T>> sliding(int size, int step)
Traversable
size
and step
size over this Traversable
.
Examples:
[].sliding(1,1) = []
[1,2,3,4,5].sliding(2,3) = [[1,2],[4,5]]
[1,2,3,4,5].sliding(2,4) = [[1,2],[5]]
[1,2,3,4,5].sliding(2,5) = [[1,2]]
[1,2,3,4].sliding(5,3) = [[1,2,3,4]]
sliding
in interface Traversable<T>
size
- a positive window sizestep
- a positive step sizepublic final Tuple2<Seq<T>,Seq<T>> span(java.util.function.Predicate<? super T> predicate)
Traversable
predicate
and the second element is the remainder.span
in interface Traversable<T>
predicate
- A predicate.Tuple
containing the longest prefix of elements that satisfy p and the remainder.public final java.lang.String stringPrefix()
Value
stringPrefix
in interface Value<T>
public final Seq<T> tail()
Traversable
tail
in interface Traversable<T>
public final Option<Seq<T>> tailOption()
Traversable
Option
.tailOption
in interface Traversable<T>
Some(traversable)
or None
if this is empty.public final Seq<T> take(int n)
Traversable
The result is equivalent to sublist(0, max(0, min(length(), n)))
but does not throw if n < 0
or
n > length()
.
In the case of n < 0
the empty instance is returned, in the case of n > length()
this is returned.
take
in interface Traversable<T>
n
- The number of elements to take.public final Seq<T> takeRight(int n)
Traversable
The result is equivalent to sublist(max(0, min(length(), length() - n)), n)
, i.e. takeRight will not
throw if n < 0
or n > length()
.
In the case of n < 0
the empty instance is returned, in the case of n > length()
this is returned.
takeRight
in interface Traversable<T>
n
- The number of elements to take.public final Seq<T> takeUntil(java.util.function.Predicate<? super T> predicate)
Traversable
Note: This is essentially the same as takeWhile(predicate.negate())
. It is intended to be used with
method references, which cannot be negated directly.
takeUntil
in interface Traversable<T>
predicate
- A condition tested subsequently for this elements.public final Seq<T> takeWhile(java.util.function.Predicate<? super T> predicate)
Traversable
takeWhile
in interface Traversable<T>
predicate
- A condition tested subsequently for the contained elements.public final <U> Tree<Tuple2<T,U>> zip(java.lang.Iterable<? extends U> that)
Traversable
The length of the returned traversable is the minimum of the lengths of this traversable and that
iterable.
zip
in interface Traversable<T>
U
- The type of the second half of the returned pairs.that
- The Iterable providing the second half of each result pair.that
iterable.public final <U,R> Tree<R> zipWith(java.lang.Iterable<? extends U> that, java.util.function.BiFunction<? super T,? super U,? extends R> mapper)
Traversable
The length of the returned traversable is the minimum of the lengths of this traversable and that
iterable.
zipWith
in interface Traversable<T>
U
- The type of the second parameter of the mapper.R
- The type of the mapped elements.that
- The Iterable providing the second parameter of the mapper.mapper
- a mapper.that
iterable.public final <U> Tree<Tuple2<T,U>> zipAll(java.lang.Iterable<? extends U> that, T thisElem, U thatElem)
Traversable
The length of the returned traversable is the maximum of the lengths of this traversable and that
iterable.
Special case: if this traversable is shorter than that elements, and that elements contains duplicates, the resulting traversable may be shorter than the maximum of the lengths of this and that because a traversable contains an element at most once.
If this Traversable is shorter than that, thisElem values are used to fill the result. If that is shorter than this Traversable, thatElem values are used to fill the result.
zipAll
in interface Traversable<T>
U
- The type of the second half of the returned pairs.that
- The Iterable providing the second half of each result pair.thisElem
- The element to be used to fill up the result if this traversable is shorter than that.thatElem
- The element to be used to fill up the result if that is shorter than this traversable.public final Tree<Tuple2<T,java.lang.Integer>> zipWithIndex()
Traversable
zipWithIndex
in interface Traversable<T>
public final <U> Tree<U> zipWithIndex(java.util.function.BiFunction<? super T,? super java.lang.Integer,? extends U> mapper)
Traversable
The length of the returned traversable is the minimum of the lengths of this traversable and that
iterable.
zipWithIndex
in interface Traversable<T>
U
- The type of the mapped elements.mapper
- a mapper.that
iterable.public abstract java.lang.String draw()