Created
October 23, 2015 04:03
-
-
Save killme2008/f192804eee700511642c to your computer and use it in GitHub Desktop.
Elixir protocols example
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
defprotocol Blank do | |
@doc "Returns true if data is considered blank/empty" | |
@fallback_to_any true | |
def blank?(data) | |
end | |
defimpl Blank, for: Any do | |
def blank?(_), do: false | |
end | |
# Just empty list is blank | |
defimpl Blank, for: List do | |
def blank?([]), do: true | |
def blank?(_), do: false | |
end | |
# Just empty map is blank | |
defimpl Blank, for: Map do | |
# Keep in mind we could not pattern match on %{} because | |
# it matches on all maps. We can however check if the size | |
# is zero (and size is a fast operation). | |
def blank?(map), do: map_size(map) == 0 | |
end | |
# Just the atoms false and nil are blank | |
defimpl Blank, for: Atom do | |
def blank?(false), do: true | |
def blank?(nil), do: true | |
def blank?(_), do: false | |
end |
Author
killme2008
commented
Oct 23, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment