Skip to content

Commit 1ffda62

Browse files
author
José Valim
committed
Enum.split_while did not behave as advertised and provided the wrong API. Provide Enum.split_with instead
1 parent d4eda43 commit 1ffda62

File tree

2 files changed

+30
-29
lines changed

2 files changed

+30
-29
lines changed

lib/elixir/lib/enum.ex

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -776,20 +776,21 @@ defmodule Enum do
776776
777777
## Examples
778778
779-
Enum.split_while [1,2,3,4], fn x -> x == 2 end
780-
#=> { [1], [2, 3, 4] }
779+
Enum.split_with [1,2,3,4], fn x -> x == 2 end
780+
#=> { [1,3,4], [2] }
781+
781782
"""
782-
@spec split_while(t, (element -> as_boolean(term))) :: {list, list}
783-
def split_while(collection, fun) when is_list(collection) do
784-
do_split_while(collection, fun, [])
783+
@spec split_with(t, (element -> as_boolean(term))) :: {list, list}
784+
def split_with(collection, fun) when is_list(collection) do
785+
do_split_with(collection, fun, [])
785786
end
786787

787-
def split_while(collection, fun) do
788+
def split_with(collection, fun) do
788789
case I.iterator(collection) do
789790
{ iterator, pointer } ->
790-
do_split_while(pointer, iterator, fun, [])
791+
do_split_with(pointer, iterator, fun, [])
791792
list when is_list(list) ->
792-
do_split_while(list, fun, [])
793+
do_split_with(list, fun, [])
793794
end
794795
end
795796

@@ -1460,29 +1461,29 @@ defmodule Enum do
14601461
{ :lists.reverse(acc), [] }
14611462
end
14621463

1463-
## split_while
1464+
## split_with
14641465

1465-
defp do_split_while([h|t], fun, acc) do
1466+
defp do_split_with([h|t], fun, acc) do
14661467
if fun.(h) do
1467-
do_split_while(t, fun, [h|acc])
1468+
do_split_with(t, fun, [h|acc])
14681469
else
14691470
{ :lists.reverse(acc), [h|t] }
14701471
end
14711472
end
14721473

1473-
defp do_split_while([], _, acc) do
1474+
defp do_split_with([], _, acc) do
14741475
{ :lists.reverse(acc), [] }
14751476
end
14761477

1477-
defp do_split_while({ h, next } = extra, iterator, fun, acc) do
1478+
defp do_split_with({ h, next } = extra, iterator, fun, acc) do
14781479
if fun.(h) do
1479-
do_split_while(iterator.(next), iterator, fun, [h|acc])
1480+
do_split_with(iterator.(next), iterator, fun, [h|acc])
14801481
else
14811482
{ :lists.reverse(acc), to_list(extra, iterator) }
14821483
end
14831484
end
14841485

1485-
defp do_split_while(:stop, _, _, acc) do
1486+
defp do_split_with(:stop, _, _, acc) do
14861487
{ :lists.reverse(acc), [] }
14871488
end
14881489

lib/elixir/test/elixir/enum_test.exs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,13 @@ defmodule EnumTest.List do
188188
assert Enum.split([1,2,3], -10) == { [], [1,2,3] }
189189
end
190190

191-
test :split_while do
192-
assert Enum.split_while([1,2,3], fn(_) -> false end) == { [], [1,2,3] }
193-
assert Enum.split_while([1,2,3], fn(_) -> true end) == { [1,2,3], [] }
194-
assert Enum.split_while([1,2,3], fn(x) -> x > 2 end) == { [], [1,2,3] }
195-
assert Enum.split_while([1,2,3], fn(x) -> x > 3 end) == { [], [1,2,3] }
196-
assert Enum.split_while([1,2,3], fn(x) -> x < 3 end) == { [1,2], [3] }
197-
assert Enum.split_while([], fn(_) -> true end) == { [], [] }
191+
test :split_with do
192+
assert Enum.split_with([1,2,3], fn(_) -> false end) == { [], [1,2,3] }
193+
assert Enum.split_with([1,2,3], fn(_) -> true end) == { [1,2,3], [] }
194+
assert Enum.split_with([1,2,3], fn(x) -> x > 2 end) == { [], [1,2,3] }
195+
assert Enum.split_with([1,2,3], fn(x) -> x > 3 end) == { [], [1,2,3] }
196+
assert Enum.split_with([1,2,3], fn(x) -> x < 3 end) == { [1,2], [3] }
197+
assert Enum.split_with([], fn(_) -> true end) == { [], [] }
198198
end
199199

200200
test :take do
@@ -499,16 +499,16 @@ defmodule EnumTest.Range do
499499
assert Enum.split(range, 3) == { [1,0], [] }
500500
end
501501

502-
test :split_while do
502+
test :split_with do
503503
range = Range.new(first: 1, last: 3)
504-
assert Enum.split_while(range, fn(_) -> false end) == { [], [1,2,3] }
505-
assert Enum.split_while(range, fn(_) -> true end) == { [1,2,3], [] }
506-
assert Enum.split_while(range, fn(x) -> x > 2 end) == { [], [1,2,3] }
507-
assert Enum.split_while(range, fn(x) -> x > 3 end) == { [], [1,2,3] }
508-
assert Enum.split_while(range, fn(x) -> x < 3 end) == { [1,2], [3] }
504+
assert Enum.split_with(range, fn(_) -> false end) == { [], [1,2,3] }
505+
assert Enum.split_with(range, fn(_) -> true end) == { [1,2,3], [] }
506+
assert Enum.split_with(range, fn(x) -> x > 2 end) == { [], [1,2,3] }
507+
assert Enum.split_with(range, fn(x) -> x > 3 end) == { [], [1,2,3] }
508+
assert Enum.split_with(range, fn(x) -> x < 3 end) == { [1,2], [3] }
509509

510510
range = Range.new(first: 1, last: 0)
511-
assert Enum.split_while(range, fn(_) -> true end) == { [1, 0], [] }
511+
assert Enum.split_with(range, fn(_) -> true end) == { [1, 0], [] }
512512
end
513513

514514
test :take do

0 commit comments

Comments
 (0)