Skip to content

Commit 147a452

Browse files
author
José Valim
committed
Fix regression on Stream nesting
1 parent b39a5d8 commit 147a452

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

lib/elixir/lib/stream.ex

+11-11
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ defmodule Stream do
106106

107107
def member?(Lazy[] = lazy, value) do
108108
do_reduce(lazy, false, fn(entry, _) ->
109-
if entry === value, do: throw({ :stream_lazy, true }), else: false
109+
if entry === value, do: throw({ :stream_lazy, 0, true }), else: false
110110
end, 0)
111111
end
112112

@@ -115,13 +115,13 @@ defmodule Stream do
115115
end
116116

117117
defp do_reduce(Lazy[enumerable: enumerable, fun: f1, acc: side], acc, fun, nesting) do
118-
do_reduce(enumerable, { acc, side }, f1.(fun), nesting + 1)
118+
do_reduce(enumerable, { acc, side }, f1.(fun, nesting), nesting + 1)
119119
end
120120

121121
defp do_reduce(enumerable, acc, fun, nesting) do
122122
Enumerable.reduce(enumerable, acc, fun) |> remove_nesting(nesting)
123123
catch
124-
{ :stream_lazy, res } -> res
124+
{ :stream_lazy, nesting, res } -> remove_nesting(res, nesting)
125125
end
126126

127127
defp remove_nesting(acc, 0), do: acc
@@ -208,7 +208,7 @@ defmodule Stream do
208208
@spec drop(Enumerable.t, non_neg_integer) :: t
209209
def drop(enumerable, n) when n >= 0 do
210210
Lazy[enumerable: enumerable,
211-
fun: fn(f1) ->
211+
fun: fn(f1, _) ->
212212
fn
213213
_entry, { acc, n } when n > 0 ->
214214
{ acc, n - 1 }
@@ -233,7 +233,7 @@ defmodule Stream do
233233
@spec drop_while(Enumerable.t, (element -> as_boolean(term))) :: t
234234
def drop_while(enumerable, f) do
235235
Lazy[enumerable: enumerable,
236-
fun: fn(f1) ->
236+
fun: fn(f1, _) ->
237237
fn
238238
entry, { acc, true } ->
239239
if f.(entry), do: { acc, true }, else: { f1.(entry, acc), false }
@@ -392,10 +392,10 @@ defmodule Stream do
392392

393393
def take(enumerable, n) when n > 0 do
394394
Lazy[enumerable: enumerable,
395-
fun: fn(f1) ->
395+
fun: fn(f1, nesting) ->
396396
fn(entry, { acc, n }) ->
397397
res = f1.(entry, acc)
398-
if n > 1, do: { res, n-1 }, else: throw { :stream_lazy, res }
398+
if n > 1, do: { res, n-1 }, else: throw { :stream_lazy, nesting, res }
399399
end
400400
end,
401401
acc: n]
@@ -415,12 +415,12 @@ defmodule Stream do
415415
@spec take_while(Enumerable.t, (element -> as_boolean(term))) :: t
416416
def take_while(enumerable, f) do
417417
Lazy[enumerable: enumerable,
418-
fun: fn(f1) ->
418+
fun: fn(f1, nesting) ->
419419
fn(entry, { acc, true }) ->
420420
if f.(entry) do
421421
{ f1.(entry, acc), true }
422422
else
423-
throw { :stream_lazy, acc }
423+
throw { :stream_lazy, nesting, acc }
424424
end
425425
end
426426
end,
@@ -441,10 +441,10 @@ defmodule Stream do
441441
@spec with_index(Enumerable.t) :: t
442442
def with_index(enumerable) do
443443
Lazy[enumerable: enumerable,
444-
fun: fn(f1) ->
444+
fun: fn(f1, _) ->
445445
fn(entry, { acc, counter }) ->
446446
acc = f1.({ entry, counter }, acc)
447-
{ acc, counter + 1}
447+
{ acc, counter + 1 }
448448
end
449449
end,
450450
acc: 0]

lib/elixir/test/elixir/stream_test.exs

+3
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ defmodule StreamTest do
156156

157157
stream = Stream.drop(1..100, 5)
158158
assert Stream.take(stream, 5) |> Enum.to_list == [6,7,8,9,10]
159+
160+
stream = 1..5 |> Stream.take(10) |> Stream.drop(15)
161+
assert { [], [] } = Enum.split(stream, 5)
159162
end
160163

161164
test :take_while do

0 commit comments

Comments
 (0)