stefanluptak
Good day everyone!
I am using Enum.reduce/3 (Enum — Elixir v1.9.0-rc.0) function quite often and I am regularly wondering why are the arguments for reducing function x, acc instead of acc, x.
Almost all functions in Elixir are in the form of Module.function(subject_of_change, change_argument). For example MapSet.put/2 or List.delete/2. Thanks to this, we can write code like:
changeset
|> cast(params, [ ... ])
|> validate_required([ ... ])
|> unique_constraint( ... )
But then, when I want to do something like this:
Enum.reduce([1,2,3,4], MapSet.new, &MapSet.put(&2, &1))
I need to use the &2, &1 form which is a bit unhandy. I think this form would be a lot nicer:
Enum.reduce([1,2,3,4], MapSet.new, &MapSet.put/2)
Is there some explanation for this? I am really curious. ![]()
Thanks a lot.
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
michalmuskala
I always thought that it follows the order of the arguments to
Enum.reduceitself - first the collection, then the accumulator, so in the reducer you get the element first and the accumulator second.dimitarvp
Probably to give you a chance to utilise tail-call recursion in the passed closure function, which only works if the accumulator is the last parameter (unless I am horribly misremembering this).
stefanluptak
Interesting. This would make sense. Thanks for the info.
alco
That is incorrect.
A simple implementation of
reduce()looks somewhat like this:Tail recursion is happening for invocations of
reduce()itself, the details of the functionfundon’t matter.peerreynders
I think you’re remembering that
foldlis easier to tco thanfoldr.Folding (reducing) emerged from “folding lists” - the most common list operation is cons-ing:
Note how the parameter order isn’t “unhandy”. One could argue that the unhandy-ness is a result of the preferred parameter order in Elixir due to pipelining. In a curried language the opposite order (the thing that changes should be last) would be preferred.
Note also that Elixir inherited the order from Erlang (1986):
and Erlang has no pipelining.
FYI:
Haskell (1990):
OCaml (1996):
Interestingly for “fold left” it’s
(acc, elem -> acc)while for “fold right” it’s(elem, acc -> acc)Hypothesis:
(acc, elem -> acc)for “fold left” accumulator is built from the list left-to-right (left associative)(elem, acc -> acc)for “fold right” accumulator is built from the list right-to-left (right associative)LISP (1958)
Again LISP used
(acc, elem -> acc)for reduce/fold left.So why would Erlang use
(elem, acc -> acc)?Hypothesis:
foldrbecause it preserves the order within a list - hence(elem, acc -> acc).From a performance standpoint
foldlis preferred for long lists but requires reversal if order is relevant. But for pragmatic reasons sticking to the same function type(elem, acc -> acc)makes a function usable for bothfoldrandfoldl.dimitarvp
Yeah, that was it and thanks for correction.
sasajuric
Don’t know the answer, but FWIW I also think that passing the acc first, collection element second would be more intuitive, and I frequently find myself flipping these two args in reduce.
keathley
Yup. Generally having the arguments in the current order is less ergonomic. But I suppose I’ll have to wait for elixir 2.0 before it’s made consistent.
sasajuric
My guess is that this change will never happen, but who knows
peerreynders
Many other languages agree with you (though initially I found the swap between
foldl(i.e.acc, elem -> acc) andfoldr(i.e.elem, acc -> acc) a bit confusing).for comprehensions side step the issue:
https://github.com/erlang/otp/blob/master/lib/stdlib/src/lists.erl#L1253-L1277