How capture operators work

Hello one and all,

can someone explain this line of code.

tbl = [%{"insurance_verification" => 1}, %{"modifications_review" => 1}]
key = "insurance_verification"
Enum.find(tbl, & &1[key])
%{"insurance_verification" => 1}

it works, but I don’t understand how it works.
I have read on the capture operator but how it’s being used here escapes me.

1 Like

& &1[key] is syntatic sugar for fn x -> x[key] end


thanks @eksperimental that’s the piece i did not understand… great

one thing though why the extra & before the function, is that an and function?

1 Like

The first & is the capture. &(...) is fn ... end
The second & together with the number 1 tell it where the first argument to the function is used.

1 Like

@lc0815 here are the docs:

1 Like

& &1[key] can be read like &(&1[key]) but the formatter will remove the parens


great @derek-zhou coming from a C# javascript world, the first & to me meant the AND operator, that’s why it was confusing… thanks

ah, now it’s clear… great

1 Like