The pipe operator in Elixir
A honey harvest flows through map, filter, sum, and round without a nested call in sight.
# |> feeds the left result in as the first argument on the right.
harvest = [12.5, 8.0, 15.25, 6.75, 11.0]
total =
harvest
|> Enum.map(fn lbs -> lbs * 0.9 end)
|> Enum.filter(fn lbs -> lbs > 7 end)
|> Enum.sum()
|> Float.round(1)
IO.puts("market-ready honey: #{total} lbs")
IO.puts("raw off the frames: #{Float.round(Enum.sum(harvest), 1)} lbs")
How it works
|>feeds each result in as the next call's first argument.- The chain scales each frame, drops the light ones, and folds the rest into one total.
- The last line does one step without pipes, for the before-and-after.
Keywords and builtins used here
EnumFloatIOendfn
The run, in numbers
- Lines
- 12
- Characters to type
- 347
- Tokens
- 92
- Three-star pace
- 70 tpm
At the three-star pace of 70 tokens a minute, this run takes about 79 seconds.
Step 3 of 3 in Enum & pipes, step 15 of 27 in Language basics.