[Proposal] Add zip_by/4 to the Enum module

39 views
Skip to first unread message

florian....@gmail.com

unread,
Dec 14, 2018, 7:19:02 AM12/14/18
to elixir-lang-core
Hi,

after having used this functionality in several different projects I like to propose it to be included in Elixir:

Enum.zip_by/4 is similar to Enum.zip/2 but takes a function which determines the identity of elements (most often through some kind of identifier). That means elements can end up having no matching element in the other list.

It is very handy when doing merges or updates of any kind. I mostly use it as a preparation step to make on-by-one decisions of how a merge is handled.

Here is what I put together so far:
https://github.com/elixir-lang/elixir/compare/master...odo:zip_by

There are some things that I think could be improved where I like to have some input:

- The code seems a bit bulky, compared to the other functions in the Enum module (comments could be removed)
- I don't really like the name of the do_zip_by/3 recursive function
- Both enumerables have to traversed several times (mapping, sorting, matching)

Let me know what you think.

Thanks,
Florian

Allen Madsen

unread,
Dec 14, 2018, 9:15:58 AM12/14/18
to elixir-l...@googlegroups.com
I feel like this a bit too nitch for Enum. I'll also say, I was surprised the implementation sorts the two lists. I think what you've implemented might better be called something like pair_by. I would have expected zip_by to allow you to control how the zipped items are combined in the resulting list.

[
  [1, 2],
  ~w(one two)
|> Enum.zip_by(fn {num, word} -> 
  "#{num} is #{word}" 
end)
#=> ["1 is one", "2 is two"]


However, this could just as easily be handled by:

[
  [1, 2],
  ~w(one two)
|> Enum.zip()
|> Enum.map(fn {num, word} -> 
  "#{num} is #{word}" 
end)
#=> ["1 is one", "2 is two"]

--
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 elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/e4bfde94-a39f-402c-9487-205dfe59acbf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Florian Odronitz

unread,
Dec 14, 2018, 9:31:29 AM12/14/18
to elixir-l...@googlegroups.com
That’s correct, pair_by would be a better name since it forms pairs by the return value of the match function.

If it’s too niche for Enum, do you think it fits another module or would it better be its own library?
> To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAK-y3Cu7BEExYRcMSEP1GSqUfg5DCKiZjaf6eY5AH6qpOQgOaA%40mail.gmail.com.

Allen Madsen

unread,
Dec 14, 2018, 9:40:42 AM12/14/18
to elixir-l...@googlegroups.com
I think it's better as a library.

Also, wow, did I butcher the spelling of niche.

Florian Odronitz

unread,
Dec 14, 2018, 10:43:42 AM12/14/18
to elixir-l...@googlegroups.com
> I think it's better as a library.
Makes sense
>
> Also, wow, did I butcher the spelling of niche.
Just kind of a niche way of spelling 8)

Have a nice weekend!
Florian
> To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAK-y3CtuDp3QydTsppHNjatq97sWzn8MjOSdyYg2xErY0cEUxQ%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages