Proposal: Enum.find_map/4

69 views
Skip to first unread message

Thijs Klaver

unread,
May 27, 2016, 6:56:48 AM5/27/16
to elixir-lang-core
Hello,

In the same sense as Enum.filter_map/3, Enum.map_join/3, I propose Enum.find_map/4 (or Enum.find_transform/4).
Right now you would do something like

foo =
 
Enum.find list, nil, fn item -> some_check(item) end
 
|> if(foo, do: foo.bar)

With Enum.find_map you could do something like this

foo = Enum.find list, nil, fn item -> some_check(item) end, fn item -> Map.get item, :bar end

This way you don't have to check if Enum.find actually found something before transforming it.

Greg Vaughn

unread,
May 27, 2016, 9:46:40 AM5/27/16
to elixir-l...@googlegroups.com
I think you'll be able to achieve this cleanly with Kernel.get_in and Kernel.update_in with Elixir 1.3. I'm sure there's more info somewhere, but this is the extent of my knowledge at this time: https://twitter.com/elixirlang/status/735942800039088129

-Greg
> --
> 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/8d31a0c3-1902-480b-aeb7-38182726c884%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Alexei Sholik

unread,
May 27, 2016, 10:52:24 AM5/27/16
to elixir-lang-core
Greg is right. In Elixir 1.3 it'll become possible to access map fields when using get_in(). So the first code snippet from the original post may become this

foo = get_in(Enum.find(list, &some_check/1), [Access.field(:bar)])

However, given the fact that OP uses both field access and Map.get, it's not clear what the actual desired behaviour is. Maybe this would suffice

foo = get_in(Enum.find(list, &some_check/1), [:bar])

which already works in Elixir 1.2.x.

Reply all
Reply to author
Forward
0 new messages