Last active
March 28, 2018 09:17
-
-
Save luislee818/6f13c7e9d9bf82fb473d6e3e4094a202 to your computer and use it in GitHub Desktop.
Illustrating Elixir's function invocation with pipe operators
This file contains hidden or 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
defmodule Funcs do | |
def greet(name) do | |
"Hola #{name}" | |
end | |
def test do | |
# functions as target of pipe | |
IO.puts test_1() | |
IO.puts test_2() | |
IO.puts test_3() | |
IO.puts test_4() | |
IO.puts test_5() | |
# functions as helper in higher order functions as target of pipe | |
IO.puts test_6() | |
IO.puts test_7() | |
IO.puts test_8() | |
IO.puts test_9() | |
end | |
def test_1 do | |
# functions that got piped into are in the format of invocation | |
# can omit parentheses if arity is 1 | |
"hello" | |
|> String.capitalize | |
# "Hello" | |
end | |
def test_2 do | |
# functions that got piped into are in the format of invocation | |
# can use parentheses if arity is 1 | |
"hello" | |
|> String.capitalize() | |
# "Hello" | |
end | |
def test_3 do | |
# functions that got piped into are in the format of invocation | |
# must use parentheses if arity is larger than 1 | |
"hello" | |
|> String.at(1) | |
# "e" | |
end | |
def test_4 do | |
# functions that got piped into are in the format of invocation | |
# can use custom defined named function | |
"John" | |
|> greet | |
# "Hola John" | |
end | |
def test_5 do | |
# functions that got piped into are in the format of invocation | |
# can use anonymous function, but looks strange | |
"John" | |
|> (&("Adios #{&1}")).() | |
# |> &("Adios #{&1}").() # this doesn't work | |
# "Adios John" | |
end | |
def test_6 do | |
# functions that got passed to higher order functions as target of pipe, should be in the format of function reference, not invocation | |
# can use & to capture named function, specify its name and arity | |
~w(uno dos tres) | |
|> Enum.map(&String.capitalize/1) | |
|> Enum.join(", ") | |
# "Uno, Dos, Tres" | |
end | |
def test_7 do | |
# functions that got passed to higher order functions as target of pipe, should be in the format of function reference, not invocation | |
# can use & to capture custom named function, specify its name and arity | |
~w(Felix Jaime Jaap) | |
|> Enum.map(&greet/1) | |
|> Enum.join(", ") | |
# "Hola Felix, Hola Jaime, Hola Jaap" | |
end | |
def test_8 do | |
# functions that got passed to higher order functions as target of pipe, should be in the format of function reference, not invocation | |
# can use & to define anonymous functions | |
~w(Felix Jaime Jaap) | |
|> Enum.map(&("Adios #{&1}")) | |
|> Enum.join(", ") | |
# "Adios Felix, Adios Jaime, Adios Jaap" | |
end | |
def test_9 do | |
op = fn(x) -> "Adios #{x}" end | |
# functions that got passed to higher order functions as target of pipe, should be in the format of function reference, not invocation | |
# can reference anonymous functions | |
~w(Felix Jaime Jaap) | |
|> Enum.map(&(op.(&1))) | |
|> Enum.join(", ") | |
# "Adios Felix, Adios Jaime, Adios Jaap" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment