Hi Tallak,
I have implemented a new version taking into consideration your
advices. It really helped me with the decision. I ended up with
Enum.pad/3 and Enum.pad_zip/3 & /4
Enum.pad_zip leverages all the work to Enum.pad,
and once the two enumerables are padded, it is just a matter of calling
Enum.zip/2 for a regular zipping operation.
So the padding in Enum.pad and Enum.pad_zip can only an enumerable or
a function with arity of 1.
No options are required, just good old plain arguments.
Here are some examples from the docs:
## Enum.pad/3
# Enum.pad(enum, size, pad)
# default padding
iex> Enum.pad([1, 2, 3], 5)
[1, 2, 3, nil, nil]
# not enought padding
iex> Enum.pad([1, 2, 3], 5, [nil])
[1, 2, 3, nil]
# functions
iex> Enum.pad([1, 2, 3], 5, &(&1+10))
[1, 2, 3, 13, 23]
# ranges
iex> Enum.pad([1, 2, 3], 6, 50..100)
[1, 2, 3, 50, 51, 52]
# Stream.cycle
iex> Enum.pad([1, 2, 3], 10, Stream.cycle([0, 1]))
[1, 2, 3, 0, 1, 0, 1, 0, 1, 0]
## Enum.pad_zip/3 and Enum.pad_zip/4
# Enum.pad_zip(enum1, enum2, pad1, pad2)
# default pad_zip
iex> Enum.pad_zip(1..5, [:a])
[{1, :a}, {2, nil}, {3, nil}, {4, nil}, {5, nil}]
# Stream.cycle as two differnt paddings
iex> Enum.pad_zip(1..5, [:a, :b, :c], Stream.cycle([0]),
...> Stream.cycle([:z]))
[{1, :a}, {2, :b}, {3, :c}, {4, :z}, {5, :z}]
# a bit more complex function as padding
iex> Enum.pad_zip([:a, :aN], 1..4, &("#{&1}" |> String.reverse
...> |> String.duplicate(2) |> String.to_atom))
[a: 1, aN: 2, NaNa: 3, aNaNaNaN: 4]
You can find the sources here:
https://github.com/eksperimental/experimental_elixir/blob/master/lib/enum_pad.ex
https://github.com/eksperimental/experimental_elixir/blob/master/test/enum_pad_test.exs
Regards,
- eksperimental
On Sun, 20 Sep 2015 23:39:33 -0700 (PDT)
Tallak Tveide <
tal...@gmail.com> wrote:
>
> I am thinking `zip/3` or `pad_zip` rather than `zip_pad`, because you
> are first padding then zipping.
>
> The `pad` parameter ofum.chunk` is an enumerable, I think this