Skip to content

Commit 067bdde

Browse files
author
José Valim
committed
Deprecate Enum.chunks* in favor of Enum.chunk*
1 parent fcad4a1 commit 067bdde

File tree

6 files changed

+96
-77
lines changed

6 files changed

+96
-77
lines changed

CHANGELOG.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,19 @@
88
* [Kernel] Implement `defmodule/2`, `@/1`, `def/2` and friends in Elixir itself. `case/2`, `try/2` and `receive/1` have been made special forms. `var!/1`, `var!/2` and `alias!/1` have also been implemented in Elixir and demoted from special forms
99
* [Record] Support dynamic fields in `defrecordp`
1010
* [Stream] Add `Stream.resource/3`
11-
* [Stream] Add `Stream.zip/2`, `Stream.filter_map/3`, `Stream.each/2`, `Stream.take_every/2`, `Stream.chunks/2`, `Stream.chunks/3`, `Stream.chunks/4`, `Stream.chunks_by/2`, `Stream.scan/2`, `Stream.scan/3`, `Stream.uniq/2`, `Stream.after/2` and `Stream.run/1`
11+
* [Stream] Add `Stream.zip/2`, `Stream.filter_map/3`, `Stream.each/2`, `Stream.take_every/2`, `Stream.chunk/2`, `Stream.chunk/3`, `Stream.chunk/4`, `Stream.chunk_by/2`, `Stream.scan/2`, `Stream.scan/3`, `Stream.uniq/2`, `Stream.after/2` and `Stream.run/1`
1212
* [Stream] Support `Stream.take/2` and `Stream.drop/2` with negative counts
1313
* [Typespec] Support `is_var/1` in typespecs
1414

1515
* Bug fixes
1616
* [HashDict] Ensure a `HashDict` stored in an attribute can be accessed via the attribute
17-
* [Enum] Fix bug in `Enum.chunks/4` where you'd get an extra element when there enumerable was a multiple of the counter and a pad was given
17+
* [Enum] Fix bug in `Enum.chunk/4` where you'd get an extra element when there enumerable was a multiple of the counter and a pad was given
1818
* [Kernel] `quote location: :keep` now only affects definitions in order to keep the proper trace in definition exceptions
1919
* [Mix] Also symlink `include` directories in _build dependencies
2020

2121
* Deprecations
2222
* [Enum] `Enumerable.count/1` and `Enumerable.member?/2` should now return tagged tuples. Please see `Enumerable` docs for more info
23+
* [Enum] Deprecate `Enum.chunks/2`, `Enum.chunks/4` and `Enum.chunks_by/2` in favor of `Enum.chunk/2`, `Enum.chunk/4` and `Enum.chunk_by/2`
2324
* [File] `File.binstream!/3` is deprecated. Simply use `File.stream!/3` which is able to figure out if `stream` or `binstream` operations should be used
2425
* [Macro] `Macro.extract_args/1` is deprecated in favor of `Macro.decompose_call/1`
2526
* [Typespec] `when` clauses in typespecs were moved to the outer part of the spec
@@ -146,7 +147,7 @@
146147
* Enhancements
147148
* [CLI] Add `--verbose` to elixirc, which now is non-verbose by default
148149
* [Dict] Add `Dict.Behaviour` as a convenience to create your own dictionaries
149-
* [Enum] Add `Enum.split/2`, `Enum.reduce/2`, `Enum.flat_map/2`, `Enum.chunks/2`, `Enum.chunks/4`, `Enum.chunks_by/2`, `Enum.concat/1` and `Enum.concat/2`
150+
* [Enum] Add `Enum.split/2`, `Enum.reduce/2`, `Enum.flat_map/2`, `Enum.chunk/2`, `Enum.chunk/4`, `Enum.chunk_by/2`, `Enum.concat/1` and `Enum.concat/2`
150151
* [Enum] Support negative indices in `Enum.at/fetch/fetch!`
151152
* [ExUnit] Show failures on CLIFormatter as soon as they pop up
152153
* [IEx] Allow for strings in `h` helper

lib/elixir/lib/enum.ex

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,29 @@ defmodule Enum do
282282
end
283283
end
284284

285+
@doc false
286+
def chunks(coll, n) do
287+
IO.write "Enum.chunks/2 is deprecated, please use Enum.chunk/2 instead\n#{Exception.format_stacktrace}"
288+
chunk(coll, n)
289+
end
290+
291+
@doc false
292+
def chunks(coll, n, step, pad) do
293+
IO.write "Enum.chunks/4 is deprecated, please use Enum.chunk/4 instead\n#{Exception.format_stacktrace}"
294+
chunk(coll, n, step, pad)
295+
end
296+
297+
@doc false
298+
def chunks_by(coll, fun) do
299+
IO.write "Enum.chunks_by/2 is deprecated, please use Enum.chunk_by/2 instead\n#{Exception.format_stacktrace}"
300+
chunk_by(coll, fun)
301+
end
302+
285303
@doc """
286-
Shortcut to `chunks(coll, n, n)`.
304+
Shortcut to `chunk(coll, n, n)`.
287305
"""
288-
@spec chunks(t, non_neg_integer) :: [list]
289-
def chunks(coll, n), do: chunks(coll, n, n, nil)
306+
@spec chunk(t, non_neg_integer) :: [list]
307+
def chunk(coll, n), do: chunk(coll, n, n, nil)
290308

291309
@doc """
292310
Returns a collection of lists containing `n` items each, where
@@ -303,23 +321,23 @@ defmodule Enum do
303321
304322
## Examples
305323
306-
iex> Enum.chunks([1, 2, 3, 4, 5, 6], 2)
324+
iex> Enum.chunk([1, 2, 3, 4, 5, 6], 2)
307325
[[1, 2], [3, 4], [5, 6]]
308-
iex> Enum.chunks([1, 2, 3, 4, 5, 6], 3, 2)
326+
iex> Enum.chunk([1, 2, 3, 4, 5, 6], 3, 2)
309327
[[1, 2, 3], [3, 4, 5]]
310-
iex> Enum.chunks([1, 2, 3, 4, 5, 6], 3, 2, [7])
328+
iex> Enum.chunk([1, 2, 3, 4, 5, 6], 3, 2, [7])
311329
[[1, 2, 3], [3, 4, 5], [5, 6, 7]]
312-
iex> Enum.chunks([1, 2, 3, 4, 5, 6], 3, 3, [])
330+
iex> Enum.chunk([1, 2, 3, 4, 5, 6], 3, 3, [])
313331
[[1, 2, 3], [4, 5, 6]]
314332
315333
"""
316-
@spec chunks(t, non_neg_integer, non_neg_integer) :: [list]
317-
@spec chunks(t, non_neg_integer, non_neg_integer, t | nil) :: [list]
318-
def chunks(coll, n, step, pad // nil) when n > 0 and step > 0 do
334+
@spec chunk(t, non_neg_integer, non_neg_integer) :: [list]
335+
@spec chunk(t, non_neg_integer, non_neg_integer, t | nil) :: [list]
336+
def chunk(coll, n, step, pad // nil) when n > 0 and step > 0 do
319337
limit = :erlang.max(n, step)
320338

321339
{ _, { acc, { buffer, i } } } =
322-
Enumerable.reduce(coll, { :cont, { [], { [], 0 } } }, R.chunks(n, step, limit))
340+
Enumerable.reduce(coll, { :cont, { [], { [], 0 } } }, R.chunk(n, step, limit))
323341

324342
if nil?(pad) || i == 0 do
325343
:lists.reverse(acc)
@@ -334,14 +352,14 @@ defmodule Enum do
334352
335353
## Examples
336354
337-
iex> Enum.chunks_by([1, 2, 2, 3, 4, 4, 6, 7, 7], &(rem(&1, 2) == 1))
355+
iex> Enum.chunk_by([1, 2, 2, 3, 4, 4, 6, 7, 7], &(rem(&1, 2) == 1))
338356
[[1], [2, 2], [3], [4, 4, 6], [7, 7]]
339357
340358
"""
341-
@spec chunks_by(t, (element -> any)) :: [list]
342-
def chunks_by(coll, fun) do
359+
@spec chunk_by(t, (element -> any)) :: [list]
360+
def chunk_by(coll, fun) do
343361
{ _, { acc, res } } =
344-
Enumerable.reduce(coll, { :cont, { [], nil } }, R.chunks_by(fun))
362+
Enumerable.reduce(coll, { :cont, { [], nil } }, R.chunk_by(fun))
345363

346364
case res do
347365
{ buffer, _ } ->

lib/elixir/lib/stream.ex

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,10 @@ defmodule Stream do
180180
def unquote(:after)(enum, fun), do: Lazy[enum: enum, after: [fun]]
181181

182182
@doc """
183-
Shortcut to `chunks(enum, n, n)`.
183+
Shortcut to `chunk(enum, n, n)`.
184184
"""
185-
@spec chunks(Enumerable.t, non_neg_integer) :: Enumerable.t
186-
def chunks(enum, n), do: chunks(enum, n, n, nil)
185+
@spec chunk(Enumerable.t, non_neg_integer) :: Enumerable.t
186+
def chunk(enum, n), do: chunk(enum, n, n, nil)
187187

188188
@doc """
189189
Streams the enumerable in chunks, containing `n` items each, where
@@ -200,26 +200,26 @@ defmodule Stream do
200200
201201
## Examples
202202
203-
iex> Stream.chunks([1, 2, 3, 4, 5, 6], 2) |> Enum.to_list
203+
iex> Stream.chunk([1, 2, 3, 4, 5, 6], 2) |> Enum.to_list
204204
[[1, 2], [3, 4], [5, 6]]
205-
iex> Stream.chunks([1, 2, 3, 4, 5, 6], 3, 2) |> Enum.to_list
205+
iex> Stream.chunk([1, 2, 3, 4, 5, 6], 3, 2) |> Enum.to_list
206206
[[1, 2, 3], [3, 4, 5]]
207-
iex> Stream.chunks([1, 2, 3, 4, 5, 6], 3, 2, [7]) |> Enum.to_list
207+
iex> Stream.chunk([1, 2, 3, 4, 5, 6], 3, 2, [7]) |> Enum.to_list
208208
[[1, 2, 3], [3, 4, 5], [5, 6, 7]]
209-
iex> Stream.chunks([1, 2, 3, 4, 5, 6], 3, 3, []) |> Enum.to_list
209+
iex> Stream.chunk([1, 2, 3, 4, 5, 6], 3, 3, []) |> Enum.to_list
210210
[[1, 2, 3], [4, 5, 6]]
211211
212212
"""
213-
@spec chunks(Enumerable.t, non_neg_integer, non_neg_integer) :: Enumerable.t
214-
@spec chunks(Enumerable.t, non_neg_integer, non_neg_integer, Enumerable.t | nil) :: Enumerable.t
215-
def chunks(enum, n, step, pad // nil) when n > 0 and step > 0 do
213+
@spec chunk(Enumerable.t, non_neg_integer, non_neg_integer) :: Enumerable.t
214+
@spec chunk(Enumerable.t, non_neg_integer, non_neg_integer, Enumerable.t | nil) :: Enumerable.t
215+
def chunk(enum, n, step, pad // nil) when n > 0 and step > 0 do
216216
limit = :erlang.max(n, step)
217217
lazy enum, { [], 0 },
218-
fn(f1) -> R.chunks(n, step, limit, f1) end,
219-
fn(f1) -> &do_chunks(&1, n, pad, f1) end
218+
fn(f1) -> R.chunk(n, step, limit, f1) end,
219+
fn(f1) -> &do_chunk(&1, n, pad, f1) end
220220
end
221221

222-
defp do_chunks(acc(h, { buffer, count } = old, t) = acc, n, pad, f1) do
222+
defp do_chunk(acc(h, { buffer, count } = old, t) = acc, n, pad, f1) do
223223
if nil?(pad) || count == 0 do
224224
{ :cont, acc }
225225
else
@@ -235,23 +235,23 @@ defmodule Stream do
235235
236236
## Examples
237237
238-
iex> stream = Stream.chunks_by([1, 2, 2, 3, 4, 4, 6, 7, 7], &(rem(&1, 2) == 1))
238+
iex> stream = Stream.chunk_by([1, 2, 2, 3, 4, 4, 6, 7, 7], &(rem(&1, 2) == 1))
239239
iex> Enum.to_list(stream)
240240
[[1], [2, 2], [3], [4, 4, 6], [7, 7]]
241241
242242
"""
243-
@spec chunks_by(Enumerable.t, (element -> any)) :: Enumerable.t
244-
def chunks_by(enum, fun) do
243+
@spec chunk_by(Enumerable.t, (element -> any)) :: Enumerable.t
244+
def chunk_by(enum, fun) do
245245
lazy enum, nil,
246-
fn(f1) -> R.chunks_by(fun, f1) end,
247-
fn(f1) -> &do_chunks_by(&1, f1) end
246+
fn(f1) -> R.chunk_by(fun, f1) end,
247+
fn(f1) -> &do_chunk_by(&1, f1) end
248248
end
249249

250-
defp do_chunks_by(acc(_, nil, _) = acc, _f1) do
250+
defp do_chunk_by(acc(_, nil, _) = acc, _f1) do
251251
{ :cont, acc }
252252
end
253253

254-
defp do_chunks_by(acc(h, { buffer, _ }, t), f1) do
254+
defp do_chunk_by(acc(h, { buffer, _ }, t), f1) do
255255
cont_with_acc(f1, :lists.reverse(buffer), h, nil, t)
256256
end
257257

lib/elixir/lib/stream/reducers.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ defmodule Stream.Reducers do
22
# Collection of reducers shared by Enum and Stream.
33
@moduledoc false
44

5-
defmacro chunks(n, step, limit, f // nil) do
5+
defmacro chunk(n, step, limit, f // nil) do
66
quote do
77
fn entry, acc(h, { buffer, count }, t) ->
88
buffer = [entry|buffer]
@@ -25,7 +25,7 @@ defmodule Stream.Reducers do
2525
end
2626
end
2727

28-
defmacro chunks_by(callback, f // nil) do
28+
defmacro chunk_by(callback, f // nil) do
2929
quote do
3030
fn
3131
entry, acc(h, { buffer, value }, t) ->

lib/elixir/test/elixir/enum_test.exs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -354,21 +354,21 @@ defmodule EnumTest.List do
354354
end
355355
end
356356

357-
test :chunks do
358-
assert Enum.chunks([1, 2, 3, 4, 5], 2) == [[1, 2], [3, 4]]
359-
assert Enum.chunks([1, 2, 3, 4, 5], 2, 2, [6]) == [[1, 2], [3, 4], [5, 6]]
360-
assert Enum.chunks([1, 2, 3, 4, 5, 6], 3, 2) == [[1, 2, 3], [3, 4, 5]]
361-
assert Enum.chunks([1, 2, 3, 4, 5, 6], 2, 3) == [[1, 2], [4, 5]]
362-
assert Enum.chunks([1, 2, 3, 4, 5, 6], 3, 2, []) == [[1, 2, 3], [3, 4, 5], [5, 6]]
363-
assert Enum.chunks([1, 2, 3, 4, 5, 6], 3, 3, []) == [[1, 2, 3], [4, 5, 6]]
364-
assert Enum.chunks([1, 2, 3, 4, 5], 4, 4, 6..10) == [[1, 2, 3, 4], [5, 6, 7, 8]]
357+
test :chunk do
358+
assert Enum.chunk([1, 2, 3, 4, 5], 2) == [[1, 2], [3, 4]]
359+
assert Enum.chunk([1, 2, 3, 4, 5], 2, 2, [6]) == [[1, 2], [3, 4], [5, 6]]
360+
assert Enum.chunk([1, 2, 3, 4, 5, 6], 3, 2) == [[1, 2, 3], [3, 4, 5]]
361+
assert Enum.chunk([1, 2, 3, 4, 5, 6], 2, 3) == [[1, 2], [4, 5]]
362+
assert Enum.chunk([1, 2, 3, 4, 5, 6], 3, 2, []) == [[1, 2, 3], [3, 4, 5], [5, 6]]
363+
assert Enum.chunk([1, 2, 3, 4, 5, 6], 3, 3, []) == [[1, 2, 3], [4, 5, 6]]
364+
assert Enum.chunk([1, 2, 3, 4, 5], 4, 4, 6..10) == [[1, 2, 3, 4], [5, 6, 7, 8]]
365365
end
366366

367-
test :chunks_by do
368-
assert Enum.chunks_by([1, 2, 2, 3, 4, 4, 6, 7, 7], &(rem(&1, 2) == 1)) == [[1], [2, 2], [3], [4, 4, 6], [7, 7]]
369-
assert Enum.chunks_by([1, 2, 3, 4], fn _ -> true end) == [[1, 2, 3, 4]]
370-
assert Enum.chunks_by([], fn _ -> true end) == []
371-
assert Enum.chunks_by([1], fn _ -> true end) == [[1]]
367+
test :chunk_by do
368+
assert Enum.chunk_by([1, 2, 2, 3, 4, 4, 6, 7, 7], &(rem(&1, 2) == 1)) == [[1], [2, 2], [3], [4, 4, 6], [7, 7]]
369+
assert Enum.chunk_by([1, 2, 3, 4], fn _ -> true end) == [[1, 2, 3, 4]]
370+
assert Enum.chunk_by([], fn _ -> true end) == []
371+
assert Enum.chunk_by([1], fn _ -> true end) == [[1]]
372372
end
373373

374374
test :slice do
@@ -767,18 +767,18 @@ defmodule EnumTest.Range do
767767
assert Enum.min_by(1..3, fn(x) -> :math.pow(-2, x) end) == 3
768768
end
769769

770-
test :chunks do
771-
assert Enum.chunks(1..5, 2) == [[1, 2], [3, 4]]
772-
assert Enum.chunks(1..5, 2, 2, [6]) == [[1, 2], [3, 4], [5, 6]]
773-
assert Enum.chunks(1..6, 3, 2) == [[1, 2, 3], [3, 4, 5]]
774-
assert Enum.chunks(1..6, 2, 3) == [[1, 2], [4, 5]]
775-
assert Enum.chunks(1..6, 3, 2, []) == [[1, 2, 3], [3, 4, 5], [5, 6]]
776-
assert Enum.chunks(1..5, 4, 4, 6..10) == [[1, 2, 3, 4], [5, 6, 7, 8]]
770+
test :chunk do
771+
assert Enum.chunk(1..5, 2) == [[1, 2], [3, 4]]
772+
assert Enum.chunk(1..5, 2, 2, [6]) == [[1, 2], [3, 4], [5, 6]]
773+
assert Enum.chunk(1..6, 3, 2) == [[1, 2, 3], [3, 4, 5]]
774+
assert Enum.chunk(1..6, 2, 3) == [[1, 2], [4, 5]]
775+
assert Enum.chunk(1..6, 3, 2, []) == [[1, 2, 3], [3, 4, 5], [5, 6]]
776+
assert Enum.chunk(1..5, 4, 4, 6..10) == [[1, 2, 3, 4], [5, 6, 7, 8]]
777777
end
778778

779-
test :chunks_by do
780-
assert Enum.chunks_by(1..4, fn _ -> true end) == [[1, 2, 3, 4]]
781-
assert Enum.chunks_by(1..4, &(rem(&1, 2) == 1)) == [[1], [2], [3], [4]]
779+
test :chunk_by do
780+
assert Enum.chunk_by(1..4, fn _ -> true end) == [[1, 2, 3, 4]]
781+
assert Enum.chunk_by(1..4, &(rem(&1, 2) == 1)) == [[1], [2], [3], [4]]
782782
end
783783

784784
test :slice do

lib/elixir/test/elixir/stream_test.exs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,31 +39,31 @@ defmodule StreamTest do
3939
assert Process.get(:after)
4040
end
4141

42-
test "chunks" do
43-
assert Stream.chunks([1, 2, 3, 4, 5], 2) |> Enum.to_list ==
42+
test "chunk" do
43+
assert Stream.chunk([1, 2, 3, 4, 5], 2) |> Enum.to_list ==
4444
[[1, 2], [3, 4]]
45-
assert Stream.chunks([1, 2, 3, 4, 5], 2, 2, [6]) |> Enum.to_list ==
45+
assert Stream.chunk([1, 2, 3, 4, 5], 2, 2, [6]) |> Enum.to_list ==
4646
[[1, 2], [3, 4], [5, 6]]
47-
assert Stream.chunks([1, 2, 3, 4, 5, 6], 3, 2) |> Enum.to_list ==
47+
assert Stream.chunk([1, 2, 3, 4, 5, 6], 3, 2) |> Enum.to_list ==
4848
[[1, 2, 3], [3, 4, 5]]
49-
assert Stream.chunks([1, 2, 3, 4, 5, 6], 2, 3) |> Enum.to_list ==
49+
assert Stream.chunk([1, 2, 3, 4, 5, 6], 2, 3) |> Enum.to_list ==
5050
[[1, 2], [4, 5]]
51-
assert Stream.chunks([1, 2, 3, 4, 5, 6], 3, 2, []) |> Enum.to_list ==
51+
assert Stream.chunk([1, 2, 3, 4, 5, 6], 3, 2, []) |> Enum.to_list ==
5252
[[1, 2, 3], [3, 4, 5], [5, 6]]
53-
assert Stream.chunks([1, 2, 3, 4, 5, 6], 3, 3, []) |> Enum.to_list ==
53+
assert Stream.chunk([1, 2, 3, 4, 5, 6], 3, 3, []) |> Enum.to_list ==
5454
[[1, 2, 3], [4, 5, 6]]
55-
assert Stream.chunks([1, 2, 3, 4, 5], 4, 4, 6..10) |> Enum.to_list ==
55+
assert Stream.chunk([1, 2, 3, 4, 5], 4, 4, 6..10) |> Enum.to_list ==
5656
[[1, 2, 3, 4], [5, 6, 7, 8]]
5757
end
5858

59-
test "chunks is zippable" do
60-
stream = Stream.chunks([1, 2, 3, 4, 5, 6], 3, 2, [])
59+
test "chunk is zippable" do
60+
stream = Stream.chunk([1, 2, 3, 4, 5, 6], 3, 2, [])
6161
list = Enum.to_list(stream)
6262
assert Enum.zip(list, list) == Enum.zip(stream, stream)
6363
end
6464

65-
test "chunks_by" do
66-
stream = Stream.chunks_by([1, 2, 2, 3, 4, 4, 6, 7, 7], &(rem(&1, 2) == 1))
65+
test "chunk_by" do
66+
stream = Stream.chunk_by([1, 2, 2, 3, 4, 4, 6, 7, 7], &(rem(&1, 2) == 1))
6767

6868
assert is_lazy(stream)
6969
assert Enum.to_list(stream) ==
@@ -72,8 +72,8 @@ defmodule StreamTest do
7272
[[1], [2, 2], [3]]
7373
end
7474

75-
test "chunks_by is zippable" do
76-
stream = Stream.chunks_by([1, 2, 2, 3], &(rem(&1, 2) == 1))
75+
test "chunk_by is zippable" do
76+
stream = Stream.chunk_by([1, 2, 2, 3], &(rem(&1, 2) == 1))
7777
list = Enum.to_list(stream)
7878
assert Enum.zip(list, list) == Enum.zip(stream, stream)
7979
end

0 commit comments

Comments
 (0)