Re: find first match in a sequence

2019-03-21 Thread Nathan Booth
I'm glad you pointed this out! On stackoverflow everyone was assuming 
filter was completely lazy in all cases

On Sunday, May 19, 2013 at 4:54:53 PM UTC+2, Jim foo.bar wrote:
>
> ha! you cheated with iterate... 
>
> try this which is closer to the example...
>
> (first (filter odd? (map #(do (println "realized " %) %)  [2 4 6 7 8 9])))
> realized  2
> realized  4
> realized  6
> realized  7
> realized  8
> realized  9
> 7
>
> Jim
>
>
>
> On 19/05/13 15:31, Cedric Greevey wrote:
>
> On Sun, May 19, 2013 at 10:06 AM, Jim - FooBar();  > wrote:
>
>> no need to traverse the entire seq with 'filter' if you only want the 1st 
>> match...
>>
>
> Pretty sure filter is lazy.
>
> user=> (first (filter odd? (map #(do (println "realized " %) %) (iterate 
> inc 0
> realized  0
> realized  1
> 1
> user=>
>
> -- 
> -- 
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@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+u...@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+u...@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/d/optout.


Re: find first match in a sequence

2013-05-19 Thread Jonathan Fischer Friberg
On Sun, May 19, 2013 at 4:54 PM, Jim - FooBar(); wrote:

>  ha! you cheated with iterate...
>
> try this which is closer to the example...
>
> (first (filter odd? (map #(do (println "realized " %) %)  [2 4 6 7 8 9])))
> realized  2
> realized  4
> realized  6
> realized  7
> realized  8
> realized  9
> 7
>
> Jim
>

=> (some #(when (odd? %) %)
 (map #(do (println "realized" %) %)
  [2 4 6 7 8 9]))
realized 2
realized 4
realized 6
realized 7
realized 8
realized 9
7

;)

In fact, just calling 'first' on a mapped sequence gives the same behaviour:

=> (first (map #(do (println "realized" %) %)
   [2 4 6 7 8 9]))
realized 2
realized 4
realized 6
realized 7
realized 8
realized 9
2

That said, 'some' does avoid realizing the first 32 elements if it's a vec.
I still don't
think you should care about that though - that's kind of the point of
laziness, to be
able to use map/filter etc without caring about how much is realized.

Jonathan

-- 
-- 
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: find first match in a sequence

2013-05-19 Thread Mikera
On Wednesday, 11 February 2009 00:18:53 UTC+8, Jeff Rose wrote:

> Hi,
>   Is there a built-in function that will return the first item in a 
> collection that matches a predicate?  (Something equivalent to Ruby's 
> Enumerable#find...)  Seems pretty basic, but I can't find it in the docs.
>
> Thanks,
> Jeff
>
See the find-first function in my cljutils library:
https://github.com/mikera/clojure-utils/blob/master/src/main/clojure/mikera/cljutils/find.clj

Note the initial check to see if the collection is Indexed: if it is then 
it is better to search by index than to create a seq (which causes a lot of 
unnecessary allocations)

-- 
-- 
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: find first match in a sequence

2013-05-19 Thread Jim - FooBar();

remember the 32-chunked model... :)

Jim

On 19/05/13 15:54, Jim - FooBar(); wrote:

ha! you cheated with iterate...

try this which is closer to the example...

(first (filter odd? (map #(do (println "realized " %) %)  [2 4 6 7 8 9])))
realized  2
realized  4
realized  6
realized  7
realized  8
realized  9
7

Jim



On 19/05/13 15:31, Cedric Greevey wrote:
On Sun, May 19, 2013 at 10:06 AM, Jim - FooBar(); 
mailto:jimpil1...@gmail.com>> wrote:


no need to traverse the entire seq with 'filter' if you only want
the 1st match...


Pretty sure filter is lazy.

user=> (first (filter odd? (map #(do (println "realized " %) %) 
(iterate inc 0

realized  0
realized  1
1
user=>

--
--
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: find first match in a sequence

2013-05-19 Thread Jim - FooBar();

ha! you cheated with iterate...

try this which is closer to the example...

(first (filter odd? (map #(do (println "realized " %) %)  [2 4 6 7 8 9])))
realized  2
realized  4
realized  6
realized  7
realized  8
realized  9
7

Jim



On 19/05/13 15:31, Cedric Greevey wrote:
On Sun, May 19, 2013 at 10:06 AM, Jim - FooBar(); 
mailto:jimpil1...@gmail.com>> wrote:


no need to traverse the entire seq with 'filter' if you only want
the 1st match...


Pretty sure filter is lazy.

user=> (first (filter odd? (map #(do (println "realized " %) %) 
(iterate inc 0

realized  0
realized  1
1
user=>

--
--
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: find first match in a sequence

2013-05-19 Thread Cedric Greevey
On Sun, May 19, 2013 at 10:06 AM, Jim - FooBar(); wrote:

>  no need to traverse the entire seq with 'filter' if you only want the
> 1st match...
>

Pretty sure filter is lazy.

user=> (first (filter odd? (map #(do (println "realized " %) %) (iterate
inc 0
realized  0
realized  1
1
user=>

-- 
-- 
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: find first match in a sequence

2013-05-19 Thread Kelker Ryan
Try this 
user> (first (drop-while even? [2 4 6 7 8 10]))
7

19.05.2013, 23:06, "Jim - FooBar();" :
> no need to traverse the entire seq with 'filter' if you only want the 1st 
> match...
>
> (some #(when (odd? %) %)  [2 4 6 7 8 9])
> => 7
>
> Jim
>
> On 19/05/13 13:42, Thumbnail wrote:
>
>> ... or just (comp first filter)  >
>>> ((comp first filter) odd? [2 4 6 7 8 9])
>>>
>>> => 7
>>
>> --
>> --
>> 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.

-- 
-- 
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: find first match in a sequence

2013-05-19 Thread Jim - FooBar();
no need to traverse the entire seq with 'filter' if you only want the 
1st match...


(some #(when (odd? %) %)  [2 4 6 7 8 9])
=> 7

Jim


On 19/05/13 13:42, Thumbnail wrote:

... or just (comp first filter)


((comp first filter) odd? [2 4 6 7 8 9])
=> 7

--
--
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: find first match in a sequence

2013-05-19 Thread Thumbnail
... or just (comp first filter) 


((comp first filter) odd? [2 4 6 7 8 9]) 
=> 7

 

-- 
-- 
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: find first match in a sequence

2009-02-10 Thread Meikel Brandmeyer

Hi,

Am 10.02.2009 um 18:00 schrieb Jeff Rose:

Oh cool!  I hadn't thought about this aspect of laziness before.  I  
can

see there is some zen here that is worth exploring...


Many ways to Rome:

(first (drop-while (complement predicate) coll))

:)

Sincerely
Meikel



smime.p7s
Description: S/MIME cryptographic signature


Re: find first match in a sequence

2009-02-10 Thread Jeff Rose

Oh cool!  I hadn't thought about this aspect of laziness before.  I can 
see there is some zen here that is worth exploring...

Thanks Mark and Stuart.

-Jeff

Mark Fredrickson wrote:
> Filter is lazy:
>
> http://clojure.org/api#toc228
>
> So you can implement find-first as (first (filter pred coll))
>
> -M
>
> On Feb 10, 2009, at 10:19 AM, Jeff Rose wrote:
>
>   
>> Well, in case someone else needs the same function and it isn't built-
>> in, here's what I'm using in the meantime.  (Based off of the some
>> function that comes in core...)
>>
>> (defn find-first [pred coll]
>>  (when (seq coll)
>>(if (pred (first coll))
>>  (first coll)
>>  (recur pred (rest coll)
>>
>> Cheers,
>> Jeff
>>
>> On Feb 10, 5:18 pm, Jeff Rose  wrote:
>> 
>>> Hi,
>>>   Is there a built-in function that will return thefirstitemin a
>>> collection that matches a predicate?  (Something equivalent to Ruby's
>>> Enumerable#find...)  Seems pretty basic, but I can'tfindit in the  
>>> docs.
>>>
>>> Thanks,
>>> Jeff
>>>
>>>   
>
> - Mark Fredrickson
> mark.m.fredrick...@gmail.com
> http://www.markmfredrickson.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
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
-~--~~~~--~~--~--~---



Re: find first match in a sequence

2009-02-10 Thread Stuart Sierra

On Feb 10, 11:18 am, Jeff Rose  wrote:
>   Is there a built-in function that will return the first item in a
> collection that matches a predicate?  (Something equivalent to Ruby's
> Enumerable#find...)  Seems pretty basic, but I can't find it in the docs.

Hi, Jeff, here's how I do it:

user> (def numbers [2 4 6 7 8 9])
#'user/numbers
user> (some #(when (odd? %) %) numbers)
7

-Stuart Sierra
--~--~-~--~~~---~--~~
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
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
-~--~~~~--~~--~--~---



Re: find first match in a sequence

2009-02-10 Thread Mark Fredrickson

Filter is lazy:

http://clojure.org/api#toc228

So you can implement find-first as (first (filter pred coll))

-M

On Feb 10, 2009, at 10:19 AM, Jeff Rose wrote:

>
> Well, in case someone else needs the same function and it isn't built-
> in, here's what I'm using in the meantime.  (Based off of the some
> function that comes in core...)
>
> (defn find-first [pred coll]
>  (when (seq coll)
>(if (pred (first coll))
>  (first coll)
>  (recur pred (rest coll)
>
> Cheers,
> Jeff
>
> On Feb 10, 5:18 pm, Jeff Rose  wrote:
>> Hi,
>>   Is there a built-in function that will return thefirstitemin a
>> collection that matches a predicate?  (Something equivalent to Ruby's
>> Enumerable#find...)  Seems pretty basic, but I can'tfindit in the  
>> docs.
>>
>> Thanks,
>> Jeff
> >

- Mark Fredrickson
mark.m.fredrick...@gmail.com
http://www.markmfredrickson.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
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
-~--~~~~--~~--~--~---



Re: find first match in a sequence

2009-02-10 Thread Jeff Rose

Well, in case someone else needs the same function and it isn't built-
in, here's what I'm using in the meantime.  (Based off of the some
function that comes in core...)

(defn find-first [pred coll]
  (when (seq coll)
(if (pred (first coll))
  (first coll)
  (recur pred (rest coll)

Cheers,
Jeff

On Feb 10, 5:18 pm, Jeff Rose  wrote:
> Hi,
>   Is there a built-in function that will return thefirstitemin a
> collection that matches a predicate?  (Something equivalent to Ruby's
> Enumerable#find...)  Seems pretty basic, but I can'tfindit in the docs.
>
> Thanks,
> Jeff
--~--~-~--~~~---~--~~
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
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
-~--~~~~--~~--~--~---



find first match in a sequence

2009-02-10 Thread Jeff Rose

Hi,
  Is there a built-in function that will return the first item in a 
collection that matches a predicate?  (Something equivalent to Ruby's 
Enumerable#find...)  Seems pretty basic, but I can't find it in the docs.

Thanks,
Jeff

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