Re: cond- variant?

2014-02-12 Thread Alex Baranosky
I wrote pred-cond for Midje way back, which does what you want.
https://github.com/marick/Midje/blob/master/src/midje/clojure/core.clj#L176

Example use:
https://github.com/marick/Midje/blob/master/src/midje/parsing/1_to_explicit_form/facts.clj#L100

Like normal cond pred-cond will short-circuit once it hits a match (so
unlike con-)



On Tue, Feb 11, 2014 at 11:36 PM, s...@corfield.org wrote:

  I use cond- quite a lot but I find myself often wanting a version that
 uses predicate functions rather than expressions which would seem
 more “threaded”. Am I just missing a better way to write these things?

 For example:

 (cond- accounts
   (empty? accounts) (conj “stuff”))

 That just screams for something like:

 (condp- accounts
   empty? (conj “stuff”))

 Then you could thread the predicates:

 (condp- a
   p1 (f1 args)
   p2 (f2 args))

 which would be:

 (let [x (if (p1 a) (f1 a args) a)]
   (if (p2 x) (f2 x args) x))

 Suggestions?

 Sean Corfield -- (904) 302-SEAN
 An Architect's View -- http://corfield.org
 World Singles, LLC - http://worldsingles.com

  --
 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/groups/opt_out.


-- 
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/groups/opt_out.


Re: cond- variant?

2014-02-12 Thread Sean Corfield
On Feb 12, 2014, at 1:34 AM, Alex Baranosky alexander.barano...@gmail.com 
wrote:
 I wrote pred-cond for Midje way back, which does what you want. 
 https://github.com/marick/Midje/blob/master/src/midje/clojure/core.clj#L176

That doesn't appear to thread each expression through the results so it isn't 
really a variant of cond- but between that and the source of cond- I suspect 
I will just end up rolling my own...

Sean Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/

Perfection is the enemy of the good.
-- Gustave Flaubert, French realist novelist (1821-1880)





signature.asc
Description: Message signed with OpenPGP using GPGMail


Re: cond- variant?

2014-02-12 Thread Sean Corfield
Here's what I ended up with - minor variants of cond- and cond-

(defmacro condp-
  Takes an expression and a set of predicate/form pairs. Threads expr (via -)
  through each form for which the corresponding predicate is true of expr.
  Note that, unlike cond branching, condp- threading does not short circuit
  after the first true test expression.
  [expr  clauses]
  (assert (even? (count clauses)))
  (let [g (gensym)
pstep (fn [[pred step]] `(if (~pred ~g) (- ~g ~step) ~g))]
`(let [~g ~expr
   ~@(interleave (repeat g) (map pstep (partition 2 clauses)))]
   ~g)))

(defmacro condp-
  Takes an expression and a set of predicate/form pairs. Threads expr (via -)
  through each form for which the corresponding predicate is true of expr.
  Note that, unlike cond branching, condp- threading does not short circuit
  after the first true test expression.
  [expr  clauses]
  (assert (even? (count clauses)))
  (let [g (gensym)
pstep (fn [[pred step]] `(if (~pred ~g) (- ~g ~step) ~g))]
`(let [~g ~expr
   ~@(interleave (repeat g) (map pstep (partition 2 clauses)))]
   ~g)))

On Feb 12, 2014, at 2:34 PM, Sean Corfield s...@corfield.org wrote:

 On Feb 12, 2014, at 1:34 AM, Alex Baranosky alexander.barano...@gmail.com 
 wrote:
 I wrote pred-cond for Midje way back, which does what you want. 
 https://github.com/marick/Midje/blob/master/src/midje/clojure/core.clj#L176
 
 That doesn't appear to thread each expression through the results so it 
 isn't really a variant of cond- but between that and the source of cond- I 
 suspect I will just end up rolling my own...





signature.asc
Description: Message signed with OpenPGP using GPGMail


cond- variant?

2014-02-11 Thread sean
I use cond- quite a lot but I find myself often wanting a version that uses 
predicate functions rather than expressions which would seem more “threaded”. 
Am I just missing a better way to write these things?


For example:


(cond- accounts

  (empty? accounts) (conj “stuff”))


That just screams for something like:


(condp- accounts

  empty? (conj “stuff”))


Then you could thread the predicates:


(condp- a

  p1 (f1 args)

  p2 (f2 args))


which would be:


(let [x (if (p1 a) (f1 a args) a)]

  (if (p2 x) (f2 x args) x))


Suggestions?






Sean Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org
World Singles, LLC - http://worldsingles.com

-- 
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/groups/opt_out.