Skip to content

Instantly share code, notes, and snippets.

@sharno
Last active February 2, 2018 16:35
Show Gist options
  • Save sharno/dad4d4780f54c285508513bf5157fdc1 to your computer and use it in GitHub Desktop.
Save sharno/dad4d4780f54c285508513bf5157fdc1 to your computer and use it in GitHub Desktop.
As Elixir doesn't have a way to create an anonymous recursive function yet, we have to do a little trick and pass the function to itself when calling it to get recursion
print = fn f ->
receive do
name -> IO.puts(name)
f.(f)
end
end
server = spawn(fn -> print.(print) end)
send(server, "Sharno")
concat = fn message ->
print = fn f ->
receive do
name -> IO.puts(message <> name)
f.(f)
end
end
print.(print)
end
greet = spawn(fn -> concat.("Hello ") end)
send(greet, "Sharno")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment