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 having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication):
toke...
New
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
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
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










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