public final class API extends Object
import static javaslang.API.*;
The For
-comprehension is syntactic sugar for nested for-loops. We write
// lazily evaluated
Iterator<R> result = For(iterable1, iterable2, ..., iterableN).yield(f);
or
Iterator<R> result =
For(iterable1, v1 ->
For(iterable2, v2 ->
...
For(iterableN).yield(vN -> f.apply(v1, v2, ..., vN))
)
);
instead of
for(T1 v1 : iterable1) {
for (T2 v2 : iterable2) {
...
for (TN vN : iterableN) {
R result = f.apply(v1, v2, ..., VN);
//
// We are forced to perform side effects to do s.th. meaningful with the result.
//
}
}
}
Please note that values like Option, Try, Future, etc. are also iterable.
Given a suitable function
f: (v1, v2, ..., vN) -> ...
and 1 <= N <= 8 iterables, the result is a Stream of the
mapped cross product elements.
{ f(v1, v2, ..., vN) | v1 ∈ iterable1, ... vN ∈ iterableN }
As with all Javaslang Values, the result of a For-comprehension can be converted
to standard Java library and Javaslang types.Modifier and Type | Class and Description |
---|---|
static class |
API.For1<T1>
For-comprehension with one Iterable.
|
static class |
API.For2<T1,T2>
For-comprehension with two Iterables.
|
static class |
API.For3<T1,T2,T3>
For-comprehension with three Iterables.
|
static class |
API.For4<T1,T2,T3,T4>
For-comprehension with 4 Iterables.
|
static class |
API.For5<T1,T2,T3,T4,T5>
For-comprehension with 5 Iterables.
|
static class |
API.For6<T1,T2,T3,T4,T5,T6>
For-comprehension with 6 Iterables.
|
static class |
API.For7<T1,T2,T3,T4,T5,T6,T7>
For-comprehension with 7 Iterables.
|
static class |
API.For8<T1,T2,T3,T4,T5,T6,T7,T8>
For-comprehension with 8 Iterables.
|
static class |
API.Match<T>
Scala-like structural pattern matching for Java.
|
Modifier and Type | Method and Description |
---|---|
static <T> API.Match.Pattern0<T> |
$()
Wildcard pattern, matches any value.
|
static <T> API.Match.Pattern0<T> |
$(Predicate<? super T> predicate)
Guard pattern, checks if a predicate is satisfied.
|
static <T> API.Match.Pattern0<T> |
$(T prototype)
Value pattern, checks for equality.
|
static <T,R> API.Match.Case<T,R> |
Case(API.Match.Pattern0<T> pattern,
Function<? super T,? extends R> f) |
static <T,R> API.Match.Case<T,R> |
Case(API.Match.Pattern0<T> pattern,
R retVal) |
static <T,R> API.Match.Case<T,R> |
Case(API.Match.Pattern0<T> pattern,
Supplier<? extends R> supplier) |
static <T,T1,R> API.Match.Case<T,R> |
Case(API.Match.Pattern1<T,T1> pattern,
Function<? super T1,? extends R> f) |
static <T,T1,R> API.Match.Case<T,R> |
Case(API.Match.Pattern1<T,T1> pattern,
R retVal) |
static <T,T1,R> API.Match.Case<T,R> |
Case(API.Match.Pattern1<T,T1> pattern,
Supplier<? extends R> supplier) |
static <T,T1,T2,R> |
Case(API.Match.Pattern2<T,T1,T2> pattern,
BiFunction<? super T1,? super T2,? extends R> f) |
static <T,T1,T2,R> |
Case(API.Match.Pattern2<T,T1,T2> pattern,
R retVal) |
static <T,T1,T2,R> |
Case(API.Match.Pattern2<T,T1,T2> pattern,
Supplier<? extends R> supplier) |
static <T,T1,T2,T3,R> |
Case(API.Match.Pattern3<T,T1,T2,T3> pattern,
Function3<? super T1,? super T2,? super T3,? extends R> f) |
static <T,T1,T2,T3,R> |
Case(API.Match.Pattern3<T,T1,T2,T3> pattern,
R retVal) |
static <T,T1,T2,T3,R> |
Case(API.Match.Pattern3<T,T1,T2,T3> pattern,
Supplier<? extends R> supplier) |
static <T,T1,T2,T3,T4,R> |
Case(API.Match.Pattern4<T,T1,T2,T3,T4> pattern,
Function4<? super T1,? super T2,? super T3,? super T4,? extends R> f) |
static <T,T1,T2,T3,T4,R> |
Case(API.Match.Pattern4<T,T1,T2,T3,T4> pattern,
R retVal) |
static <T,T1,T2,T3,T4,R> |
Case(API.Match.Pattern4<T,T1,T2,T3,T4> pattern,
Supplier<? extends R> supplier) |
static <T,T1,T2,T3,T4,T5,R> |
Case(API.Match.Pattern5<T,T1,T2,T3,T4,T5> pattern,
Function5<? super T1,? super T2,? super T3,? super T4,? super T5,? extends R> f) |
static <T,T1,T2,T3,T4,T5,R> |
Case(API.Match.Pattern5<T,T1,T2,T3,T4,T5> pattern,
R retVal) |
static <T,T1,T2,T3,T4,T5,R> |
Case(API.Match.Pattern5<T,T1,T2,T3,T4,T5> pattern,
Supplier<? extends R> supplier) |
static <T,T1,T2,T3,T4,T5,T6,R> |
Case(API.Match.Pattern6<T,T1,T2,T3,T4,T5,T6> pattern,
Function6<? super T1,? super T2,? super T3,? super T4,? super T5,? super T6,? extends R> f) |
static <T,T1,T2,T3,T4,T5,T6,R> |
Case(API.Match.Pattern6<T,T1,T2,T3,T4,T5,T6> pattern,
R retVal) |
static <T,T1,T2,T3,T4,T5,T6,R> |
Case(API.Match.Pattern6<T,T1,T2,T3,T4,T5,T6> pattern,
Supplier<? extends R> supplier) |
static <T,T1,T2,T3,T4,T5,T6,T7,R> |
Case(API.Match.Pattern7<T,T1,T2,T3,T4,T5,T6,T7> pattern,
Function7<? super T1,? super T2,? super T3,? super T4,? super T5,? super T6,? super T7,? extends R> f) |
static <T,T1,T2,T3,T4,T5,T6,T7,R> |
Case(API.Match.Pattern7<T,T1,T2,T3,T4,T5,T6,T7> pattern,
R retVal) |
static <T,T1,T2,T3,T4,T5,T6,T7,R> |
Case(API.Match.Pattern7<T,T1,T2,T3,T4,T5,T6,T7> pattern,
Supplier<? extends R> supplier) |
static <T,T1,T2,T3,T4,T5,T6,T7,T8,R> |
Case(API.Match.Pattern8<T,T1,T2,T3,T4,T5,T6,T7,T8> pattern,
Function8<? super T1,? super T2,? super T3,? super T4,? super T5,? super T6,? super T7,? super T8,? extends R> f) |
static <T,T1,T2,T3,T4,T5,T6,T7,T8,R> |
Case(API.Match.Pattern8<T,T1,T2,T3,T4,T5,T6,T7,T8> pattern,
R retVal) |
static <T,T1,T2,T3,T4,T5,T6,T7,T8,R> |
Case(API.Match.Pattern8<T,T1,T2,T3,T4,T5,T6,T7,T8> pattern,
Supplier<? extends R> supplier) |
static <T,R> API.Match.Case<T,R> |
Case(Predicate<? super T> predicate,
Function<? super T,? extends R> f) |
static <T,R> API.Match.Case<T,R> |
Case(Predicate<? super T> predicate,
R retVal) |
static <T,R> API.Match.Case<T,R> |
Case(Predicate<? super T> predicate,
Supplier<? extends R> supplier) |
static <T,R> API.Match.Case<T,R> |
Case(T value,
R retVal) |
static <T,R> API.Match.Case<T,R> |
Case(T value,
Supplier<? extends R> supplier) |
static <T,U> Iterator<U> |
For(Iterable<T> ts,
Function<? super T,? extends Iterable<U>> f)
A shortcut for
Iterator.ofAll(ts).flatMap(f) which allows us to write real for-comprehensions using
For(...).yield(...) . |
static <T1> API.For1<T1> |
For(Iterable<T1> ts1)
Creates a
For -comprehension of one Iterable. |
static <T1,T2> API.For2<T1,T2> |
For(Iterable<T1> ts1,
Iterable<T2> ts2)
Creates a
For -comprehension of two Iterables. |
static <T1,T2,T3> API.For3<T1,T2,T3> |
For(Iterable<T1> ts1,
Iterable<T2> ts2,
Iterable<T3> ts3)
Creates a
For -comprehension of three Iterables. |
static <T1,T2,T3,T4> |
For(Iterable<T1> ts1,
Iterable<T2> ts2,
Iterable<T3> ts3,
Iterable<T4> ts4)
Creates a
For -comprehension of 4 Iterables. |
static <T1,T2,T3,T4,T5> |
For(Iterable<T1> ts1,
Iterable<T2> ts2,
Iterable<T3> ts3,
Iterable<T4> ts4,
Iterable<T5> ts5)
Creates a
For -comprehension of 5 Iterables. |
static <T1,T2,T3,T4,T5,T6> |
For(Iterable<T1> ts1,
Iterable<T2> ts2,
Iterable<T3> ts3,
Iterable<T4> ts4,
Iterable<T5> ts5,
Iterable<T6> ts6)
Creates a
For -comprehension of 6 Iterables. |
static <T1,T2,T3,T4,T5,T6,T7> |
For(Iterable<T1> ts1,
Iterable<T2> ts2,
Iterable<T3> ts3,
Iterable<T4> ts4,
Iterable<T5> ts5,
Iterable<T6> ts6,
Iterable<T7> ts7)
Creates a
For -comprehension of 7 Iterables. |
static <T1,T2,T3,T4,T5,T6,T7,T8> |
For(Iterable<T1> ts1,
Iterable<T2> ts2,
Iterable<T3> ts3,
Iterable<T4> ts4,
Iterable<T5> ts5,
Iterable<T6> ts6,
Iterable<T7> ts7,
Iterable<T8> ts8)
Creates a
For -comprehension of 8 Iterables. |
static <T> API.Match<T> |
Match(T value)
Entry point of the match API.
|
static Void |
run(Runnable unit)
Runs a
unit of work and returns Void . |
public static Void run(Runnable unit)
unit
of work and returns Void
. This is helpful when a return value is expected,
e.g. by Match
:
Match(i).of(
Case(is(0), i -> run(() -> System.out.println("zero"))),
Case(is(1), i -> run(() -> System.out.println("one"))),
Case($(), o -> run(() -> System.out.println("many")))
)
unit
- A block of code to be run.Void
, namely null
public static <T,U> Iterator<U> For(Iterable<T> ts, Function<? super T,? extends Iterable<U>> f)
Iterator.ofAll(ts).flatMap(f)
which allows us to write real for-comprehensions using
For(...).yield(...)
.
Example:
For(getPersons(), person ->
For(person.getTweets(), tweet ->
For(tweet.getReplies())
.yield(reply -> person + ", " + tweet + ", " + reply)));
T
- element type of ts
U
- component type of the resulting Iterator
ts
- An iterablef
- A function T -> Iterable<U>
public static <T1> API.For1<T1> For(Iterable<T1> ts1)
For
-comprehension of one Iterable.T1
- component type of the 1st Iterablets1
- the 1st IterableFor
-comprehension of arity 1public static <T1,T2> API.For2<T1,T2> For(Iterable<T1> ts1, Iterable<T2> ts2)
For
-comprehension of two Iterables.T1
- component type of the 1st IterableT2
- component type of the 2nd Iterablets1
- the 1st Iterablets2
- the 2nd IterableFor
-comprehension of arity 2public static <T1,T2,T3> API.For3<T1,T2,T3> For(Iterable<T1> ts1, Iterable<T2> ts2, Iterable<T3> ts3)
For
-comprehension of three Iterables.T1
- component type of the 1st IterableT2
- component type of the 2nd IterableT3
- component type of the 3rd Iterablets1
- the 1st Iterablets2
- the 2nd Iterablets3
- the 3rd IterableFor
-comprehension of arity 3public static <T1,T2,T3,T4> API.For4<T1,T2,T3,T4> For(Iterable<T1> ts1, Iterable<T2> ts2, Iterable<T3> ts3, Iterable<T4> ts4)
For
-comprehension of 4 Iterables.T1
- component type of the 1st IterableT2
- component type of the 2nd IterableT3
- component type of the 3rd IterableT4
- component type of the 4th Iterablets1
- the 1st Iterablets2
- the 2nd Iterablets3
- the 3rd Iterablets4
- the 4th IterableFor
-comprehension of arity 4public static <T1,T2,T3,T4,T5> API.For5<T1,T2,T3,T4,T5> For(Iterable<T1> ts1, Iterable<T2> ts2, Iterable<T3> ts3, Iterable<T4> ts4, Iterable<T5> ts5)
For
-comprehension of 5 Iterables.T1
- component type of the 1st IterableT2
- component type of the 2nd IterableT3
- component type of the 3rd IterableT4
- component type of the 4th IterableT5
- component type of the 5th Iterablets1
- the 1st Iterablets2
- the 2nd Iterablets3
- the 3rd Iterablets4
- the 4th Iterablets5
- the 5th IterableFor
-comprehension of arity 5public static <T1,T2,T3,T4,T5,T6> API.For6<T1,T2,T3,T4,T5,T6> For(Iterable<T1> ts1, Iterable<T2> ts2, Iterable<T3> ts3, Iterable<T4> ts4, Iterable<T5> ts5, Iterable<T6> ts6)
For
-comprehension of 6 Iterables.T1
- component type of the 1st IterableT2
- component type of the 2nd IterableT3
- component type of the 3rd IterableT4
- component type of the 4th IterableT5
- component type of the 5th IterableT6
- component type of the 6th Iterablets1
- the 1st Iterablets2
- the 2nd Iterablets3
- the 3rd Iterablets4
- the 4th Iterablets5
- the 5th Iterablets6
- the 6th IterableFor
-comprehension of arity 6public static <T1,T2,T3,T4,T5,T6,T7> API.For7<T1,T2,T3,T4,T5,T6,T7> For(Iterable<T1> ts1, Iterable<T2> ts2, Iterable<T3> ts3, Iterable<T4> ts4, Iterable<T5> ts5, Iterable<T6> ts6, Iterable<T7> ts7)
For
-comprehension of 7 Iterables.T1
- component type of the 1st IterableT2
- component type of the 2nd IterableT3
- component type of the 3rd IterableT4
- component type of the 4th IterableT5
- component type of the 5th IterableT6
- component type of the 6th IterableT7
- component type of the 7th Iterablets1
- the 1st Iterablets2
- the 2nd Iterablets3
- the 3rd Iterablets4
- the 4th Iterablets5
- the 5th Iterablets6
- the 6th Iterablets7
- the 7th IterableFor
-comprehension of arity 7public static <T1,T2,T3,T4,T5,T6,T7,T8> API.For8<T1,T2,T3,T4,T5,T6,T7,T8> For(Iterable<T1> ts1, Iterable<T2> ts2, Iterable<T3> ts3, Iterable<T4> ts4, Iterable<T5> ts5, Iterable<T6> ts6, Iterable<T7> ts7, Iterable<T8> ts8)
For
-comprehension of 8 Iterables.T1
- component type of the 1st IterableT2
- component type of the 2nd IterableT3
- component type of the 3rd IterableT4
- component type of the 4th IterableT5
- component type of the 5th IterableT6
- component type of the 6th IterableT7
- component type of the 7th IterableT8
- component type of the 8th Iterablets1
- the 1st Iterablets2
- the 2nd Iterablets3
- the 3rd Iterablets4
- the 4th Iterablets5
- the 5th Iterablets6
- the 6th Iterablets7
- the 7th Iterablets8
- the 8th IterableFor
-comprehension of arity 8public static <T> API.Match<T> Match(T value)
T
- type of the valuevalue
- a value to be matchedMatch
instancepublic static <T,R> API.Match.Case<T,R> Case(T value, Supplier<? extends R> supplier)
public static <T,R> API.Match.Case<T,R> Case(T value, R retVal)
public static <T,R> API.Match.Case<T,R> Case(Predicate<? super T> predicate, Function<? super T,? extends R> f)
public static <T,R> API.Match.Case<T,R> Case(Predicate<? super T> predicate, Supplier<? extends R> supplier)
public static <T,R> API.Match.Case<T,R> Case(Predicate<? super T> predicate, R retVal)
public static <T,R> API.Match.Case<T,R> Case(API.Match.Pattern0<T> pattern, Function<? super T,? extends R> f)
public static <T,R> API.Match.Case<T,R> Case(API.Match.Pattern0<T> pattern, Supplier<? extends R> supplier)
public static <T,R> API.Match.Case<T,R> Case(API.Match.Pattern0<T> pattern, R retVal)
public static <T,T1,R> API.Match.Case<T,R> Case(API.Match.Pattern1<T,T1> pattern, Function<? super T1,? extends R> f)
public static <T,T1,R> API.Match.Case<T,R> Case(API.Match.Pattern1<T,T1> pattern, Supplier<? extends R> supplier)
public static <T,T1,R> API.Match.Case<T,R> Case(API.Match.Pattern1<T,T1> pattern, R retVal)
public static <T,T1,T2,R> API.Match.Case<T,R> Case(API.Match.Pattern2<T,T1,T2> pattern, BiFunction<? super T1,? super T2,? extends R> f)
public static <T,T1,T2,R> API.Match.Case<T,R> Case(API.Match.Pattern2<T,T1,T2> pattern, Supplier<? extends R> supplier)
public static <T,T1,T2,R> API.Match.Case<T,R> Case(API.Match.Pattern2<T,T1,T2> pattern, R retVal)
public static <T,T1,T2,T3,R> API.Match.Case<T,R> Case(API.Match.Pattern3<T,T1,T2,T3> pattern, Function3<? super T1,? super T2,? super T3,? extends R> f)
public static <T,T1,T2,T3,R> API.Match.Case<T,R> Case(API.Match.Pattern3<T,T1,T2,T3> pattern, Supplier<? extends R> supplier)
public static <T,T1,T2,T3,R> API.Match.Case<T,R> Case(API.Match.Pattern3<T,T1,T2,T3> pattern, R retVal)
public static <T,T1,T2,T3,T4,R> API.Match.Case<T,R> Case(API.Match.Pattern4<T,T1,T2,T3,T4> pattern, Function4<? super T1,? super T2,? super T3,? super T4,? extends R> f)
public static <T,T1,T2,T3,T4,R> API.Match.Case<T,R> Case(API.Match.Pattern4<T,T1,T2,T3,T4> pattern, Supplier<? extends R> supplier)
public static <T,T1,T2,T3,T4,R> API.Match.Case<T,R> Case(API.Match.Pattern4<T,T1,T2,T3,T4> pattern, R retVal)
public static <T,T1,T2,T3,T4,T5,R> API.Match.Case<T,R> Case(API.Match.Pattern5<T,T1,T2,T3,T4,T5> pattern, Function5<? super T1,? super T2,? super T3,? super T4,? super T5,? extends R> f)
public static <T,T1,T2,T3,T4,T5,R> API.Match.Case<T,R> Case(API.Match.Pattern5<T,T1,T2,T3,T4,T5> pattern, Supplier<? extends R> supplier)
public static <T,T1,T2,T3,T4,T5,R> API.Match.Case<T,R> Case(API.Match.Pattern5<T,T1,T2,T3,T4,T5> pattern, R retVal)
public static <T,T1,T2,T3,T4,T5,T6,R> API.Match.Case<T,R> Case(API.Match.Pattern6<T,T1,T2,T3,T4,T5,T6> pattern, Function6<? super T1,? super T2,? super T3,? super T4,? super T5,? super T6,? extends R> f)
public static <T,T1,T2,T3,T4,T5,T6,R> API.Match.Case<T,R> Case(API.Match.Pattern6<T,T1,T2,T3,T4,T5,T6> pattern, Supplier<? extends R> supplier)
public static <T,T1,T2,T3,T4,T5,T6,R> API.Match.Case<T,R> Case(API.Match.Pattern6<T,T1,T2,T3,T4,T5,T6> pattern, R retVal)
public static <T,T1,T2,T3,T4,T5,T6,T7,R> API.Match.Case<T,R> Case(API.Match.Pattern7<T,T1,T2,T3,T4,T5,T6,T7> pattern, Function7<? super T1,? super T2,? super T3,? super T4,? super T5,? super T6,? super T7,? extends R> f)
public static <T,T1,T2,T3,T4,T5,T6,T7,R> API.Match.Case<T,R> Case(API.Match.Pattern7<T,T1,T2,T3,T4,T5,T6,T7> pattern, Supplier<? extends R> supplier)
public static <T,T1,T2,T3,T4,T5,T6,T7,R> API.Match.Case<T,R> Case(API.Match.Pattern7<T,T1,T2,T3,T4,T5,T6,T7> pattern, R retVal)
public static <T,T1,T2,T3,T4,T5,T6,T7,T8,R> API.Match.Case<T,R> Case(API.Match.Pattern8<T,T1,T2,T3,T4,T5,T6,T7,T8> pattern, Function8<? super T1,? super T2,? super T3,? super T4,? super T5,? super T6,? super T7,? super T8,? extends R> f)
public static <T,T1,T2,T3,T4,T5,T6,T7,T8,R> API.Match.Case<T,R> Case(API.Match.Pattern8<T,T1,T2,T3,T4,T5,T6,T7,T8> pattern, Supplier<? extends R> supplier)
public static <T,T1,T2,T3,T4,T5,T6,T7,T8,R> API.Match.Case<T,R> Case(API.Match.Pattern8<T,T1,T2,T3,T4,T5,T6,T7,T8> pattern, R retVal)
public static <T> API.Match.Pattern0<T> $()
T
- injected type of the underlying valuePattern0
instancepublic static <T> API.Match.Pattern0<T> $(T prototype)
T
- type of the prototypeprototype
- the value that should be equal to the underlying objectPattern0
instancepublic static <T> API.Match.Pattern0<T> $(Predicate<? super T> predicate)
T
- type of the prototypepredicate
- the predicate that tests a given valuePattern0
instanceCopyright © 2017. All Rights Reserved.