Skip to content

Commit b1cf5d9

Browse files
author
José Valim
committed
Merge pull request #1667 from betawaffle/lazy-throw-fix
Fix Stream.take/2 and Stream.take_while/2 for Stream.Lazy
2 parents 447d221 + e525fb3 commit b1cf5d9

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

lib/elixir/lib/stream.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,10 @@ defmodule Stream do
115115
end
116116

117117
defp do_reduce(Lazy[enumerable: enumerable, fun: f1, acc: side], acc, fun) do
118-
do_reduce(enumerable, { acc, side }, f1.(fun)) |> elem(0)
118+
case do_reduce(enumerable, { acc, side }, f1.(fun)) do
119+
{ acc, _ } -> acc
120+
acc -> acc
121+
end
119122
end
120123

121124
defp do_reduce(enumerable, acc, fun) do

lib/elixir/test/elixir/stream_test.exs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ defmodule StreamTest do
153153

154154
nats = Stream.iterate(1, &(&1 + 1))
155155
assert Enum.to_list(Stream.take(nats, 5)) == [1,2,3,4,5]
156+
157+
stream = Stream.drop(1..100, 5)
158+
assert Stream.take(stream, 5) |> Enum.to_list == [6,7,8,9,10]
156159
end
157160

158161
test :take_while do
@@ -165,6 +168,9 @@ defmodule StreamTest do
165168

166169
nats = Stream.iterate(1, &(&1 + 1))
167170
assert Enum.to_list(Stream.take_while(nats, &(&1 <= 5))) == [1,2,3,4,5]
171+
172+
stream = Stream.drop(1..100, 5)
173+
assert Stream.take_while(stream, &(&1 < 11)) |> Enum.to_list == [6,7,8,9,10]
168174
end
169175

170176
test :with_index do

0 commit comments

Comments
 (0)