Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an identity function in Elixir?

Tags:

elixir

Is there an identity function already defined Elixir ?

Something like:

identity = fn a -> a end

like image 267
Martinos Avatar asked Feb 25 '16 00:02

Martinos


People also ask

How do you define a function in Elixir?

An Elixir function is typically named, and is called a "named function". Functions are defined with the def keyword followed by a parameter list. A function also has a body containing the contents of the function. Like modules, the function body begins with the do keyword and ends with the end keyword.

What does T () mean in Elixir?

Basically it's a reference to “string” type which is being used by functions from Elixir's String module.


2 Answers

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

like image 191
Cody Poll Avatar answered Sep 18 '22 20:09

Cody Poll


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) 
like image 32
focused Avatar answered Sep 16 '22 20:09

focused