Skip to content

Instantly share code, notes, and snippets.

@ray-sh
Created July 26, 2017 14:02
Show Gist options
  • Save ray-sh/844e6e5d5e9dd002941acbfebb890504 to your computer and use it in GitHub Desktop.
Save ray-sh/844e6e5d5e9dd002941acbfebb890504 to your computer and use it in GitHub Desktop.
Transfor function between different and run
1.Demo how to transfor function
2.Demo how to construct function and call it
3.& capture operator will conver Module.Function/arity to lambda
4.& capture operation could be also used to shorten lambda
defmodule FunctionTransfor do
@moduledoc false
def display(msg), do: IO.puts msg
def add1 do
fn(x)-> x + 1 end
end
end
defmodule Profile do
def run(fun,msg) do
fun.(msg)
end
end
#Test case
#Profile.run(&FunctionTransfor.display/1,"hello")
#Profile.run(fn(msg)->IO.puts msg end,"hello")
#Profile.run(&IO.puts/1,"hello")
#Profile.run(FunctionTransfor.add1, 1) == 2
#Profile.run(&(&1 +1), 1) == 2
#apply(FunctionTransfor, :display, ["hello"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment