Thanks, I also needed zip_reduce a couple days ago while working on Nx, so
zip_reduce is appreciated. The cool thing is that we can also implement
both zip_with and zip on top of zip_reduce, so it shouldn’t make the code
more complex either.

A PR is welcome!



On Sat, Mar 20, 2021 at 15:10 [email protected] <[email protected]> wrote:

> Here are some use cases that having a reducer would help with:
>
> ```elixir
> Enum.zip([:a, :b, :c], [1, 2, 3], %{}, fn k, val, acc -> Map.put(acc, k,
> val)  end)
> # %{a: 1, b: 2, c: 3}
>
> def zip_into_map(left, right) do
>   Enum.zip(left, right, %{}, fn k, val, acc -> Map.put(acc, k, val)  end)
> end
>
> csv_headers = ["id", "date", "whatever"]
>
> Enum.map(csv_rows, fn row ->
>   zip_into_map(csv_headers, row)
> end)
>
> dot_product =
>   Enum.zip([1, 2], [3, 4], [], fn l, r, acc -> [ l * r | acc] end)
>   |> Enum.reverse
>
> def zip_merge_values(left, right) do
>   Enum.zip(left, right, %{}, fn
>     {key, left_val}, {key, right_val}, acc ->
>       Map.put(acc, key, left_val ++ right_val)
>
>     {left_key, left_val}, {right_key, right_val}, acc ->
>       Map.merge(acc, %{left_key => left_val, right_key => right_val})
>   end)
> end
>
> zip_merge_values(%{a: [1], c: [5]}, %{a: [2], b: [7])
> # %{a: [1, 2], b: [7] c: [5]}
>
> Enum.zip_with(["a", "key", "list"], Stream.repeatedly(fn -> 10 end), %{},
> fn k, v, acc ->
>   Map.merge(acc, {k, v})
> end)
>
> # => %{"a" => 10, "key" => 10, "list" => 10}
> ```
>
> Though I think if we are really going for it if you had a Zippable
> protocol a la Eumerable and defined Zippable.reduce_while you'd get a lot
> of functions for free I suspect.
>
> Best
>
> Adam
>
>
> On Wednesday, February 3, 2021 at 5:59:34 AM UTC José Valim wrote:
>
>> I think zip_with is still valuable as is because a zip_with without an
>> accumulator can be streamable (i.e. in the Stream module) but a zip_with
>> with an accumulator cannot. We may be looking at a zip_reduce in the future
>> though, given use cases are provided. :)
>>
>> On Tue, Feb 2, 2021 at 11:29 PM [email protected] <[email protected]>
>> wrote:
>>
>>> How embarrasing I typo'd the subject. It should read Should zip /
>>> zip_with have a reducer?
>>>
>>> Best
>>>
>>> Adam
>>>
>>> On Tuesday, February 2, 2021 at 10:27:49 PM UTC [email protected]
>>> wrote:
>>>
>>>> Hello!
>>>>
>>>> I was thinking about zip_with/3 et al. (don't judge me I'm in a
>>>> lockdown).
>>>>
>>>> Currently they work by returning a list, but would it not make more
>>>> sense for it to take an accumulator?
>>>>
>>>> ```elixir
>>>> Enum.zip_with([1, 2], [3, 4], %{}, fn left, right, acc -> Map.put(acc,
>>>> left, right) end)
>>>> %{ 1 => 3, 2 => 4 }
>>>>
>>>> Enum.zip_with([1, 2], [3, 4], [], fn left, right, acc -> [{left, right}
>>>> | acc] end)
>>>> [{1, 3}, {2, 4}]
>>>> ```
>>>> If so... I guess it also opens to door for a zip_while.
>>>>
>>>> Am I mad? Has science gone too far?
>>>>
>>>> Best
>>>>
>>>> Adz
>>>>
>>>>
>>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "elixir-lang-core" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/elixir-lang-core/468649a3-7e6f-4214-99d8-6e8ef5e6d72an%40googlegroups.com
>>> <https://groups.google.com/d/msgid/elixir-lang-core/468649a3-7e6f-4214-99d8-6e8ef5e6d72an%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>> --
> You received this message because you are subscribed to the Google Groups
> "elixir-lang-core" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/elixir-lang-core/47214770-475d-4499-a13e-3d7077d4e614n%40googlegroups.com
> <https://groups.google.com/d/msgid/elixir-lang-core/47214770-475d-4499-a13e-3d7077d4e614n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4Km4hiN1CB%3DfTKAsnyo1jf_tT%3D1r8pmx%2BQqw_mPVsip2A%40mail.gmail.com.

Reply via email to