Please compare
coll |> Enum.map_reverse(&my_fun/1)
to
coll |> Enum.reduce([], fn ele, acc -> [fun(ele)|acc] end)
And also would Enum.map_reverse/2 not be even more optimized, although
performance was not my primary motivation.
--
Non datemi consigli che so sbagliare da solo
(Giuseppe Di Stefano)
On Sun, Mar 31, 2019 at 6:01 PM Andrea Leopardi <[email protected]>
wrote:
> You can do the same in an efficient way with Enum.reduce/3 and prepending
> to the accumulator:
>
> Enum.reduce(list, [], fn elem, acc ->
> [fun.(elem) | acc ]
> end)
>
> I think given this is small and straightforward, an addition of
> reverse_map to the standard library could be something that we can avoid.
>
> Andrea
>
> On Sun, 31 Mar 2019 at 17:58, Robert Dober <[email protected]> wrote:
>
>> Hi there
>>
>> I do this all the time, travering a reversed list and accumulating a
>> transformation into a new list, unreversed now.
>>
>> Is this not a very frequent FP pattern.
>>
>> I would quite happy to use `Enum.reverse_map` which would be more
>> efficent than my eternal internal remimplementation:
>>
>> ```elixir
>>
>> defp map_reverse(collection, fun)
>> do
>>
>> _map_reverse(collection, fun,
>> [])
>>
>>
>> end
>>
>>
>>
>> defp _map_reverse(collection, fun,
>> result)
>>
>> defp _map_reverse([], _, result)
>> do
>>
>>
>> result
>>
>>
>> end
>>
>> defp _map_reverse([head|rest], fun, result)
>> do
>>
>> _map_reverse(rest, fun,
>> [fun.(head)|result])
>>
>> end
>> ```
>>
>> Any thoughts?
>>
>> KR
>> Robert
>>
>> --
>> 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/a4fe903e-9661-448e-ba80-0b8cfc4f8bff%40googlegroups.com
>> <https://groups.google.com/d/msgid/elixir-lang-core/a4fe903e-9661-448e-ba80-0b8cfc4f8bff%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
> --
>
> Andrea Leopardi
> [email protected]
>
> --
> 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/CAM9Rf%2BLpJYUfHg901KY3EF8MwrX3pXHEacrdRywM%3D-qkzw77RQ%40mail.gmail.com
> <https://groups.google.com/d/msgid/elixir-lang-core/CAM9Rf%2BLpJYUfHg901KY3EF8MwrX3pXHEacrdRywM%3D-qkzw77RQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>
--
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/CA%2Bvts6t8J36kgR-xUFKhEaEbUEoYB-8x%2BAmVArRt9%2BaFMnQxjA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.