The existing way of doing this might allow beginners to develop a better 
intuition for the Elixir's capture operator.

For example consider this code for creating a lookup map from a list of 
maps:

```
lookup = Enum.map(map_list, & {&1.id, &2.name}) |> Enum.into(%{})
```

Knowing about `& &1` allowed me to then intuit that the `& {&1.id, 
&2.name}` bit of the code above might be possible.

On Tuesday, July 2, 2019 at 5:23:47 PM UTC+8, Alexey Nikitin wrote:
>
> Like Haskell's `id` or Clojure's `identity` or Ruby's `itself`
>
> It can be very useful sometimes. Instead of writing ugly `&(&1)` it would 
> much more attractive to write `&identity/1` or `&id\1`
> Moreover this function is already used as a default argument for some 
> higher-order function. For example `Enum.group_by(enumerable, key_fun, 
> value_fun \\ fn x -> x end)`
>
> Here are some examples
>
> ```
> #=> [1, 2, 3, true, 1234] 
>
> 'abcdaabccc' |> Enum.sort |> Enum.chunk_by(&identity/1) 
> #=> ['aaa', 'bb', 'cccc', 'd'] 
>
> [1, 1, 2, 3, 3, 1, 1, 5, 5] |> Enum.chunk_by(&identity/1) |> 
> Enum.map(&hd/1) 
> #=> [1, 2, 3, 1, 5] 
>
> Enum.group_by('abracadabra', &identity/1) 
> #=> %{97 => 'aaaaa', 98 => 'bb', 99 => 'c', 100 => 'd', 114 => 'rr'} 
>
> Enum.map([1, 2, 3, 4], &identity/1) 
> #=> [1, 2, 3, 4]
> [1,2,3,nil, true, false, 1234] |> Enum.filter(&identity/1) 
> ```
>

-- 
You received this message because you are subscribed to the Google Groups 
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elixir-lang-core/2abc17cf-d615-4d2f-82e3-c45f5513c61a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to