Idiom question

2016-09-28 Thread paul
Hi. I'm new to clojure, and it is quite lovely. The threading model is great, the emacs integration is super, and the tasteful lisp extensions are good. A very nice programming environment all around. But as I write more code I find a couple of structures I'm using a lot which seem related to

Re: Idiom question

2016-09-28 Thread Alex Miller
Hi Paul, there is a rich history of people extending the thread macros. For things in core, check out some of the other threading operators: as->, cond->, some-> etc as they handle more of your cases below. This is a good overview of what they do: http://clojure.org/guides/threading_macros Add

Re: Idiom question

2016-09-28 Thread Colin Yates
You might want to checkout 'cond->', 'condp' and 'as->'. Checkout the excellent clojuredocs.org website for examples of their usage (https://clojuredocs.org/clojure.core/condp for example). Sorry to be so succinct - deadlines etc. On Wed, 28 Sep 2016, at 03:26 PM, p...@pwjw.com wrote: > Hi. > > I

Re: Idiom question

2016-09-28 Thread Stuart Sierra
I will answer the clojure.test question, since I wrote it. You can call clojure.test/is with *any* Clojure expression: if the expression returns logical true, the assertion passes. Having just the one expression keeps the scope of the testing library small. The `clojure.test/is` macro was desig

Re: Idiom question

2016-09-28 Thread Sean Corfield
Welcome to Clojure! As others have noted, there are quite a few threading macros so your example could become: (-> blah (do-this) (->> (do-that arg)) (as-> s (do-the-other a1 s a2))) Or: (-> (->> blah (do-this)

Re: Idiom question

2016-09-28 Thread paul
Wow thank you all for the really useful responses. How great. I appreciate the time. as-> cleans up a couple of ugly bits of my code already, yes. And I'm sort of irritated with myself for not thinking of _ (if (throw)) in my let but yes that's a perfect idiom. I totally get the minimalist ap

Re: Idiom question

2016-09-28 Thread Colin Yates
Welcome to one of the best things about Clojure - the community. However, if anyone starts using the phrase 'Clojurian' then it is time to leave :-). On Wed, 28 Sep 2016, at 07:17 PM, p...@pwjw.com wrote: > Wow thank you all for the really useful responses. How great. I > appreciate the time. >

Re: Idiom question

2016-09-28 Thread Alan Thompson
Hi Paul, Since you are interested in the threading macros, you might like to take a look at one of my favorite variants from the Tupelo library : Literate Threading Macro We all love to use the threading macros -> and ->> for certain t

Re: Idiom question

2016-09-28 Thread paul
Wow yes that's exactly the -%> macro I imagined just with "%" replaced with "it". And it exists! Plus I just converted my fragment to use as-> and thought "aren't the first two arguments backwards (vs, say, "let"); and what name should I use (I chose 's')". Thank you. Reading the tupelo docs n

Re: Idiom question

2016-09-28 Thread Mark Engelberg
A common convention with as-> is to use the symbol $ because it is recognized as a Clojure identifier but looks distinctive, so it stands out as the placeholder of where to thread. Personally, when reading code, I don't really like to see long uses of the threading operator, and I'd argue that if

Re: Idiom question

2016-09-28 Thread paul
This is a super interesting thread. Thank you all for your input I think you are right, @puzzler, that for my case a let may be better. The original code is above. Using as-> it looks like this (using 'it' as the name) (as-> (MidiSystem/getMidiDeviceInfo) it (filter #(= (.getName ^MidiDe

Re: Idiom question

2016-09-28 Thread Josh Tilles
By the way, the order of arguments of as-> makes more sense when it’s used *within* one of the other threading macros. For example: (-> x foo (bar y) (baz w z) (as-> mid-x (log/debug "The intermediate value:" mid-x) (if (color-requested? mid-x) (assoc mid-x :color "periwinkle

Re: Idiom question

2016-09-28 Thread Mark Engelberg
Right, and just to take this one step farther, imagine that instead of throwing an error, you wanted to actually return a value. And imagine that your call to MidiSystem/getMidiDeviceInfo might return nil as well, and this is something that needs to be protected against and a value returned accord

Re: Idiom question

2016-09-28 Thread Sean Corfield
And for comparison, here’s a threaded version that uses -> (with ->> embedded, and doto): (-> (MidiSystem/getMidiDeviceInfo) (->> (filter #(= (.getName ^MidiDevice$Info %) name)) (map #(MidiSystem/getMidiDevice ^MidDevice$Info %)) (filter #(>= (.getMaxTransmitters ^M

Re: Idiom question

2016-09-28 Thread Sean Corfield
Ooops, should be: (defn has-transmitters [^MidiDevice device] (<= 0 (.getMaxTransmitters device))) And if we made a helper for open-device, we could make it return the now-open device and then you wouldn’t need the doto – just call (open-device). Sean Corfield -- (970) FOR-SEAN

Re: Idiom question

2016-09-28 Thread paul
All very interesting, and Sean that first/or threading pattern is very helpful. @puzzler - totally get that the language is extensible yup and appreciate the mainstream warning. When I read your cond-better version I got it; and I also thought "a return statement like thing could be useful in a

Re: Parsing namespaced XML with clojure.data.xml

2016-09-28 Thread Herwig Hochleitner
So, your comment about using uri-encoding inspired me to just use that as an encoding to fit in a kw-ns. It seems to work out: https://github.com/bendlas/data.xml/commit/22cbe21181175d302c884b4ec9162bd5ebf336d7 There is a couple of open issues, that I commented on the commit. ​ I'll open a dev-thr

Re: Parsing namespaced XML with clojure.data.xml

2016-09-28 Thread Herwig Hochleitner
http://dev.clojure.org/jira/browse/CLJ-2030 ​ -- 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 un