Skip to content

Commit 728d08c

Browse files
author
José Valim
committed
Revert "Enum.split_while did not behave as advertised and provided the wrong API. Provide Enum.split_with instead"
This reverts commit 1ffda62.
1 parent 84cd94c commit 728d08c

File tree

2 files changed

+30
-31
lines changed

2 files changed

+30
-31
lines changed

lib/elixir/lib/enum.ex

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -771,26 +771,25 @@ defmodule Enum do
771771
end
772772

773773
@doc """
774-
Splits `collection` at the first element, for which `fun` returns true.
775-
Expects an ordered collection.
774+
Splits `collection` in two while `fun` returns true.
776775
777776
## Examples
778777
779-
Enum.split_with [1,2,3,4], fn x -> x == 2 end
780-
#=> { [1,3,4], [2] }
778+
Enum.split_while [1,2,3,4], fn(x) -> x < 3 end
779+
#=> { [1, 2], [3, 4] }
781780
782781
"""
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, [])
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, [])
786785
end
787786

788-
def split_with(collection, fun) do
787+
def split_while(collection, fun) do
789788
case I.iterator(collection) do
790789
{ iterator, pointer } ->
791-
do_split_with(pointer, iterator, fun, [])
790+
do_split_while(pointer, iterator, fun, [])
792791
list when is_list(list) ->
793-
do_split_with(list, fun, [])
792+
do_split_while(list, fun, [])
794793
end
795794
end
796795

@@ -1461,29 +1460,29 @@ defmodule Enum do
14611460
{ :lists.reverse(acc), [] }
14621461
end
14631462

1464-
## split_with
1463+
## split_while
14651464

1466-
defp do_split_with([h|t], fun, acc) do
1465+
defp do_split_while([h|t], fun, acc) do
14671466
if fun.(h) do
1468-
do_split_with(t, fun, [h|acc])
1467+
do_split_while(t, fun, [h|acc])
14691468
else
14701469
{ :lists.reverse(acc), [h|t] }
14711470
end
14721471
end
14731472

1474-
defp do_split_with([], _, acc) do
1473+
defp do_split_while([], _, acc) do
14751474
{ :lists.reverse(acc), [] }
14761475
end
14771476

1478-
defp do_split_with({ h, next } = extra, iterator, fun, acc) do
1477+
defp do_split_while({ h, next } = extra, iterator, fun, acc) do
14791478
if fun.(h) do
1480-
do_split_with(iterator.(next), iterator, fun, [h|acc])
1479+
do_split_while(iterator.(next), iterator, fun, [h|acc])
14811480
else
14821481
{ :lists.reverse(acc), to_list(extra, iterator) }
14831482
end
14841483
end
14851484

1486-
defp do_split_with(:stop, _, _, acc) do
1485+
defp do_split_while(:stop, _, _, acc) do
14871486
{ :lists.reverse(acc), [] }
14881487
end
14891488

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_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) == { [], [] }
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) == { [], [] }
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_with do
502+
test :split_while do
503503
range = Range.new(first: 1, last: 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] }
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] }
509509

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

514514
test :take do

0 commit comments

Comments
 (0)