Skip to content

Instantly share code, notes, and snippets.

@spikegrobstein
Last active December 20, 2015 03:59
Show Gist options
  • Save spikegrobstein/6067718 to your computer and use it in GitHub Desktop.
Save spikegrobstein/6067718 to your computer and use it in GitHub Desktop.
first blush at elixir
defmodule MessageBus do
# call any function that matches the listener
# type is atom (eg: :hello)
# message is string
# listeners is list of tuples of functions
# [ hello: fn(m) -> IO.puts m end, bye: fn(m) -> IO.puts m end, hello: fn(m) -> IO.puts "I will also do this crap" end ]""]
def publish(type, message, listeners) do
_publish(type, message, listeners)
end
def _publish( type, message, [] ), do: :nothing
def _publish(type, message, [ { handler, fun } |t ]) when handler == type do
fun.(message)
_publish(type, message, t)
end
def _publish( type, message, [ { _, _} | t ] ) do
_publish(type, message, t)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment