Re: filter but for non-sequences

2014-10-27 Thread Fluid Dynamics
On Monday, October 27, 2014 3:00:40 AM UTC-4, Marcus Magnusson wrote:

 I don't see how the macro version would offer any performance benefits - 
 in both cases, x is evaluated once and pred is called once. Am I missing 
 something?


Nah. Someone upthread suggested a macro, for some reason. I don't see much 
advantage either, other than saving the overhead of a function call and 
having exactly the same performance as just writing (if (pred x) x).

-- 
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: filter but for non-sequences

2014-10-24 Thread Laurens Van Houtven
Hi Sam,


I’m not sure I understand. If not for sequences, then what for? `(if (pred x) 
x)` looks like it’s missing an else-clause; so I don’t know what happens when 
(not (pred x)).

From the rest of your e-mail it sounds a little bit like you might want (get m 
x x)? (i.e. get me x if x isn’t in m; otherwise give me the value mapped to x 
in m).


hth
lvh


On 24 Oct 2014, at 06:06, Sam Raker sam.ra...@gmail.com wrote:

 Is there anything simpler/more idiomatic than `(if (pred x) x)`? What I 
 really want is something better than `(if (= (get a-map a-key) a-val) 
 a-map)`/`(if (= (get-in a-map some-keys) a-val) a-map)`, but that might be 
 too specific to have been 'cached' somewhere.
 
 -- 
 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.



signature.asc
Description: Message signed with OpenPGP using GPGMail


Re: filter but for non-sequences

2014-10-24 Thread Gary Verhaegen
I think the point is to get nil if x does not satisfy some predicate.

The best option would depend on the context in which these expressions are
used, but one option would be to write your predicates to return the input
as the truthy value (and nil otherwise).

On Friday, 24 October 2014, Laurens Van Houtven _...@lvh.io wrote:

 Hi Sam,


 I’m not sure I understand. If not for sequences, then what for? `(if (pred
 x) x)` looks like it’s missing an else-clause; so I don’t know what happens
 when (not (pred x)).

 From the rest of your e-mail it sounds a little bit like you might want
 (get m x x)? (i.e. get me x if x isn’t in m; otherwise give me the value
 mapped to x in m).


 hth
 lvh


 On 24 Oct 2014, at 06:06, Sam Raker sam.ra...@gmail.com javascript:;
 wrote:

  Is there anything simpler/more idiomatic than `(if (pred x) x)`? What I
 really want is something better than `(if (= (get a-map a-key) a-val)
 a-map)`/`(if (= (get-in a-map some-keys) a-val) a-map)`, but that might be
 too specific to have been 'cached' somewhere.
 
  --
  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
 javascript:;
  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 javascript:;
  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 javascript:;.
  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: filter but for non-sequences

2014-10-24 Thread Sam Raker
I think the point is to get nil if x does not satisfy some predicate. -- 
exactly.

I basically want something like `if-let`, but which returns x instead of 
f(x). `(if-let [m-with-k (contains? m k)]...` results in `m-with-k` 
evaluating to `true`, not `m`. I *think* it's better to reject something 
that doesn't meet your criteria early, as opposed to guarding against not 
accidentally doing something like `(.toUpper {:foo m})` and getting an 
error every step of the way.

But like I said, I'm totally willing to believe there's a more 
idiomatic/appropriate way of doing things.

On Friday, October 24, 2014 4:59:33 AM UTC-4, Gary Verhaegen wrote:

 I think the point is to get nil if x does not satisfy some predicate.

 The best option would depend on the context in which these expressions are 
 used, but one option would be to write your predicates to return the input 
 as the truthy value (and nil otherwise).

 On Friday, 24 October 2014, Laurens Van Houtven _...@lvh.io wrote:

 Hi Sam,


 I’m not sure I understand. If not for sequences, then what for? `(if 
 (pred x) x)` looks like it’s missing an else-clause; so I don’t know what 
 happens when (not (pred x)).

 From the rest of your e-mail it sounds a little bit like you might want 
 (get m x x)? (i.e. get me x if x isn’t in m; otherwise give me the value 
 mapped to x in m).


 hth
 lvh


 On 24 Oct 2014, at 06:06, Sam Raker sam.ra...@gmail.com wrote:

  Is there anything simpler/more idiomatic than `(if (pred x) x)`? What I 
 really want is something better than `(if (= (get a-map a-key) a-val) 
 a-map)`/`(if (= (get-in a-map some-keys) a-val) a-map)`, but that might be 
 too specific to have been 'cached' somewhere.
 
  --
  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: filter but for non-sequences

2014-10-24 Thread Laurens Van Houtven
On 24 Oct 2014, at 15:05, Sam Raker sam.ra...@gmail.com wrote:

 I think the point is to get nil if x does not satisfy some predicate. -- 
 exactly.

… and shorter than (when (p x) x)? If you can come up for a good name for that, 
I guess you could macro it? :-)

hth
lvh


signature.asc
Description: Message signed with OpenPGP using GPGMail


Re: filter but for non-sequences

2014-10-24 Thread Fluid Dynamics
(defn if-its [pred x]
  (if (pred x) x))

= (if-its even? 4)
4
= (if-its even? 5)
nil

Or, if you want a macro for performance reasons or something, and don't 
mind the loss of e.g. (filter identity (map (partial if-its pred) coll)):

(defmacro if-its [pred x]
  `(let [a# ~x]
 (if (~pred a#) a#)))

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


filter but for non-sequences

2014-10-23 Thread Sam Raker
Is there anything simpler/more idiomatic than `(if (pred x) x)`? What I 
really want is something better than `(if (= (get a-map a-key) a-val) 
a-map)`/`(if (= (get-in a-map some-keys) a-val) a-map)`, but that might be 
too specific to have been 'cached' somewhere.

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