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'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 me not knowing idioms for a couple of uses > cases. So thought I'd ask and see if you have any suggestions. > > Apologies if this is covered elsewhere. And if I should read some > existing documentation I didn't find, I apologize for missing it. And > thanks in advance for your time reading! > > First the thrush operators (-> and ->>) are super handy. But I find > myself needing to 'move' arguments every now and then. So I get code > which looks like > > (->> blah > (do-this) > (do-that arg) > ((fn [s] (rearrange arg s arg)))) > > quite a lot.The alternate is a big nested let like > > (let [ first (blah) > second (do-this first) > ... > result (wrap-it-up fourteenth) ] > result) > > for sort of sequential application where arguments fall in different > 'spots'. So I sort of find myself wanting to write a 'positional- > thrush' macro like > > (-%> blah > (do-this %) > (do-that arg %) > (do-the-other a1 % a2)) > > where % is replaced with the output of the prior. But no such operator > exists as far as I can see. So either I've had a good idea (which is > unlikely since I'm super new to the language) or there's some other > idiom you all use for this pattern which I've missed. > > The second is smaller, but is more a question. clojure.test seems to > only have 'is' so for things like equality I end up writing (is (= > (...) (...))) a lot. Or to test if an exception is thrown (is (thrown? > ...)). That's OK, but I'm wondering what led to that decision rather > than having is-eq and is-thrown and so on (considering the core > language has shortcuts like when and unless and if-not so the compound > macros seem idiomatic). > > The last is sort of related to the first. Sometimes I'm assembling a > data structure in a set of operators and I write them with a let or a > -> and half way through I have an error condition I want to check. In > a mutable procedural language you would do something like > > x = blah > y = bim > if (! (condition (y))) throw "y doesn't meet condition" > z = blob > > I don't see a good idiom for this. I have to split and nest lets for > instance > > (let [x (blah) y (bim) ] > (if (condition (y)) (throw ...) > (let [ z (blob) ] > )) > > which seems a bit ugly. I sort of want a let-with-test or a thrush-with- > test so something which looks like > > (-%?> (init) > (operator-1 %) (post-condition) > (operator-2 %) (post-condition) ) > > where if I don't have a post condition I could just use 'true'. Then > this expands to doing a quick '(if (not (postcondition (intermedia- > result)))) throw...) > > but that's a crazy thing to want. So curious how you all tackle this. > > Thank you all for your consideration. And apologies again if this is > covered elsewhere or I should have asked in a different forum. > > Best, > > Paul > > -- > 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 > --- > 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+unsubscr...@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 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 --- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.