identity
Returns its argument unchanged.
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.