Martinos
Martinos

Reputation: 2226

Is there an identity function in Elixir?

Is there an identity function already defined Elixir ?

Something like:

identity = fn a -> a end

Upvotes: 35

Views: 5027

Answers (2)

Cody Poll
Cody Poll

Reputation: 8280

@focused is correct - 1.10-dev has added the identity function.

Documentation

Old Answer: No such function has been predefined (at least that I'm aware of). It can trivially be written as you've done in your question, or more succinctly as &(&1).

Upvotes: 40

focused
focused

Reputation: 364

Function.identity/1 has been added to Elixir v1.10.0-dev recently:

Commit

Usage example:

Enum.map([1, 2, 3, 4], &Function.identity/1)

Upvotes: 13

Related Questions