@josevalim: Thank you very much!
Last question (related to previous - see “My current version”):
Do you think that defguard
could help me short my code?
Here is a old, but full version that uses macros:
defmodule Example do
defmacro is_rgb_integer(integer) do
quote do
unquote(integer) in 0..255
end
end
defmacro is_rgb_strict(rgb) do
quote do
Example.is_rgb_integer(elem(unquote(rgb), 0)) and
Example.is_rgb_integer(elem(unquote(rgb), 1)) and
Example.is_rgb_integer(elem(unquote(rgb), 2))
end
end
defmacro is_css_alpha(alpha) do
quote do
unquote(alpha) >= 0 and unquote(alpha) <= 1
end
end
defmacro is_rgba(rgba) do
quote do
tuple_size(unquote(rgba)) == 4 and Example.is_rgb_strict(unquote(rgba)) and
Example.is_css_alpha(elem(unquote(rgba), 3))
end
end
defmacro is_rgb(rgba) do
quote do
(tuple_size(unquote(rgba)) == 3 and Example.is_rgb_strict(unquote(rgba))) or
(tuple_size(unquote(rgba)) == 4 and Example.is_rgb_strict(unquote(rgba)) and
Example.is_css_alpha(elem(unquote(rgba), 3)))
end
end
end
It would be awesome if it will be possible to somehow declare guards with pattern matching in future - just like in function head.