Thanks for the explanation!

So the solution is much simpler:

user=> (-> person :childs first (select-keys [:name]))
{:name "Son"}

Cool :-)



On Sep 22, 3:26 pm, Jarkko Oranen <chous...@gmail.com> wrote:
> On Sep 22, 3:58 pm, Roman Roelofsen <roman.roelof...@googlemail.com>
> wrote:
>
>
>
> > Hi there!
>
> > Lets assume I have this map:
>
> > user=> (def person {:name "Father" :childs [{:name "Son" :age 10}]})
>
> > Testing:
>
> > user=> (-> person :childs first)
> > {:name "Son", :age 10}
>
> > Now lets filter the child map:
>
> > user=> (def only-name (fn [m] (select-keys m [:name])))
> > user=> (-> person :childs first only-name)
> > {:name "Son"}
>
> > So why does this not work?:
>
> > user=> (-> person :childs first (fn [m] (select-keys m [:name])))
> > java.lang.RuntimeException: java.lang.IllegalArgumentException: Don't
> > know how to create ISeq from: Symbol (NO_SOURCE_FILE:59)
>
> > Or this?:
>
> > user=> (-> person :childs first #(select-keys % [:name]))
> > java.lang.ClassCastException: clojure.lang.Symbol cannot be cast to
> > clojure.lang.IPersistentVector (NO_SOURCE_FILE:60)
>
> You assume more of -> than you should :)
> -> does not take functions as parameters. It only sees a bunch of
> symbols, as it is a macro, which means it does a code transformation.
> It puts the first form (the symbol person) in the second position of
> the following form (the keyword :childs), which must be a sequence, or
> if it is not, is wrapped in a list first. This repeats recursively
> until there are no more forms left.
> Now, the anonymous function example fails because (-> person :childs
> first (fn [] whatever)) transforms into (fn (first (:childs person)) x
> [] y). which is not what you intended, but nonetheless a correct
> result.
>
> If you look at the examples that work and macroexpand them, you would
> see that (-> person :childs first) transforms first into (-> (:childs
> person) first) and from there to (first (:childs person)) which
> happens to be a valid Clojure expression

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to