-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathiterable.cr
123 lines (107 loc) · 3.07 KB
/
iterable.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# The `Iterable` mixin provides convenience methods to collection classes
# that provide an `each` method that returns an `Iterator` over the collection.
module Iterable(T)
# Must return an `Iterator` over the elements in this collection.
abstract def each
# Same as `each.cycle`.
#
# See also: `Iterator#cycle`.
def cycle
each.cycle
end
# Same as `each.cycle(n)`.
#
# See also: `Iterator#cycle(n)`.
def cycle(n)
each.cycle(n)
end
# Returns an Iterator that enumerates over the items, chunking them together
# based on the return value of the block.
#
# ```
# (0..7).chunk(&.//(3)).to_a # => [{0, [0, 1, 2]}, {1, [3, 4, 5]}, {2, [6, 7]}]
# ```
#
# See also: `Iterator#chunk`.
def chunk(reuse = false, &block : T -> U) forall U
each.chunk reuse, &block
end
# Same as `each.slice(count, reuse)`.
#
# See also: `Iterator#slice(count, reuse)`.
def each_slice(count : Int, reuse = false)
each.slice(count, reuse)
end
# Same as `each.cons(count, reuse)`.
#
# See also: `Iterator#cons(count, reuse)`.
def each_cons(count : Int, reuse = false)
each.cons(count, reuse)
end
# Same as `each.cons_pair`.
#
# See also: `Iterator#cons_pair`.
def each_cons_pair
each.cons_pair
end
# Same as `each.step(n)`.
#
# See also: `Iterator#step`.
def each_step(n : Int)
each.step(n)
end
# Same as `each.skip(offset).step(n)`.
#
# See also: `Iterator#step`.
def each_step(n : Int, *, offset : Int)
each.skip(offset).step(n)
end
# Same as `each.with_index(offset)`.
#
# See also: `Iterator#with_index(offset)`.
def each_with_index(offset = 0)
each.with_index(offset)
end
# Same as `each.with_object(obj)`.
#
# See also: `Iterator#with_object(obj)`.
def each_with_object(obj)
each.with_object(obj)
end
# Same as `each.slice_after(reuse, &block)`.
#
# See also: `Iterator#slice_after(reuse, &block)`.
def slice_after(reuse : Bool | Array(T) = false, &block : T -> B) forall B
each.slice_after(reuse, &block)
end
# Same as `each.slice_after(pattern, reuse)`.
#
# See also: `Iterator#slice_after(pattern, reuse)`.
def slice_after(pattern, reuse : Bool | Array(T) = false)
each.slice_after(pattern, reuse)
end
# Same as `each.slice_before(reuse, &block)`.
#
# See also: `Iterator#slice_before(reuse, &block)`.
def slice_before(reuse : Bool | Array(T) = false, &block : T -> B) forall B
each.slice_before(reuse, &block)
end
# Same as `each.slice_before(pattern, reuse)`.
#
# See also: `Iterator#slice_before(pattern, reuse)`.
def slice_before(pattern, reuse : Bool | Array(T) = false)
each.slice_before(pattern, reuse)
end
# Same as `each.slice_when(reuse, &block)`.
#
# See also: `Iterator#slice_when`.
def slice_when(reuse : Bool | Array(T) = false, &block : T, T -> B) forall B
each.slice_when(reuse, &block)
end
# Same as `each.chunk_while(reuse, &block)`.
#
# See also: `Iterator#chunk_while`.
def chunk_while(reuse : Bool | Array(T) = false, &block : T, T -> B) forall B
each.chunk_while(reuse, &block)
end
end