This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2015-11-20
Channels
- # admin-announcements (28)
- # aws (16)
- # beginners (70)
- # boot (54)
- # cider (86)
- # cljsrn (8)
- # clojure (14)
- # clojure-art (12)
- # clojure-conj (2)
- # clojure-hk (45)
- # clojure-nl (2)
- # clojure-poland (2)
- # clojure-russia (32)
- # clojurescript (60)
- # cursive (27)
- # datomic (12)
- # devcards (46)
- # editors (2)
- # emacs (37)
- # immutant (72)
- # jobs (6)
- # ldnclj (7)
- # leiningen (1)
- # off-topic (1)
- # om (205)
- # onyx (16)
- # re-frame (21)
- # reagent (52)
- # slack-help (2)
- # spacemacs (11)
why does this (println (set (map (partial matching-part "left_") ["right_", "left_", "center-", "right2-", "left2-"]))) give a null pointer exception ?
Good morning :)
meikemertsch: I wonder why (println (set (map (partial matching-part "left_") ["right_", "left_", "center-", "right2-", "left2-"]))) give a null pointer exception
@roelof: "left_"??
Be careful with "_" and "-"
Part is one of the elements in your hobbit list
These are maps with name and size.
You put just a string there, so on evaluation there is no key "name"
Partial matching-part part
Not Partial matching-part "left-2"
Yeah, seems reasonable. Or you do
Partial matching-part {:name "left-eye" :size 1}
That could work
Great!!
I'm currently shopping. Am home with proper keyboard in about 45 mins
Feel free to ping me again please :)
I am not as fast in typing with my phone ;)
what is wrong here : (println (map (partial (str "test" )) [1 2] ) ) I see this error message Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to clojure.lang.IFn,
Let’s take it in a private room?
I’m back home and at your service
@roelof partial accepts a function, and so does map so you should be doing (map (partial str "test") [1 2])
@roelof idiomatic clojure encourages you to be more explicit about your mappings though; the same can be achieved with this: (map (fn [n] str "test" n) [1 2]) Or better: (map #(str "test" %) [1 2])
Hi @jethroksy can you tell me more about it being more idiomatic than partial?
I’m still learning and would like to know more about that
@meikemertsch when we use functions like map and reduce we tend to already know what we're trying to process. That is, we know the arity of element fed into the map and reduce. In this case it's of arity 1 (the numbers 1 and 2). Partial on the other hand doesn't do an arity check. By explicitly telling map with (fn [n] ...) That you're dealing with only an integer eliminates the possibility of a whole host of arity-related bugs. If this sounds terribly hard to understand, its because I suck at explaining :P but what I'm essentially trying to say is the more declarative you are about stuff that tend not to change, the better?
nice, thanks..
I have a follow up question then. In which cases would it be idiomatic to use partial then?
So suppose I wanna multiply things by a 100 a lot, I'd do: (def multiply-by-100 (partial * 100))
Yeah, that one is clear. But why would you not use
(def multiply-by-100 #(* 100 %))
I’m kinda searching for a heuristic when to use partial/named functions/anonymous functions
(def multiply-by-100 #(apply * 100 &%))
But that's not really answering your question because the shorthand notation has a version that allows multiple arities
I guess anonymous functions and partial functions can both be used to achieve equivalent outcomes, but readability is better often in one more than the other?
I kinda favour partial…. but I’d like to know idiomatic ways, too 😉
will read through http://blog.jayfields.com/2012/10/clojure-avoiding-anonymous-functions.html now
thanks, will look into it
His conclusion is that it's a matter of personal preference, and I think it is, but functions give finer control
Thanks for your patience
to hopefully add something of value for someone, partial is a function that literally just returns an anonymous function - it is a specific use of anonymous functions. I haven't looked at either of the blog posts linked, so I might be repeating this, but partial doesn't do anything an anonymous function can't, and I believe the same holds true in the opposite direction.
I'd say, of course, it's personal preference, but partial does have the advantage of an immediately clear use of an anonymous function in some situations (the common example being detailed above by @jethroksy of "I'm performing this same calculation a lot of times and want to abstract it out to a more meaningful value").