Last active
June 16, 2017 19:26
-
-
Save rodocite/99d470890658a1b1253830204afd0223 to your computer and use it in GitHub Desktop.
This is why we need the pipe operator in every language.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
list = [1, [[2], 3]] | |
list | |
|> List.flatten | |
|> Enum.reverse | |
|> Enum.map(fn(n) -> n * n end) | |
|> IO.inspect | |
IO.inspect(Enum.map(Enum.reverse(List.flatten(list)), fn(n) -> n * n end)) |
Ruby - If it was not an Enumerable, the chaining would be broken. Elixir pipe allows any value to be piped.
Ramda - pipe has arity limitations. First function can have any arity. Subsequent functions can only be unary-- meaning you have to write adapter functions or use R.curry for already existing functions you could have just piped in. I don't think your example will work. http://ramdajs.com/docs/#pipe
Less trivial example of a pipe:
"path/to/some/file"
|> File.stream!
|> Flow.from_enumerable()
|> Flow.flat_map(&String.split(&1, " "))
|> Flow.partition()
|> Flow.reduce(fn -> %{} end, fn word, acc ->
Map.update(acc, word, 1, & &1 + 1)
end)
|> Enum.to_list()
That counts the occurrence of each word in a several gig file in a few seconds, distributed across all processor cores. Stream the file, convert to enumerable, flat map it, partition it, reduce it, dump to list. Reads like a list.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Enumerable
inheriting object.to_s
, I just thought that wasIo.console
did. Here's a more equivalent: