using -> on a nested hash-map with string keywords.

147 views
Skip to first unread message

Boris V. Schmid

unread,
May 31, 2012, 10:55:57 AM5/31/12
to clo...@googlegroups.com
Can someone tell me what I'm overlooking (clojure 1.4)

(-> (hash-map :b (hash-map :a 3)) :b :a)
3
user> (-> (hash-map "b" (hash-map :a 3)) "b" :a)
; Evaluation aborted: java.lang.String cannot be cast to clojure.lang.IFn

I'm not sure why the first can work, and the second cannot. Is it a logical limitation of the language, or an oversight in how the macro -> is build?

Dave Ray

unread,
May 31, 2012, 10:59:13 AM5/31/12
to clo...@googlegroups.com
Keywords implement IFn meaning they can act as functions that look
themselves up in a map. Strings are just strings. Replace "b" with
(get "b") and you'll get the behavior you're looking for.

Dave
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@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+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

Jay Fields

unread,
May 31, 2012, 11:01:10 AM5/31/12
to clo...@googlegroups.com
(:b {:b 1}) => 1
("b" {"b" 1}) => same error.

If you want that to work, you need something that creates ({"b" 1} "b") or (get {"b" 1} "b")

for your example, I believe this works
(-> (hash-map "b" (hash-map :a 3)) (get "b") :a)

Cheers, Jay

Alan Malloy

unread,
May 31, 2012, 1:22:50 PM5/31/12
to Clojure
Yes, but really to GET a value nested IN a series of maps, he should
just be using get-in, rather than threading anything at all.

On May 31, 7:59 am, Dave Ray <dave...@gmail.com> wrote:
> Keywords implement IFn meaning they can act as functions that look
> themselves up in a map. Strings are just strings. Replace "b" with
> (get "b") and you'll get the behavior you're looking for.
>
> Dave
>

Dave Ray

unread,
May 31, 2012, 1:27:27 PM5/31/12
to clo...@googlegroups.com
Too true.

Boris V. Schmid

unread,
Jun 1, 2012, 3:08:56 AM6/1/12
to clo...@googlegroups.com
Thanks, it was get-in that I was looking for, but couldn't find. Also thanks to Dave for explaining that keywords have some internal magic compared to strings. will remember!
> > clojure+unsubscribe@googlegroups.com

Sean Corfield

unread,
Jun 1, 2012, 6:27:04 PM6/1/12
to clo...@googlegroups.com
On Fri, Jun 1, 2012 at 12:08 AM, Boris V. Schmid <boris....@gmail.com> wrote:
> Thanks, it was get-in that I was looking for, but couldn't find.

At this point I'll give a plug for http://clojureatlas.com as a great
way to explore the Clojure core/libraries by concept.
--
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/
World Singles, LLC. -- http://worldsingles.com/

"Perfection is the enemy of the good."
-- Gustave Flaubert, French realist novelist (1821-1880)
Reply all
Reply to author
Forward
0 new messages