marick
I confess I don’t use Function.identity because &(&1) comes to mind faster. However, there’s another function that seems commonly used in functional languages, which is a function that makes a function that always returns the same value:
def constantly(value) do
fn _ -> value end
end
Given there’s no shorter version than fn _ -> value end (is there?) would it be worth adding Function.constantly?
Trending in Discussions
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
New
Hey there,
It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
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
Quite interesting article Google brought me. Didn’t find any mentions about it here.
What do you think in general? Would you use togethe...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
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
Hi there! :wave:
@frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
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
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Just published claude-code-elixir, a plugin marketplace for Claude Code with Elixir support. These are the plugins I’ve been using for my...
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
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 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
marciol
I think that the best place to suggest this kind of thing is on Elixir Lang Core Mailing List
@ericmj @fishcakez @fertapric @lexmag @whatyouhide and @josevalim can confirm.
benwilson512
I think this proposal is tricky because, unlike
identity/1, there isn’t an obvious arity that your return function should have. Should it be:Each of those has real scenarios where they’d be useful.
The other issue is that these two things do not have the same semantics:
Function.constantly(code)andfn -> code endwill behave differently depending on whatcodeis. IfcodeisDateTime.utc_now()thenFunction.constantlywill return the same date time over and over, butfn -> DateTime.utc_now() endwill return a new datetime each time it is invoked.dorgan
If we take maths as our source of truth(since most constructs of this kind come from there), then I’d argue the arity should be 1, based on notions from lambda calculus.
If we consider the expression λx.λy.x, giving it an x will result in an abstraction that, no matter what y we feed it, it will always return x. The resulting abstraction would be λx.y which is the constant function x↦y. The abstraction that produces such constant functions is the constant combinator K = λx.λy.x.
The identity combinator I is already incarnated in
Function.identity, if the K combinator is implemented, we would only need to add the S combinator λfgx.fx(gx):And this would complete the three combinators needed for SKI combinator calculus.
The other argument for K’s returned function to have an arity of 1, is that a lot of this work involves giving one argument at a time to partially apply those functions, and it just makes no sense to have more arguments. Having zero arguments also goes against the definition of x↦y.
Another observation is that such constant function is the return value, what’s being proposed is the combinator that produces it.
Function.constantly(code)andfn -> code endwill behave differently because the former is an invocation to the combinator, and the latter is the produced function. Moreover, if you give a function toFunction.constantly/1, then expect the constant function to return the same function, not to evaluate it.I don’t know what was the motivation behind the addition of
Function.identity/1, but I’m not sure if it’s worth it to implement these combinators in elixir itself, or it’s best to leave it for userland code. I think that once you add one you’ll be tempted to add the others, and I don’t know if that’s the goal of theFunctionmodule.I also understand that Elixir is a pragmatic language and academic purity is not it’s goal, but if we are going to implement these abstractions then we need to pick something that’s already been generalized and well understood, otherwise we would end up discussing which arity the function should have considering x, y and z practical examples, and for such cases I believe ad-hoc functions are the best.
marick
Other languages have this function. I think it’s always arity 1, with the argument being ignored.
al2o3cr
Nitpick:
Function.constantly(value)is also not a shorter version, since it’s literally nine characters longer.OvermindDL1
“Other languages” have this function also seem to be the same languages where every function is arity 1 (what you’d think of as an arity 3 function is just an arity 1 that returns a function that is arity 1 that returns a function that is arity 1 that return the result), and no language on the BEAM can work quite like that without a huge amount of preprocessing to ‘pack’ arity’s together if you want to work with the rest of the beam.
marick
Clojure has a
constantly, and it has functions that aren’t just syntactic sugar for arity-1 functions.