T - The element typepublic static interface C.Traversable<T> extends Iterable<T>, C.Featured
Define a traversable structure with functional programming support, including map, reduce etc.
C.Featured.Factory| Modifier and Type | Method and Description |
|---|---|
C.Traversable<T> |
accept(Lang.Visitor<? super T> visitor)
Iterate this
Traversable with a visitor function. |
boolean |
allMatch(Lang.Function<? super T,Boolean> predicate)
Check if all elements match the predicate specified
|
boolean |
anyMatch(Lang.Function<? super T,Boolean> predicate)
Check if any elements matches the predicate specified
|
<R> C.Traversable<R> |
collect(String path) |
C.Traversable<T> |
each(Lang.Visitor<? super T> visitor)
Alias of
accept(Lang.Visitor) |
C.Traversable<T> |
eager()
Returns this traversable and turn off
C.Feature.LAZY |
C.Traversable<T> |
filter(Lang.Function<? super T,Boolean> predicate)
Returns an new traversable that contains all elements in the current traversable except that does not pass the test of the filter function specified.
|
Lang.Option<T> |
findOne(Lang.Function<? super T,Boolean> predicate)
Returns an element that matches the predicate specified.
|
<R> C.Traversable<R> |
flatMap(Lang.Function<? super T,? extends Iterable<? extends R>> mapper)
Returns a traversable consisting of the results of replacing each element of this stream with the contents of the iterable produced by applying the provided mapping function to each element.
|
C.Traversable<T> |
forEach(Lang.Visitor<? super T> visitor)
Alias of
accept(Lang.Visitor) |
boolean |
isEmpty()
Is this traversal empty?
|
C.Traversable<T> |
lazy()
Returns this traversable and try to turn on
C.Feature.LAZY. |
<R> C.Traversable<R> |
map(Lang.Function<? super T,? extends R> mapper)
Returns an new traversable with a mapper function specified.
|
boolean |
noneMatch(Lang.Function<? super T,Boolean> predicate)
Check if no elements matches the predicate specified.
|
C.Traversable<T> |
parallel()
Returns this traversable and try to turn on
C.Feature.PARALLEL. |
Lang.Option<T> |
reduce(Lang.Func2<T,T,T> accumulator)
Performs a reduction on the elements in this traversable, using provided accumulating function.
|
<R> R |
reduce(R identity,
Lang.Func2<R,T,R> accumulator)
Performs a reduction on the elements in this traversable, using the provided identity and accumulating function.
|
C.Traversable<T> |
sequential()
Returns this traversable and turn off
C.Feature.PARALLEL |
int |
size()
Return the size of this traversal
|
forEach, iterator, spliteratorfeatures, isC.Traversable<T> parallel()
Returns this traversable and try to turn on C.Feature.PARALLEL. If this traversable does not support C.Feature.PARALLEL then return this traversable directly without any state change
C.Traversable<T> sequential()
Returns this traversable and turn off C.Feature.PARALLEL
C.Traversable<T> lazy()
Returns this traversable and try to turn on C.Feature.LAZY. If lazy is not supported then return this traversable directly without any state change
C.Traversable<T> eager()
Returns this traversable and turn off C.Feature.LAZY
boolean isEmpty()
Is this traversal empty?
true if the traversal is empty or false otherwiseint size() throws UnsupportedOperationException
Return the size of this traversal
UnsupportedOperationException - if this structure does not support this method<R> C.Traversable<R> map(Lang.Function<? super T,? extends R> mapper)
Returns an new traversable with a mapper function specified. The element in the new traversal is the result of the mapper function applied to this traversal element.
Traversable traversable = C.list(23, .NONE, null); assertEquals(C.list(true, false, false), traversal.map(.F.NOT_NULL)); assertEquals(C.list(“23”, "“, ”"), traversal.map(_.F.AS_STRING));
For Lazy Traversable, it must use lazy evaluation for this method. Otherwise it is up to implementation to decide whether use lazy evaluation or not
R - the element type of the new traversalmapper - the function that applied to element in this traversal and returns element in the result traversal<R> C.Traversable<R> flatMap(Lang.Function<? super T,? extends Iterable<? extends R>> mapper)
Returns a traversable consisting of the results of replacing each element of this stream with the contents of the iterable produced by applying the provided mapping function to each element. If the result of the mapping function is null, this is treated as if the result is an empty traversable.
R - the element type of the the new traversablemapper - the function produce an iterable when applied to an element<R> C.Traversable<R> collect(String path)
C.Traversable<T> filter(Lang.Function<? super T,Boolean> predicate)
Returns an new traversable that contains all elements in the current traversable except that does not pass the test of the filter function specified.
Traversable traversable = C.list(-1, 0, 1, -3, 7); Traversable filtered = traversable.filter(_.F.gt(0)); assertTrue(filtered.contains(1)); assertFalse(filtered.contains(-3));
predicate - the function that test if the element in the traversable should be kept in the resulting traversable. When applying the filter function to the element, if the result is true then the element will be kept in the resulting traversable.<R> R reduce(R identity,
Lang.Func2<R,T,R> accumulator)
Performs a reduction on the elements in this traversable, using the provided identity and accumulating function. This might be equivalent to:
R result = identity; for (T element: this traversable) { result = accumulator.apply(result, element); } return result; The above shows a typical left side reduce. However depending on the implementation, it might choose another way to do the reduction, including reduction in a parallel way
R - the type of identity and the return valueidentity - the identity value for the accumulating functionaccumulator - the function the combine two valuesLang.Option<T> reduce(Lang.Func2<T,T,T> accumulator)
Performs a reduction on the elements in this traversable, using provided accumulating function. This might be equivalent to:
boolean found = false; T result = null; for (T element: this traversable) { if (found) { result = accumulator.apply(result, element); } else { found = true; result = element; } } return found ? _.some(result) : _.none(); The above shows a typical left side reduction. However depending on the implementation, it might choose another way to do the reduction, including reduction in a parallel way
accumulator - the function takes previous accumulating result and the current element being iteratedLang.none() if the structure is emptyboolean allMatch(Lang.Function<? super T,Boolean> predicate)
Check if all elements match the predicate specified
predicate - the function to test the elementtrue if all elements match the predicateboolean anyMatch(Lang.Function<? super T,Boolean> predicate)
Check if any elements matches the predicate specified
predicate - the function to test the elementtrue if any element matches the predicateboolean noneMatch(Lang.Function<? super T,Boolean> predicate)
Check if no elements matches the predicate specified. This should be equivalent to:
this.allMatch(_.F.negate(predicate));
predicate - the function to test the elementtrue if none element matches the predicateLang.Option<T> findOne(Lang.Function<? super T,Boolean> predicate)
Returns an element that matches the predicate specified. The interface does not indicate if it should be the first element matches the predicate be returned or in case of parallel computing, whatever element matches found first is returned. It’s all up to the implementation to refine the semantic of this method
predicate - the function map element to BooleanLang.NONE if no element matchesC.Traversable<T> accept(Lang.Visitor<? super T> visitor)
Iterate this Traversable with a visitor function. This method does not specify the approach to iterate through this structure. The implementation might choose iterate from left to right, or vice versa. It might even choose to split the structure into multiple parts, and iterate through them in parallel
visitor - a function that apply to element in this Traversable. The return value of the function is ignoredTraversable instance for chained callC.Traversable<T> each(Lang.Visitor<? super T> visitor)
Alias of accept(Lang.Visitor)
visitor - the visitor to tranverse the elementsTraversable instanceC.Traversable<T> forEach(Lang.Visitor<? super T> visitor)
Alias of accept(Lang.Visitor)
visitor - the visitor functionTraversable instanceCopyright © 2014–2021 OSGL (Open Source General Library). All rights reserved.