ElixirCode icon indicating copy to clipboard operation
ElixirCode copied to clipboard

Deprecation warning for Elixir 1.9.1 - Enum.chunk/2 replace with Enum.chunk_every/3.

Open newmanicspree opened this issue 6 years ago • 0 comments

Build:

Elixir 1.9.1 (compiled with Erlang/OTP 20) Ubuntu 18.04

identicon.ex:

def build_grid(%Identicon.Image{hex: hex} = image) do
  grid
    |> Enum.chunk(3)
    |> Enum.map(&mirror_row/1)
    |> List.flatten
    |> Enum.with_index
end

Issues / Improvements:

  1. warning: Enum.chunk/2 is deprecated. Use Enum.chunk_every/2 instead.
  2. Also, remainder element is not discarded.
  3. Consider explicit use of anonymous function in the coursework (to connect with ecmascripters), then refactor to &mirror_row/1.

Possible solution:

Replace Enum.chunk(3) with Enum.chunk_every(3, 3, :discard)

def build_grid(%Identicon.Image{hex: hex} = image) do
  grid
    |> Enum.chunk_every(3, 3, :discard)
    |> Enum.map(fn(row) -> mirror_row(row))
    |> List.flatten
    |> Enum.with_index
end
Refactor (for learning purposes... ):
def build_grid(%Identicon.Image{hex: hex} = image) do
  grid
    |> Enum.chunk_every(3, 3, :discard)
    |> Enum.map(&mirror_row/1)
    |> List.flatten
    |> Enum.with_index
end

Nice primer course!

newmanicspree avatar Oct 03 '19 04:10 newmanicspree