Constant function

is something like

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, Common Lisp), but I could not find it in the docs.

Are you looking for the identity function?

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.

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?

@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.

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).

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

There is a (perhaps only tangentially related) discussion and implementation of the K combinator on stack overflow. I think the standard name for this combinator is K, though I don’t know any programming language that provides it with that name.