marick

marick

Is `Function.constantly` worth adding?

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?

Most Liked

OvermindDL1

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.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

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:

fn -> value end
or
fn _ -> value end
or
fn _, _ -> value end
... etc

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) and fn -> code end will behave differently depending on what code is. If code is DateTime.utc_now() then Function.constantly will return the same date time over and over, but fn -> DateTime.utc_now() end will return a new datetime each time it is invoked.

al2o3cr

al2o3cr

Nitpick: Function.constantly(value) is also not a shorter version, since it’s literally nine characters longer.

Last Post!

marick

marick

Clojure has a constantly, and it has functions that aren’t just syntactic sugar for arity-1 functions.

Where Next?

Popular in Discussions Top

rower687
Hi all, I’ve been reading a lot about the “let it crash” term and how supervising processes and the whole messaging passing make an elixi...
New
lorenzo
Hey everone! I created a prototype for my app using Nodejs for the api. But the framework I chose wasnt great (in general theresnt any g...
New
AstonJ
If so I (and hopefully others!) might have some tips for you :slight_smile: But first, please say which area you’re finding most challen...
New
nburkley
AWS re:Invent is on at the moment with some interesting announcements. One new feature in particular is the Lambda Runtime API for AWS La...
New
Fl4m3Ph03n1x
Background A few days ago I was listening to The future of Elixir from Elixir Talks, with Dave Thomas (@pragdave ) and Brian Mitchell. I...
New
Fl4m3Ph03n1x
Background This question comes mainly from my ignorance. Today is Black Friday, one of my favorite days of the year to buy books. One boo...
New
marciol
Please, let me know if this kind of discussion already took place in another topic . Hi all, how do you consider if is better to build ...
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49134 226
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New

We're in Beta

About us Mission Statement