?? *`*IndexBy*
Following the usual spelunking
steps
ClearAttrributes[IndexByClearAttributes[IndexBy, {Protected, ReadProtected}]
?? IndexBy
reveals the code that defines IndexBy
. Simplifying (and ignoring argument type-checks) it is something like:
indexBy[f_][expr_]:=Association[(f[#]->#)&/@If[AssociationQ[expr],Values[expr],expr]]
indexBy[foo][Range[5]]
(* <|foo[1]->1,foo[2]->2,foo[3]->3,foo[4]->4,foo[5]->5|> *)
indexBy[foo][<|a -> x, b -> y, c -> z|>]
(* <|foo[x]->x,foo[y]->y,foo[z]->z|> *)