Skip to content

Latest commit

 

History

History
26 lines (18 loc) · 416 Bytes

20170302_reduce_by_operator.md

File metadata and controls

26 lines (18 loc) · 416 Bytes

Elixir - Reduce by operator

I already knew that + is a shortcut to Kernel.+/2:

iex(1)> 1 + 2
3

iex(2)> Kernel.+(1, 2)
3

But I didn't know that you can skip the Kernel-part when referencing the function with the &-notiation.

This makes reducing by an operator very nice:

iex(3)> &Kernel.+/2
&:erlang.+/2

iex(4)> &+/2
&:erlang.+/2

iex(5)> [1, 2, 3] |> Enum.reduce(&+/2)
6