The module Enumerable
is used by mixin
into the class object. Now the class Range
is used Enumerable
by mixin
.
The interfaces of Enumerable
can be used by the class which doing mixin
the Enumerable
module.
For example, the Range
can also use the following interfaces.
Lazy
field means the lazy operation will be performed after you call lazy()
and use the Enumerator
object returned by lazy()
.
Name | Outline | Lazy |
---|---|---|
filter(func) | Filters by the element returned by func . |
O |
map(func) | Maps by the element returned by func . |
O |
flatMap(func) | Maps by the element returned by func , then flatten the all result. |
O |
take(n) | Takes n elements from the beginning. |
O |
takeWhile(func) | Takes an element while returning true by func . |
O |
drop(n) | Skips n elements from the beginning. |
O |
dropWhile(func) | Skips an element while returning true by func . |
O |
each(func) | Applies func for each. |
O |
reduce(func, initer) | Reduces by the result of func . initer is the initial value if necessary. |
|
sort(func) | Sorts elements by comparing with the result of func |
|
all(func) | true if the result of func is all true for all elements. |
|
any(func) | true if there is at least one element which is true returned by func . |
|
toArray() | Returns elements as Array. | |
println() | Applies System.println for all elements. |
|
lazy() | Returns Enumerator for lazy operation. |
System.println((2..10).filter(&(e) => e % 2));
[3, 5, 7, 9]
(2..10).map(&(e) => e**e).each(&(e) => System.println(e));
4
27
256
3125
46656
823543
16777216
387420489
10000000000
(2..10).flatMap(&(e) => [e, e**2]).each(&(e) => System.println(e));
2
4
3
9
4
16
5
25
6
36
7
49
8
64
9
81
10
100
(2..10).take(5).each(&(e) => System.println(e));
2
3
4
5
6
(2..10).takeWhile(&(e) => e < 4).each(&(e) => System.println(e));
2
3
(2..10).drop(5).each(&(e) => System.println(e));
7
8
9
10
(2..10).dropWhile(&(e) => e < 4).each(&(e) => System.println(e));
4
5
6
7
8
9
10
(2..10).each(&(e) => System.println(e));
2
3
4
5
6
7
8
9
10
System.println((2..10).reduce(&(r, e) => r + e));
System.println((2..10).reduce(&(r, e) => r + e, 1));
54
55
System.println((2..10).sort(&(e1, e2) => e1 <=> e2));
System.println((2..10).sort(&(e1, e2) => e2 <=> e1));
[2, 3, 4, 5, 6, 7, 8, 9, 10]
[10, 9, 8, 7, 6, 5, 4, 3, 2]
System.println((2..10).all(&(e) => e < 3));
System.println((2..10).all(&(e) => e <= 10));
0
1
System.println((2..10).any(&(e) => e == 3));
System.println((2..10).any(&(e) => e == 13));
1
0
System.println((2..10).toArray());
[2, 3, 4, 5, 6, 7, 8, 9, 10]
(2..10)
.takeWhile(&(e) => e < 4)
.println();
2
3
(2..10).lazy()
.filter(&(e) => e % 2)
.take(3)
.println();
3
5
7