このページはまだ翻訳されていないため、英語で表示されます。 翻訳に参加する

identity

Returns its argument unchanged.

T identity<T>(T a) => a;

Lecture

identity is the simplest function in the library, and one of the most useful: it takes a value and hands it straight back. On its own that looks pointless. Its value shows up whenever an API expects a function but you don't actually want to transform anything — a default key function for groupBy/sortBy, a placeholder branch in a dispatch table, or the "no-op" arm of a conditional pipeline.

Because identity is generic (T identity<T>(T a)), it slots into any unary-function position without you writing (x) => x yourself. It has no async variant and no chain form — it's a plain value-in, value-out function you pass around.

Demo 1 · Basics

Used directly, and as the key function for a grouping operation where "group by the value itself" is exactly what you want:

Demo 2 · As the "do nothing" branch

A dispatch table of string transforms, where one entry deliberately does nothing — identity fills that slot cleanly instead of a hand-written no-op lambda:

Try it yourself

Exercise: sortBy takes a key function. Use identity as that key to sort this list of words by their natural (string) order.

Related: always — a constant instead of a pass-through · tap — pass through, but run a side effect first · cases — dispatch table with predicates · sortBy — a common home for a key function