This file contains hidden or 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 EventBus do | |
| def init do | |
| {:ok, _} = Registry.start_link(:duplicate, __MODULE__) | |
| end | |
| def register_handler(event_name, {m, f}, name \\ nil) do | |
| name = name || "#{m}##{f}" | |
| Registry.register(__MODULE__, event_name, {m, f, name}) | |
| end |
This file contains hidden or 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 SimpleBus do | |
| def init do | |
| {:ok, _} = Registry.start_link(:duplicate, __MODULE__) | |
| end | |
| def register_handler(module) do | |
| Registry.register(__MODULE__, :event, module) | |
| end | |
| def emit(event_name, args) do |
This file contains hidden or 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 Protocolz do | |
| defmacro defimplz(protocolo, [for: modulo_do_struct], [do: block]) do | |
| quote do | |
| defmodule (Module.concat([unquote(protocolo), unquote(modulo_do_struct)])) do | |
| unquote(block) | |
| end | |
| end | |
| end | |
| defmacro defprotocolz(protocolo, [do: block]) do |
This file contains hidden or 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
| ALGARISMOS = {"I" => 1, "V" => 5, "X" => 10, "L" => 50, "C" => 100, "D" => 500, "M" => 1000} | |
| def to_arabico(romano) | |
| total = 0 | |
| romano = romano.chars | |
| while romano != [] | |
| ultimo = romano[-1] | |
| penultimo = romano[-2] |
This file contains hidden or 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
| # fibo O(n) | |
| CACHE = {} | |
| def fib(n) | |
| raise ArgumentError if n < 0 | |
| if CACHE[n] | |
| CACHE[n] | |
| else |
This file contains hidden or 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
| # fibo O(n^2) | |
| def fib(n) | |
| raise ArgumentError if n < 0 | |
| if n == 0 || n == 1 | |
| 1 | |
| else | |
| fib(n-1) + fib(n -2) | |
| end |
This file contains hidden or 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 Pentabonacci do | |
| import Integer, only: [is_odd: 1] | |
| defp calc(0, _), do: 0 | |
| defp calc(1, _), do: 1 | |
| defp calc(2, _), do: 1 | |
| defp calc(3, _), do: 2 | |
| defp calc(4, _), do: 4 | |
| defp calc(n, ets) do |
This file contains hidden or 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 Pentabonacci do | |
| import Integer, only: [is_odd: 1] | |
| defp calc(0, _), do: 0 | |
| defp calc(1, _), do: 1 | |
| defp calc(2, _), do: 1 | |
| defp calc(3, _), do: 2 | |
| defp calc(4, _), do: 4 | |
| defp calc(n, ets) do |
This file contains hidden or 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 Pentabonacci do | |
| import Integer, only: [is_odd: 1] | |
| defp calc(0, _), do: 0 | |
| defp calc(1, _), do: 1 | |
| defp calc(2, _), do: 1 | |
| defp calc(3, _), do: 2 | |
| defp calc(4, _), do: 4 | |
| defp calc(n, ets) do |