# Is \`Function.constantly\` worth adding?

**URL:** <https://forum.elixirforum.com/t/is-function-constantly-worth-adding/39830>\
**Category:** Discussions\
**Tags:** kernel\
**Created:** [May 19, 2021, 11:25pm UTC](https://forum.elixirforum.com/t/is-function-constantly-worth-adding/39830 "2021-05-19T23:25:08Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![dorgan](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dorgan/32/22107_2.png) [@dorgan](https://forum.elixirforum.com/u/dorgan)\
**Post date:** [May 20, 2021, 2:44am UTC](https://forum.elixirforum.com/t/is-function-constantly-worth-adding/39830/4 "2021-05-20T02:44:46Z")

</div>

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)_:

```elixir
def substitution(f) do
  fn g ->
    fn x ->
      (f.(x)).(g.(x))
    end
  end
end

```

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)` and `fn -> code end` will behave differently because the former is an invocation to the combinator, and the latter is the produced function. Moreover, if you give a function to `Function.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 the `Function` module.  
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.

---

_[View the full topic](https://forum.elixirforum.com/t/is-function-constantly-worth-adding/39830)._
