Re: cond->: Using of threading expression in tests?

2015-10-05 Thread ru
Thank you very much, Sean and Oliver!

condas-> really awesome!

пятница, 2 октября 2015 г., 17:49:28 UTC+3 пользователь ru написал:
>
> Hi,
>
> Can I use in tests threading expression of cond->, and how?
>
> Thanx in advance,
>   Ru
>

-- 
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.


Re: cond->: Using of threading expression in tests?

2015-10-04 Thread Sean Corfield
So am I! Sean, can you share code for condp->> too, please.

Oh, sorry, I thought it was obvious from condp-> since the only difference is 
that it uses ->> instead of -> in one place:

(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)))

Sean


-- 
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.


Re: cond->: Using of threading expression in tests?

2015-10-03 Thread Oliver Hine
You may also find a use for condas-> which I describe in a mini blog post 
here: http://blog.juxt.pro/posts/condas.html

It combines cond-> and as-> which I find super useful when dealing with 
various fn type signatures.

On Saturday, 3 October 2015 15:59:51 UTC+1, ru wrote:
>
> So am I! Sean, can you share code for condp->> too, please.
>
> пятница, 2 октября 2015 г., 17:49:28 UTC+3 пользователь ru написал:
>>
>> Hi,
>>
>> Can I use in tests threading expression of cond->, and how?
>>
>> Thanx in advance,
>>   Ru
>>
>

-- 
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.


Re: cond->: Using of threading expression in tests?

2015-10-03 Thread ru
So am I! Sean, can you share code for condp->> too, please.

пятница, 2 октября 2015 г., 17:49:28 UTC+3 пользователь ru написал:
>
> Hi,
>
> Can I use in tests threading expression of cond->, and how?
>
> Thanx in advance,
>   Ru
>

-- 
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.


Re: cond->: Using of threading expression in tests?

2015-10-02 Thread Colin Yates
Well I feel a numpty :-).

> On 2 Oct 2015, at 18:23, Jason Felice  wrote:
> 
> This doesn't work, as (fn [x] (= (:x x) 1)) is never evaluated and the 
> function itself is always truthy.  (Same for the #(= (:x %) 1) version).
> 
> On Fri, Oct 2, 2015 at 9:00 AM, Colin Yates  > wrote:
> Alternatively you can do (cond-> {:x 1} (fn [x] (= (:x x) 1)) (assoc :x 2))
> 
>> On 2 Oct 2015, at 16:33, ru mailto:soro...@oogis.ru>> 
>> wrote:
>> 
>> Thank you, Colin.
>> 
>> This works:
>> 
>> user=> (as-> {:x 5} x1 (if (> (get x1 :x) 3) (assoc x1 :y 6) x1) (if (<= 
>> (get x1 :x) 3) (assoc x1 :y 12) x1))
>> {:y 6, :x 5}
>> 
>> But without cond-> at all :( :)
>> 
>> пятница, 2 октября 2015 г., 17:49:28 UTC+3 пользователь ru написал:
>> Hi,
>> 
>> Can I use in tests threading expression of cond->, and how?
>> 
>> Thanx in advance,
>>   Ru
>> 
>> -- 
>> 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 
> .
> 
> 
> -- 
> 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.


Re: cond->: Using of threading expression in tests?

2015-10-02 Thread Sean Corfield
We needed this functionality too and ended up writing our own utility macro:

(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)))

So (condp-> {:x 1} #(= (:x %) 1) (assoc :x 2)) produces {:x 2} as desired.

We have a condp->> version as well.

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

"If you're not annoying somebody, you're not really alive."
-- Margaret Atwood






From:   on behalf of Jason Felice
Reply-To:  
Date:  Friday, October 2, 2015 at 10:23 AM
To:  
Subject:  Re: cond->: Using of threading expression in tests?


>This doesn't work, as (fn [x] (= (:x x) 1)) is never evaluated and the 
>function itself is always truthy.  (Same for the #(= (:x %) 1) version).
>
>On Fri, Oct 2, 2015 at 9:00 AM, Colin Yates  wrote:
>
>Alternatively you can do (cond-> {:x 1} (fn [x] (= (:x x) 1)) (assoc :x 2))
>
>On 2 Oct 2015, at 16:33, ru  wrote:
>
>Thank you, Colin.
>This works:
>
>user=> (as-> {:x 5} x1 (if (> (get x1 :x) 3) (assoc x1 :y 6) x1) (if (<= (get 
>x1 :x) 3) (assoc x1 :y 12) x1))
>{:y 6, :x 5}
>
>
>But without cond-> at all :( :)
>
>пятница, 2 октября 2015 г., 17:49:28 UTC+3 пользователь ru написал:
>Hi,
>Can I use in tests threading expression of cond->, and how?
>
>Thanx in advance,
>  Ru
>
>
>

-- 
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.


Re: cond->: Using of threading expression in tests?

2015-10-02 Thread Jason Felice
This doesn't work, as (fn [x] (= (:x x) 1)) is never evaluated and the
function itself is always truthy.  (Same for the #(= (:x %) 1) version).

On Fri, Oct 2, 2015 at 9:00 AM, Colin Yates  wrote:

> Alternatively you can do (cond-> {:x 1} (fn [x] (= (:x x) 1)) (assoc :x
> 2))
>
> On 2 Oct 2015, at 16:33, ru  wrote:
>
> Thank you, Colin.
>
> This works:
>
> user=> (as-> {:x 5} x1 (if (> (get x1 :x) 3) (assoc x1 :y 6) x1) (if (<=
> (get x1 :x) 3) (assoc x1 :y 12) x1))
> {:y 6, :x 5}
>
> But without cond-> at all :( :)
>
> пятница, 2 октября 2015 г., 17:49:28 UTC+3 пользователь ru написал:
>>
>> Hi,
>>
>> Can I use in tests threading expression of cond->, and how?
>>
>> Thanx in advance,
>>   Ru
>>
>
> --
> 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.
>

-- 
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.


Re: cond->: Using of threading expression in tests?

2015-10-02 Thread ru
This is remarkable improvement and it shows that test is a function of the 
threading expression.

user=> (cond-> {:x 1} #(= (:x %) 1) (assoc :y 2))
{:y 2, :x 1}

also works.

Thanks a lot

пятница, 2 октября 2015 г., 17:49:28 UTC+3 пользователь ru написал:
>
> Hi,
>
> Can I use in tests threading expression of cond->, and how?
>
> Thanx in advance,
>   Ru
>

-- 
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.


Re: cond->: Using of threading expression in tests?

2015-10-02 Thread Colin Yates
Alternatively you can do (cond-> {:x 1} (fn [x] (= (:x x) 1)) (assoc :x 2))

> On 2 Oct 2015, at 16:33, ru  wrote:
> 
> Thank you, Colin.
> 
> This works:
> 
> user=> (as-> {:x 5} x1 (if (> (get x1 :x) 3) (assoc x1 :y 6) x1) (if (<= (get 
> x1 :x) 3) (assoc x1 :y 12) x1))
> {:y 6, :x 5}
> 
> But without cond-> at all :( :)
> 
> пятница, 2 октября 2015 г., 17:49:28 UTC+3 пользователь ru написал:
> Hi,
> 
> Can I use in tests threading expression of cond->, and how?
> 
> Thanx in advance,
>   Ru
> 
> -- 
> 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.


Re: cond->: Using of threading expression in tests?

2015-10-02 Thread ru
Thank you, Colin.

This works:

user=> (as-> {:x 5} x1 (if (> (get x1 :x) 3) (assoc x1 :y 6) x1) (if (<= 
(get x1 :x) 3) (assoc x1 :y 12) x1))
{:y 6, :x 5}

But without cond-> at all :( :)

пятница, 2 октября 2015 г., 17:49:28 UTC+3 пользователь ru написал:
>
> Hi,
>
> Can I use in tests threading expression of cond->, and how?
>
> Thanx in advance,
>   Ru
>

-- 
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.


Re: cond->: Using of threading expression in tests?

2015-10-02 Thread Colin Yates
Ah I see - maybe https://clojuredocs.org/clojure.core/as-%3E 
 will help (untried):

  (cond-> {:x 5} (as-> x1 (> (get x1 :x) 3)…)


> On 2 Oct 2015, at 16:07, ru  wrote:
> 
> Hi, Colin
> 
> For example, (cond-> {:x 5} (> (get ??? :x) 3) (assoc :y 6))
> 
> пятница, 2 октября 2015 г., 17:49:28 UTC+3 пользователь ru написал:
> Hi,
> 
> Can I use in tests threading expression of cond->, and how?
> 
> Thanx in advance,
>   Ru
> 
> -- 
> 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.


Re: cond->: Using of threading expression in tests?

2015-10-02 Thread ru
Hi, Colin

For example, (cond-> {:x 5} (> (get ??? :x) 3) (assoc :y 6))

пятница, 2 октября 2015 г., 17:49:28 UTC+3 пользователь ru написал:
>
> Hi,
>
> Can I use in tests threading expression of cond->, and how?
>
> Thanx in advance,
>   Ru
>

-- 
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.


Re: cond->: Using of threading expression in tests?

2015-10-02 Thread Colin Yates
Hi Ru - I am not sure I understand the question - can you expand on the 
use-case a bit please.
> On 2 Oct 2015, at 15:49, ru  wrote:
> 
> Hi,
> 
> Can I use in tests threading expression of cond->, and how?
> 
> Thanx in advance,
>   Ru
> 
> -- 
> 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.


cond->: Using of threading expression in tests?

2015-10-02 Thread ru
Hi,

Can I use in tests threading expression of cond->, and how?

Thanx in advance,
  Ru

-- 
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.