@@ -40,6 +40,9 @@ defmodule StreamTest do
40
40
41
41
assert Enum . to_list ( Stream . drop ( 1 .. 5 , 0 ) ) == [ 1 , 2 , 3 , 4 , 5 ]
42
42
assert Enum . to_list ( Stream . drop ( 1 .. 3 , 5 ) ) == [ ]
43
+
44
+ nats = Stream . iterate ( 1 , & ( & 1 + 1 ) )
45
+ assert Stream . drop ( nats , 2 ) |> Enum . take ( 5 ) == [ 3 , 4 , 5 , 6 , 7 ]
43
46
end
44
47
45
48
test :drop_while do
@@ -49,12 +52,18 @@ defmodule StreamTest do
49
52
50
53
assert Enum . to_list ( Stream . drop_while ( 1 .. 5 , & 1 <= 0 ) ) == [ 1 , 2 , 3 , 4 , 5 ]
51
54
assert Enum . to_list ( Stream . drop_while ( 1 .. 3 , & 1 <= 5 ) ) == [ ]
55
+
56
+ nats = Stream . iterate ( 1 , & ( & 1 + 1 ) )
57
+ assert Stream . drop_while ( nats , & 1 <= 5 ) |> Enum . take ( 5 ) == [ 6 , 7 , 8 , 9 , 10 ]
52
58
end
53
59
54
60
test :filter do
55
61
stream = Stream . filter ( [ 1 , 2 , 3 ] , fn ( x ) -> rem ( x , 2 ) == 0 end )
56
62
assert is_lazy ( stream )
57
63
assert Enum . to_list ( stream ) == [ 2 ]
64
+
65
+ nats = Stream . iterate ( 1 , & ( & 1 + 1 ) )
66
+ assert Stream . filter ( nats , & ( rem ( & 1 , 2 ) == 0 ) ) |> Enum . take ( 5 ) == [ 2 , 4 , 6 , 8 , 10 ]
58
67
end
59
68
60
69
test :iterate do
@@ -72,15 +81,21 @@ defmodule StreamTest do
72
81
stream = Stream . map ( [ 1 , 2 , 3 ] , & 1 * 2 )
73
82
assert is_lazy ( stream )
74
83
assert Enum . to_list ( stream ) == [ 2 , 4 , 6 ]
84
+
85
+ nats = Stream . iterate ( 1 , & ( & 1 + 1 ) )
86
+ assert Stream . map ( nats , & ( & 1 * 2 ) ) |> Enum . take ( 5 ) == [ 2 , 4 , 6 , 8 , 10 ]
75
87
end
76
88
77
89
test :reject do
78
90
stream = Stream . reject ( [ 1 , 2 , 3 ] , fn ( x ) -> rem ( x , 2 ) == 0 end )
79
91
assert is_lazy ( stream )
80
92
assert Enum . to_list ( stream ) == [ 1 , 3 ]
93
+
94
+ nats = Stream . iterate ( 1 , & ( & 1 + 1 ) )
95
+ assert Stream . reject ( nats , & ( rem ( & 1 , 2 ) == 0 ) ) |> Enum . take ( 5 ) == [ 1 , 3 , 5 , 7 , 9 ]
81
96
end
82
97
83
- test :repeatedly do
98
+ test :repeatedly do
84
99
stream = Stream . repeatedly ( fn -> 1 end )
85
100
assert Enum . take ( stream , 5 ) == [ 1 , 1 , 1 , 1 , 1 ]
86
101
stream = Stream . repeatedly ( & :random . uniform / 0 )
@@ -95,6 +110,9 @@ defmodule StreamTest do
95
110
96
111
assert Enum . to_list ( Stream . take ( 1 .. 1000 , 0 ) ) == [ ]
97
112
assert Enum . to_list ( Stream . take ( 1 .. 3 , 5 ) ) == [ 1 , 2 , 3 ]
113
+
114
+ nats = Stream . iterate ( 1 , & ( & 1 + 1 ) )
115
+ assert Enum . to_list ( Stream . take ( nats , 5 ) ) == [ 1 , 2 , 3 , 4 , 5 ]
98
116
end
99
117
100
118
test :take_while do
@@ -104,12 +122,18 @@ defmodule StreamTest do
104
122
105
123
assert Enum . to_list ( Stream . take_while ( 1 .. 1000 , & 1 <= 0 ) ) == [ ]
106
124
assert Enum . to_list ( Stream . take_while ( 1 .. 3 , & 1 <= 5 ) ) == [ 1 , 2 , 3 ]
125
+
126
+ nats = Stream . iterate ( 1 , & ( & 1 + 1 ) )
127
+ assert Enum . to_list ( Stream . take_while ( nats , & ( & 1 <= 5 ) ) ) == [ 1 , 2 , 3 , 4 , 5 ]
107
128
end
108
129
109
130
test :with_index do
110
131
stream = Stream . with_index ( [ 1 , 2 , 3 ] )
111
132
assert is_lazy ( stream )
112
133
assert Enum . to_list ( stream ) == [ { 1 , 0 } , { 2 , 1 } , { 3 , 2 } ]
134
+
135
+ nats = Stream . iterate ( 1 , & ( & 1 + 1 ) )
136
+ assert Stream . with_index ( nats ) |> Enum . take ( 3 ) == [ { 1 , 0 } , { 2 , 1 } , { 3 , 2 } ]
113
137
end
114
138
115
139
defp is_lazy ( stream ) do
0 commit comments