Khubaib Ahmad
Khubaib Ahmad

Reputation: 49

How to use capture operator in Elixir to capture a tuple from a list?

i am unable to understand that how to use capture operator to capture a tuple. Here is my code with function.

MyList.map([{"person 1",27},{"person 2",20}], fn({name,_}) -> name end)                 
// ["person 1","person 2"]

can anyone help me please on how to do it via capture operator. so far i have tried this but is no use.

MyList.map([{"person 1",27},{"person 2",20}], & &1}) // returns same List with same tuple
MyList.map([{"person 1",27},{"person 2",20}], &{&1,&2} &1) // error

Upvotes: 2

Views: 647

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121000

This exact behaviour is impossible, one cannot reach to the inner state of captured terms, &1, &2 etc capture the whole parameter. Use Kernel.elem/2:

MyList.map([{"person 1",27},{"person 2",20}], & elem(&1, 0))

Upvotes: 4

Related Questions