Last active
December 20, 2015 03:59
-
-
Save spikegrobstein/6067718 to your computer and use it in GitHub Desktop.
first blush at elixir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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