@@ -8,8 +8,18 @@ defmodule Enums do
8
8
assert Enum . count ( [ 1 , 2 , 3 ] ) == ___
9
9
end
10
10
11
- koan "Depending on the type, it counts pairs" do
12
- assert Enum . count ( % { a: :foo , b: :bar } ) == ___
11
+ koan "Counting is similar to length" do
12
+ assert length ( [ 1 , 2 , 3 ] ) == ___
13
+ end
14
+
15
+ koan "But it allows you to count certain elements" do
16
+ assert Enum . count ( [ 1 , 2 , 3 ] , & ( & 1 == 2 ) ) == ___
17
+ end
18
+
19
+ koan "Depending on the type, it counts pairs while length does not" do
20
+ map = % { a: :foo , b: :bar }
21
+ assert Enum . count ( map ) == ___
22
+ assert_raise ___ , fn -> length ( map ) end
13
23
end
14
24
15
25
def less_than_five? ( n ) , do: n < 5
@@ -34,7 +44,7 @@ defmodule Enums do
34
44
35
45
def multiply_by_ten ( n ) , do: 10 * n
36
46
37
- koan "Map converts each element of a list by running some function with it" do
47
+ koan "Mapping converts each element of a list by running some function with it" do
38
48
assert Enum . map ( [ 1 , 2 , 3 ] , & multiply_by_ten / 1 ) == ___
39
49
end
40
50
@@ -66,7 +76,7 @@ defmodule Enums do
66
76
assert Enum . zip ( letters , numbers ) == ___
67
77
end
68
78
69
- koan "When you want to find that one pesky element" do
79
+ koan "When you want to find that one pesky element, it returns the first " do
70
80
assert Enum . find ( [ 1 , 2 , 3 , 4 ] , & even? / 1 ) == ___
71
81
end
72
82
@@ -83,4 +93,38 @@ defmodule Enums do
83
93
koan "Collapse an entire list of elements down to a single one by repeating a function." do
84
94
assert Enum . reduce ( [ 1 , 2 , 3 ] , 0 , fn element , accumulator -> element + accumulator end ) == ___
85
95
end
96
+
97
+ koan "Enum.chunk_every splits lists into smaller lists of fixed size" do
98
+ assert Enum . chunk_every ( [ 1 , 2 , 3 , 4 , 5 , 6 ] , 2 ) == ___
99
+ assert Enum . chunk_every ( [ 1 , 2 , 3 , 4 , 5 ] , 3 ) == ___
100
+ end
101
+
102
+ koan "Enum.flat_map transforms and flattens in one step" do
103
+ result =
104
+ [ 1 , 2 , 3 ]
105
+ |> Enum . flat_map ( & [ & 1 , & 1 * 10 ] )
106
+
107
+ assert result == ___
108
+ end
109
+
110
+ koan "Enum.group_by organizes elements by a grouping function" do
111
+ words = [ "apple" , "banana" , "cherry" , "apricot" , "blueberry" ]
112
+ grouped = Enum . group_by ( words , & String . first / 1 )
113
+
114
+ assert grouped [ "a" ] == ___
115
+ assert grouped [ "b" ] == ___
116
+ end
117
+
118
+ koan "Stream provides lazy enumeration for large datasets" do
119
+ # Streams are lazy - they don't execute until you call Enum on them
120
+ stream =
121
+ 1 .. 1_000_000
122
+ |> Stream . filter ( & even? / 1 )
123
+ |> Stream . map ( & ( & 1 * 2 ) )
124
+ |> Stream . take ( 3 )
125
+
126
+ # Nothing has been computed yet!
127
+ result = Enum . to_list ( stream )
128
+ assert result == ___
129
+ end
86
130
end
0 commit comments