It is always matching the first clause because it is getting translated 
into a `let` binding. In match, the left-hand side `get` is just a symbol, 
not the `var` it resolves to, so might as well be `(let [fun assoc] (match 
fun foo "foo!"))` where `foo` will just be bound to `assoc`.

Match does not recognize vars on the left, but does recognize local 
bindings, so one option is to install them first

    (let [fun assoc
          get get
          assoc assoc]
      (match fun
        get "get"
        assoc "assoc"
        :else "other"))

Another option would be to use predicates

    (let [fun assoc]
      (match fun
        (_ :guard (partial identical? get)) "get"
        (_ :guard (partial identical? assoc))"assoc"
        :else "other"))

Another would be to wrap the functions in a way their value is respected, 
e.g. as a key in a map

    (let [fun assoc]
      (match {fun nil}
        {get _} "get"
        {assoc _} "assoc"
        :else "other"))

None of these are ideal, but the last seems the least offensive to me. One 
might wish for a special `(:value x)` syntax for such cases. 

On Monday, April 7, 2014 7:24:10 AM UTC-5, Serzh Nechyporchuk wrote:
>
> How can I match fns as values? For example:
>
> (let [fun assoc]
>   (match [fun]
>      [get] "get"
>      [assoc] "assoc"
>      :else "other"))
>
> The example above is not match correctly. It always matches on first 
> clause.
>
> Thank you.
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to