--
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
---
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 clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
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 clojure+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
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
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 clojure+unsubscribe@googlegroups.com.
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 clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
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
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 clojure+u...@googlegroups.com.
I almost never use either the `comp` or the `partial` functions. I think it is clearer to either compose the functions like Gary showed, or to use a threading macro (my favorite is the `it->` macro from the Tupelo library).Alan
--
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+unsubscribe@googlegroups.com
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 clojure+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
But that is rather verbose and naming the `coll` argument is kinda
pointless, so you can simplify it to
#(map str/lower-case %)
And as you see, have a function which calls a function (`map`) with
the first n arguments (in this case 1) pre-set (`str/lower-case`).
Therefore you can use `partial` do do just that:
(partial map str/lower-case)
All of these functions are equivalent and you can use them in the
`comp` call. There are some stylistic disagreements whether `#()` or
`partial` is nicer, but that's a story for another time :)
I use comp all the time, not only for transducers, but also for digging into maps:(map (comp first :pets)[{:pets [:fluffy]}{:pets [:spot]}])=> (:fluffy, :spot)Partial is also handy when used with a lot of sequence functions(->> [1 2 3 4 5](filter (partial > 3)))Sure I could write that function as #(< 3 %), but I find that syntax harder to mentally parse as I have to remember that the body of the #() is a function context and then I have to look up where the % symbol is. (partial > 3) is just easier to understand.Timothy
On Thu, Oct 27, 2016 at 6:12 PM, Mark Engelberg <mark.en...@gmail.com> wrote:
On Thu, Oct 27, 2016 at 9:39 AM, Alan Thompson <cloo...@gmail.com> wrote:I almost never use either the `comp` or the `partial` functions. I think it is clearer to either compose the functions like Gary showed, or to use a threading macro (my favorite is the `it->` macro from the Tupelo library).AlanYou need to use comp if you're building a transducer.
--
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
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 clojure+u...@googlegroups.com.
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 clojure+unsubscribe@googlegroups.com.
I almost never use either the `comp` or the `partial` functions. I think it is clearer to either compose the functions like Gary showed, or to use a threading macro (my favorite is the `it->` macro from the Tupelo library).Alan
--
I actually prefer the following style to both of the above:(defn camel->keyword*[s](let [words (str/split s #"(?<=[a-z])(?=[A-Z])")lc-words (map str/lower-case words)joined-words (str/join "-" lc-words)](keyword joined-words)))Reasons:- Your intermediate values are explicitly named, which helps to make the code self-describing- It is (marginally) more performant than the composed function case (I think exactly matches the threading macro)- You can use the intermediate values in more than one of the following steps if needed, which can make refactoring / adding new features easier- The ordering is (to me) more logical as it describes the stages of the transformation in the order they are performed.- It is less "Clever". Clever code is generally bad for maintenance and future understanding. Both functional composition and the code-transformation effects of the threading macro represent conceptual overhead that you don't need to pay (in this case).
The other thing to watch out for when naming intermediate results is that you don’t hold onto the head of large sequences. It’s not a problem with these book examples but can be a problem in large scale programming.
Overall tho’, the readability of these different forms is very subjective. I personally do not like the #(my-fn %) form – I find the added “punctuation” to be noisy in comparison to (partial my-fn) – and we have a ‘flip’ function in our “utility” library to handle the first-argument-omitted case where partial is not suitable so we don’t need #(my-fn % :b :c) either. I prefer -> / ->> over composition of (curried) functions from a readability p.o.v. but I know other folks who prefer ‘comp’.
Sean Corfield -- (970) FOR-SEAN -- (904) 302-SEAN
An Architect's View -- http://corfield.org/
"If you're not annoying somebody, you're not really alive."
-- Margaret Atwood
--
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
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 clojure+u...@googlegroups.com.