# Constant function

**URL:** <https://discourse.julialang.org/t/constant-function/1249>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 2, 2017, 3:29pm UTC](https://discourse.julialang.org/t/constant-function/1249 "2017-01-02T15:29:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 2, 2017, 3:29pm UTC](https://discourse.julialang.org/t/constant-function/1249/1 "2017-01-02T15:29:21Z")

</div>

is something like

```julia
constantly(x) = (_...) -> x

```

defined anywhere in the standard library, or a package?

Note that I am not asking for it to be added, just would like to use the standard name if it exists. Most functional languages have something like this (eg [Haskell](https://www.haskell.org/hoogle/?hoogle=const), [Common Lisp](http://clhs.lisp.se/Body/f_cons_1.htm)), but I could not find it in the docs.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [January 2, 2017, 4:18pm UTC](https://discourse.julialang.org/t/constant-function/1249/2 "2017-01-02T16:18:18Z")

</div>

Are you looking for the `identity` function?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 2, 2017, 4:23pm UTC](https://discourse.julialang.org/t/constant-function/1249/3 "2017-01-02T16:23:01Z")

</div>

Nope, since `identity(x) = x`. I want a function that returns a function, which returns the given constant. Like the definition above.

Not a big deal if it does not exist in `Base`, it is trivial to create, and anonymous functions only became fast in `v0.5`, so I guess someone will add it soon.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 2, 2017, 4:26pm UTC](https://discourse.julialang.org/t/constant-function/1249/4 "2017-01-02T16:26:04Z")

</div>

Why would something like this need to be in Base? Isn’t it just as easy to type `x -> 3` or whatever when you need it?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 2, 2017, 4:32pm UTC](https://discourse.julialang.org/t/constant-function/1249/5 "2017-01-02T16:32:07Z")

</div>

@stevengj: same argument applies to `identity`. I guess most languages add these constructs not because defining them is difficult, but to standardize code and make it more readable.

However, I am not arguing that it should be added to `Base`, but if it was in there, I would not argue for its removal either.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 2, 2017, 4:37pm UTC](https://discourse.julialang.org/t/constant-function/1249/6 "2017-01-02T16:37:25Z")

</div>

> [@Tamas\_Papp](#):
>
> @stevengj: same argument applies to identity.

`identity` is actually used in `Base` in a bunch of places, and it turns out to be useful to overload on (e.g. in `broadcast`). The same doesn’t seem to apply to a higher-order function that generates `x -> 3` etc (e.g. you wouldn’t be able to overload on the resulting functions, unless you created some kind of functor object instead of a `Function` per se).

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 2, 2017, 4:52pm UTC](https://discourse.julialang.org/t/constant-function/1249/7 "2017-01-02T16:52:33Z")

</div>

Good point. `f::typeof(identity)` looks like a neat trick, thanks! I never realized that dispatching on functions is a possibility.

---

<div class="post-metadata">

**Author:** ![fengyang.wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fengyang.wang/32/104_2.png) [@fengyang.wang](https://discourse.julialang.org/u/fengyang.wang)\
**Post date:** [January 3, 2017, 6:50am UTC](https://discourse.julialang.org/t/constant-function/1249/8 "2017-01-03T06:50:41Z")

</div>

There is a (perhaps only tangentially related) discussion and implementation of the K combinator on [stack overflow](http://stackoverflow.com/documentation/julia-lang/5758/combinators/20334/the-ski-combinator-system#t=201701030649307557204). I think the standard name for this combinator is `K`, though I don’t know any programming language that provides it with that name.
