Hello do @greeting "Hello to Jason Isaacs" def say(greeting \\ @greeting) do greet greeting end defp greet(greeting), do: IO.puts(greeting) end Hello.say
Hello do def say do func = greet() func.("Hello to Jason Isaacs") end defp greet do fn(msg) -> chars = String.to_char_list(msg) IO.puts :string.to_upper(chars) end end end Hello.say # HELLO TO JASON ISAACS
Foo do # I do not think it means what you think it means def inconceivable(name = "Buttercup") do "As you wish, #{name}" end end IO.puts Foo.inconceivable("Buttercup") # -> As you wish, Buttercup
Foo do # I do not think it means what you think it means def inconceivable(name = "Buttercup") do "As you wish, #{name}" end end IO.puts Foo.inconceivable("Buttercup") # -> As you wish, Buttercup IO.puts Foo.inconceivable("Vizzini") # -> ** (FunctionClauseError) no function clause matching in Foo.inconceivable/1
= 1 1 iex(2)> x 1 iex(3)> 1 = x 1 iex(4)> 2 = x ** (MatchError) no match of right hand side value: 1 iex(1)> 1 = z ** (CompileError) iex:1: undefined function z/0
defstruct name: "", admin: false ... def something_awesome(user = %User{ admin: true }) do do_something_awesome user end def something_awesome(user = %User{}) do IO.puts "User #{user.name} is insufficiently awesome" end defp do_something_awesome(user) do ... end end
some text, display all the words which are anagrams of each other • Calculate word signatures • signature("ruby") -> bruy • signature("bury") -> bruy • "bury" is an anagram of "ruby"
code runs inside lightweight threads of execution (called processes) that are isolated and exchange information via messages" source: http://elixir-lang.org/
VM beam CPU Core scheduler run queue process process process ... CPU Core scheduler run queue process process process ... CPU Core scheduler run queue process process process ... ...
defmodule HowDoYou do def loop do receive do :shutdown -> IO.puts "Shutting down." { sender, msg } -> send(sender, { :ok, "You just #{msg}" }) loop end end end
"helpful.exs" iex(2)> pid = spawn(HowDoYou, :loop, []) iex(3)> send(pid, { self, "sign up to SnapChat" }) iex(4)> send(pid, { self, "give a presentation" }) iex(5)> send(pid, :shutdown) Shutting down. iex(6)> flush "You just sign up to SnapChat" "You just give a presentation"
do use GenServer def start_link([]) do :gen_server.start_link(__MODULE__, [], []) end def init(state) do {:ok, state} end def handle_call(ip, _from, state) do {:reply, IpLookup.Lookup.lookup(ip), state} end def lookup(pid, ip) do GenServer.call(pid, ip) end end # IpLookup.Worker.lookup(pid, "1.2.3.4") Server callbacks Client API
do use Application def start(_type, _args) do import Supervisor.Spec, warn: false children = [ worker(MyApp.Worker, [arg1, arg2, arg3]) ] opts = [strategy: :one_for_one, name: MyApp.Supervisor] Supervisor.start_link(children, opts) end end
Foo do def bar(baz) do "foo" |> String.split "foo" end def bar(:some_value), do: IO.puts("bar") end $ elixir error1.exs error1.exs:3: warning: you are piping into a function call without parentheses, which may be ambiguous. Please wrap the function you are piping into in parentheses. For example: foo 1 |> bar 2 |> baz 3 Should be written as: foo(1) |> bar(2) |> baz(3) error1.exs:2: warning: variable baz is unused error1.exs:6: warning: this clause cannot match because a previous clause at line 2 always matches