Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use Bitwise
- defmodule SecretHandshake do
- def commands(handshake) do
- [&decode_wink/1, &decode_double_blink/1, &decode_close_your_eyes/1, &decode_jump/1]
- |> Enum.map(fn(decoder) -> decoder.(handshake) end)
- |> Enum.reject(&is_nil/1)
- |> decode_reverse(handshake)
- end
- defp decode_wink(handshake) when (handshake &&& 1) > 0, do: "wink"
- defp decode_wink(_), do: nil
- defp decode_double_blink(handshake) when (handshake &&& 2) > 0, do: "double blink"
- defp decode_double_blink(_), do: nil
- defp decode_close_your_eyes(handshake) when (handshake &&& 4) > 0, do: "close your eyes"
- defp decode_close_your_eyes(_), do: nil
- defp decode_jump(handshake) when (handshake &&& 8) > 0, do: "jump"
- defp decode_jump(_), do: nil
- defp decode_reverse(commands, handshake) when (handshake &&& 16) > 0, do: Enum.reverse(commands)
- defp decode_reverse(commands, _), do: commands
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement