lazy sequence question

2009-12-09 Thread Mike K
I'm working my way through "Programming Clojure" and got an unexpected
result with sequences:

user> (take 10 (filter even? (iterate inc 1)))
(2 4 6 8 10 12 14 16 18 20)
user> (take-while #(< % 10) (iterate inc 1))
(1 2 3 4 5 6 7 8 9)
user> (take 10 (filter #(< % 10) (iterate inc 1)))
; Evaluation aborted.

The first two work but the third one hangs.  Why?

   Thanks,
   Mike

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


Lazy sequence question

2011-11-30 Thread Paweł Łoziński
Hi everybody,

I'd like to create a lazy sequence which has first element x and all
the rest from another lazy sequence. I couldn't find a suitable
function in the docs. Can somebody give a hint?

Best regards
PŁ

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


Re: lazy sequence question

2009-12-09 Thread Mike K
On Dec 9, 10:35 pm, Mike K  wrote:

> The first two work but the third one hangs.  Why?

user> (take 5 (filter #(< % 10) (iterate inc 1)))
(1 2 3 4 5)

OK, I figured out that it won't hang with taking <= 9 elements, which
is the total that pass the filter.

But shouldn't it give me 9 items without hanging when I ask for 10 or
more as in the first case?

   Mike

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


Re: lazy sequence question

2009-12-09 Thread Mark Engelberg
There are only 9 items that satisfy your predicate.  (take 10 ...)
demands a 10th, and it keeps searching the (iterate inc 1) stream
forever, endlessly searching for that 10th item it will never find.

On Wed, Dec 9, 2009 at 9:41 PM, Mike K  wrote:
> On Dec 9, 10:35 pm, Mike K  wrote:
>
>> The first two work but the third one hangs.  Why?
>
> user> (take 5 (filter #(< % 10) (iterate inc 1)))
> (1 2 3 4 5)
>
> OK, I figured out that it won't hang with taking <= 9 elements, which
> is the total that pass the filter.
>
> But shouldn't it give me 9 items without hanging when I ask for 10 or
> more as in the first case?
>
>   Mike
>
> --
> 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 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


Re: lazy sequence question

2009-12-09 Thread Richard Newman
> But shouldn't it give me 9 items without hanging when I ask for 10 or
> more as in the first case?

No.

take returns a lazy sequence. The printer is trying to realize it in  
order to print it. It can't be completely realized until it's taken  
ten elements (at which point it's done, by definition).

In realizing the sequence, the first nine items are collected. take  
wants one more. It tries to fetch it from the filtered lazy-seq.

The filtered seq gets 10 from the iterate lazy-seq, which doesn't pass  
the filter. It gets 11, which doesn't pass the filter...

Neither filter nor take know to abandon their attempt. That's how this  
works. Imagine if you wrote:

user=> (take 10 (filter #(or (= 1000 %) (< % 10)) (iterate inc 1)))
(1 2 3 4 5 6 7 8 9 1000)


It just keeps on going until it's found ten elements.

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


Re: lazy sequence question

2009-12-09 Thread Mike K
> Neither filter nor take know to abandon their attempt. That's how this  
> works.

Ah, of course.  Thanks Mark and Richard!

   Mike

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


Re: lazy sequence question

2009-12-09 Thread ataggart
On Dec 9, 9:46 pm, Mark Engelberg  wrote:
> There are only 9 items that satisfy your predicate.  (take 10 ...)
> demands a 10th, and it keeps searching the (iterate inc 1) stream
> forever, endlessly searching for that 10th item it will never find.


Aww.  You make it sound so sad.

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


Re: Lazy sequence question

2011-11-30 Thread Kasper Galschiot Markus

Is conf what you're looking for?

http://clojure.github.com/clojure/clojure.core-api.html#clojure.core/cons

~Kasper

On 11/30/11 2:27 PM, Paweł Łoziński wrote:

Hi everybody,

I'd like to create a lazy sequence which has first element x and all
the rest from another lazy sequence. I couldn't find a suitable
function in the docs. Can somebody give a hint?

Best regards
PŁ



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


Re: Lazy sequence question

2011-12-01 Thread Paweł Łoziński
On 30 Lis, 22:31, Kasper Galschiot Markus  wrote:
> Is conf what you're looking for?
>
> http://clojure.github.com/clojure/clojure.core-api.html#clojure.core/...
>
> ~Kasper

Of course. Thanks!

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