2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
57
Task/Call-a-function/Elixir/call-a-function.elixir
Normal file
57
Task/Call-a-function/Elixir/call-a-function.elixir
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
# Anonymous function
|
||||
|
||||
foo = fn() ->
|
||||
IO.puts("foo")
|
||||
end
|
||||
|
||||
foo() #=> undefined function foo/0
|
||||
foo.() #=> "foo"
|
||||
|
||||
# Using `def`
|
||||
|
||||
defmodule Foo do
|
||||
def foo do
|
||||
IO.puts("foo")
|
||||
end
|
||||
end
|
||||
|
||||
Foo.foo #=> "foo"
|
||||
Foo.foo() #=> "foo"
|
||||
|
||||
|
||||
# Calling a function with a fixed number of arguments
|
||||
|
||||
defmodule Foo do
|
||||
def foo(x) do
|
||||
IO.puts(x)
|
||||
end
|
||||
end
|
||||
|
||||
Foo.foo("foo") #=> "foo"
|
||||
|
||||
# Calling a function with a default argument
|
||||
|
||||
defmodule Foo do
|
||||
def foo(x \\ "foo") do
|
||||
IO.puts(x)
|
||||
end
|
||||
end
|
||||
|
||||
Foo.foo() #=> "foo"
|
||||
Foo.foo("bar") #=> "bar"
|
||||
|
||||
# There is no such thing as a function with a variable number of arguments. So in Elixir, you'd call the function with a list
|
||||
|
||||
defmodule Foo do
|
||||
def foo(args) when is_list(args) do
|
||||
Enum.each(args, &(IO.puts(&1)))
|
||||
end
|
||||
end
|
||||
|
||||
# Calling a function with named arguments
|
||||
|
||||
defmodule Foo do
|
||||
def foo([x: x]) do
|
||||
IO.inspect(x)
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue