defmodule Visitor do
defmodule FilterTypes do
def c_GREATER_THAN, do: "greater_than"
def c_LESS_THAN, do: "less_than"
def c_IS, do: "is"
def c_IS_NOT, do: "is_not"
end
...
Visitor.FilterTypes.c_GREATER_THAN # == "greater than"
def filter(query, filter, field, value) do
case filter do
FilterTypes.c_GREATER_THAN -> greater_than(query, field, value)
FilterTypes.c_LESS_THAN -> less_than(query, field, value)
FilterTypes.c_IS -> is(query, field, value)
FilterTypes.c_IS_NOT -> is_not(query, field, value)
_ -> {:error, "unknown filter", filter}
end
end
defmodule Visitor do@greater_than "greater_than"@less_than "less_than"
def filter(query, filter, field, value) docase filter do
@greater_than -> greater_than(query, field, value)@less_than -> less_than(query, field, value)....endendend
defmodule Visitor do@greater_than "greater_than"@less_than "less_than"
def filter(query, @greater_than, field, value),do: greater_than(query, field, value)def filter(query, @less_than, field, value),do: less_than(query, field, value)end
defmodule Visitor do
def filter(query, filter, field, value) do
apply(__MODULE__, filter, [query, field, value])endend
defmodule Constants do defmacro val1 do quote do "val1" end end defmacro val2 do quote do "val2" end end end
And here's how it's used:
defmodule Test do require Constants def match v do case v do Constants.val1 -> "matched val1" Constants.val2 -> "matched val2" _ -> "no match" end end end
Max
--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/ace6b869-dfce-4d3f-aeeb-2d9bc4583f03%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
defmodule Constants do defmacro c name, value do quote do defmacro unquote(name) do unquote(value) end end end end
defmodule Constants.Vals do require Constants Constants.c val1, "val1" Constants.c val2, "val2" end
To finally use them in your code:
defmodule Test do
require Constants.Vals
def match v do case v do
Constants.Vals.val1 -> "matched val1" Constants.Vals.val2 -> "matched val2"
_ -> "no match" end end end
Metaprogramming makes you able to introduce any syntactic sugar you like (more or less), you should take advantage of it.
Max
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/d11f1487-1151-4c40-b9e3-b63d3deb67c0%40googlegroups.com.
def filter(query, filter, field, value) do
if (filter == FilterTypes.c_GREATER_THAN), do: greater_than(query, field, value)
if (filter == FilterType.c_LESS_THAN), do: less_than(query, field, value)
end
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/6dd25b23-30d4-4f04-b3ef-6cad331a4489%40googlegroups.com.
--
You received this message because you are subscribed to a topic in the Google Groups "elixir-lang-talk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-lang-talk/l-7rK9_bT2Q/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elixir-lang-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/CAOPKHes6YJYUsEK_%3DS%2B7bW4ptiJfwN3rNkFiHTxVpdX-z78MUw%40mail.gmail.com.
--
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/CAGnRm4KmHUN5PHYgoV4hT9tyOnN--o9ysuRoofi5V%2B%3DfJSth8Q%40mail.gmail.com.
--
You received this message because you are subscribed to a topic in the Google Groups "elixir-lang-talk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-lang-talk/l-7rK9_bT2Q/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elixir-lang-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/CAOPKHev-O0ZdDURxp144Xp4UrT-0FWECVYfNRwxyrg6HRkP0vQ%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/CAP%3DvNq8U5rmL%3DxfZ2UT4VJ4hDOgPaUqzThKZoSmOO8FJjZLRhw%40mail.gmail.com.
The empirical proof was running fprof, an erlang tool "used to profile a program to find out how the execution time is used".
The empirical proof was running fprof, an erlang tool "used to profile a program to find out how the execution time is used".To be fair, fprof should not be used for benchmarking because the profiling hooks will affect code performance in different ways. For example, if you are running code and then enables fprof, that same code path may now take much longer. A benchmarking tool would be ideal here.
--
You received this message because you are subscribed to a topic in the Google Groups "elixir-lang-talk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-lang-talk/l-7rK9_bT2Q/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elixir-lang-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/CAGnRm4%2BotB7niwyNFtbH_hZsw6ubw65ZXFKqVkfSdXXdCRg6VQ%40mail.gmail.com.
1: That doesn't solve the issue of sharing constants between modules
2: I use strings because I match it against user supplied input that I do not want to convert to atoms
def from_user_input("less_than"), do: :less_thandef from_user_input("greater_than"), do: :greater_than...
Say, Module A i reference :less_than and in module B :less_then (typo). This error will only be shown (silently) at runtime.
This seems like a lot of boilerplate as I may have 20 + of those filters