What performance hit is that? Have you profiled the code? Assuming that this would perform worse than any other code is an assumption I'm not willing to make absent some profiler output to show me that it actually performs worse. Premature optimization, etc. etc.
I'll grant that it's not beautiful code but I disagree with the a priori assumption that it would automatically perform worse than any other alternative. On Mon, May 23, 2016 at 11:21 AM, max s <[email protected]> wrote: > It's an alternative, but I would not recommend it. > > Apart from the performance hit (branching and function call instead of > benefiting from the optimizations that come with matching on constant > values), it's not very elegant and does not express the intent of the > reduction in a functional way. > > Max > > On 23 May 2016 at 14:23, Onorio Catenacci <[email protected]> wrote: > >> You could do something like this: >> >> 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) >> # etc. etc. >> end >> >> I'm not sure that's a vastly superior alternative but I think it would >> work. >> >> -- >> Onorio >> >> >> On Sunday, May 22, 2016 at 9:54:24 AM UTC-4, Nytz12 wrote: >>> >>> I see, not too fond of how much boilerplate is required for each >>> constant, but perhpas this is the only way to get them in guards? >>> >>> On Sunday, May 22, 2016 at 12:35:24 PM UTC+2, max s wrote: >>>> >>>> Hi, >>>> >>>> I use macros for such requirement in my own code: >>>> >>>> defmodule Constants do >>>> defmacro val1 do >>>> quote do >>>> "val1" >>>> end >>>> end >>>> >>>> defmacro val2 do >>>> quote do >>>> "val2" >>>> end >>>> endend >>>> >>>> >>>> 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 >>>> endend >>>> >>>> >>>> Max >>>> >>>> >>>> On 22 May 2016 at 12:16, Nytz12 <[email protected]> wrote: >>>> >>>>> Hi >>>>> >>>>> I use the following pattern to define cross module constants >>>>> >>>>> 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 >>>>> ... >>>>> >>>>> Which lets me do something like this from other modules >>>>> >>>>> Visitor.FilterTypes.c_GREATER_THAN # == "greater than" >>>>> >>>>> Problem is that I just discored that these 'constants', which are >>>>> really function calls, cannot be used to do pattern match like: >>>>> >>>>> 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 >>>>> >>>>> Is there a better approach i'm missing? >>>>> >>>>> -- >>>>> 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 [email protected]. >>>>> To view this discussion on the web visit >>>>> https://groups.google.com/d/msgid/elixir-lang-talk/ace6b869-dfce-4d3f-aeeb-2d9bc4583f03%40googlegroups.com >>>>> <https://groups.google.com/d/msgid/elixir-lang-talk/ace6b869-dfce-4d3f-aeeb-2d9bc4583f03%40googlegroups.com?utm_medium=email&utm_source=footer> >>>>> . >>>>> For more options, visit https://groups.google.com/d/optout. >>>>> >>>> >>>> -- >> 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 [email protected]. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/elixir-lang-talk/6dd25b23-30d4-4f04-b3ef-6cad331a4489%40googlegroups.com >> <https://groups.google.com/d/msgid/elixir-lang-talk/6dd25b23-30d4-4f04-b3ef-6cad331a4489%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> >> For more options, visit https://groups.google.com/d/optout. >> > > -- > 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 > [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/elixir-lang-talk/CAOPKHes6YJYUsEK_%3DS%2B7bW4ptiJfwN3rNkFiHTxVpdX-z78MUw%40mail.gmail.com > <https://groups.google.com/d/msgid/elixir-lang-talk/CAOPKHes6YJYUsEK_%3DS%2B7bW4ptiJfwN3rNkFiHTxVpdX-z78MUw%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > > For more options, visit https://groups.google.com/d/optout. > -- Onorio Catenacci http://onor.io http://www.google.com/+OnorioCatenacci -- 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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/CAP%3DvNq81Sa1YOPzOcFUAZcwv74igjpYWfr270d%2B2JhrK4T7Tqg%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
