15
$\begingroup$

Today I came across the function Identity[x] in Mathematica (documentation). The example given in the documentation uses it in Map as follows

TableForm[Composition[Through, {Identity, Sqrt}] /@ {0, 1.0, 2.0, 3.0, 4.0}].

For me, I have always written such maps as

TableForm[Composition[Through, {#&, Sqrt}] /@ {0, 1.0, 2.0, 3.0, 4.0}]

Is there any real advantage (performance, memory usage, etc ..) for using Identity over a lambda function? Are there any examples where one is forced to actually use Identity? I am not sure I get the point of having this Identity function.

$\endgroup$
  • $\begingroup$ One good reason for having a built-in, named Identity function is that it can be documented in same way as all the other named functions. $\endgroup$ – m_goldberg Aug 29 '15 at 13:27
14
$\begingroup$

There are subtle differences between #& and Identity.

If you pass more than one argument, Identity will complain and remain unevaluated, #& will just return the first argument.

Identity[x, y]
(* Identity::argx: Identity called with 2 arguments; 1 argument is expected. >> *)
(* Identity[x, y] *)

#&[x, y]
(* x *)

Also Identity is automatically simplified inside Composition and #& is not.

g @* Identity @* f
(* g @* f *)

g @* (#&) @* f
(* g @* (#1&) @* f *)
$\endgroup$
  • $\begingroup$ What does this @* construct do? My Mathematica 9 doesn't understand it. $\endgroup$ – Ruslan Aug 29 '15 at 10:02
  • 1
    $\begingroup$ @Ruslan @* is short form for Composition. $\endgroup$ – dionys Aug 29 '15 at 10:33
  • 1
    $\begingroup$ @Ruslan. @* was introduced in V10 $\endgroup$ – m_goldberg Aug 29 '15 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.