Re: Need help understanding a lazy sequence function

2014-03-10 Thread Gary Trakhman
If you haven't seen the impl yet, it's relatively small and simple:
https://github.com/clojure/clojure/blob/master/src/clj/clojure/core.clj#L642
https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/LazySeq.java

Chunked seqs are more complex, but they're basically a performance
optimization.


On Mon, Mar 10, 2014 at 6:43 PM, Frank Behrens  wrote:

> Hello,
>
> I'm trying to understand the lazyness, how they work, how to create them,
> how to avoid pre-realisation.
>
> Can someone point me to which documentation would be helpful, where do I
> find it ?
>
> Frank
>
> Am Montag, 10. März 2014 13:16:00 UTC+1 schrieb Asfand Yar Qazi:
>
>> On Monday, 10 March 2014 11:35:30 UTC, Alan Forrester wrote:
>>>
>>> According to the documentation for map
>>> http://clojuredocs.org/clojure_core/clojure.core/map
>>> (map + x y)
>>>
>>> where x and y are two collections adds the first element of x to the
>>> first element of y, the second element of x to the second element of y
>>> and so on until either x or y is exhausted.
>>>
>>
>> OK I feel like an idiot - I was going by what I picked up from Clojure
>> Programming, and didn't read the official API docs, sorry.  The "until
>> either x or y is exhausted" bit is what was the missing piece of the puzzle.
>>
>>
>>> You seem to be trying to imagine how lazy-seqs work rather than
>>> reading the documentation, which tells you how they behave when you
>>> run a program or type an expression into the REPL.
>>>
>>
>> I will take your advice on-board.
>>
>> Many 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
> ---
> 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: Need help understanding a lazy sequence function

2014-03-10 Thread Frank Behrens
Hello,

I'm trying to understand the lazyness, how they work, how to create them, 
how to avoid pre-realisation.

Can someone point me to which documentation would be helpful, where do I 
find it ?

Frank

Am Montag, 10. März 2014 13:16:00 UTC+1 schrieb Asfand Yar Qazi:
>
> On Monday, 10 March 2014 11:35:30 UTC, Alan Forrester wrote:
>>
>> According to the documentation for map 
>> http://clojuredocs.org/clojure_core/clojure.core/map 
>> (map + x y) 
>>
>> where x and y are two collections adds the first element of x to the 
>> first element of y, the second element of x to the second element of y 
>> and so on until either x or y is exhausted. 
>>
>
> OK I feel like an idiot - I was going by what I picked up from Clojure 
> Programming, and didn't read the official API docs, sorry.  The "until 
> either x or y is exhausted" bit is what was the missing piece of the puzzle.
>  
>
>> You seem to be trying to imagine how lazy-seqs work rather than 
>> reading the documentation, which tells you how they behave when you 
>> run a program or type an expression into the REPL.
>>
>
> I will take your advice on-board.
>
> Many 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
--- 
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: Need help understanding a lazy sequence function

2014-03-10 Thread Asfand Yar Qazi
On Monday, 10 March 2014 11:35:30 UTC, Alan Forrester wrote:
>
> According to the documentation for map 
> http://clojuredocs.org/clojure_core/clojure.core/map 
> (map + x y) 
>
> where x and y are two collections adds the first element of x to the 
> first element of y, the second element of x to the second element of y 
> and so on until either x or y is exhausted. 
>

OK I feel like an idiot - I was going by what I picked up from Clojure 
Programming, and didn't read the official API docs, sorry.  The "until 
either x or y is exhausted" bit is what was the missing piece of the puzzle.
 

> You seem to be trying to imagine how lazy-seqs work rather than 
> reading the documentation, which tells you how they behave when you 
> run a program or type an expression into the REPL.
>

I will take your advice on-board.

Many 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
--- 
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: Need help understanding a lazy sequence function

2014-03-10 Thread Alan Forrester
On 10 March 2014 10:26, Asfand Yar Qazi  wrote:
> Hi,
>
> On Sunday, 9 March 2014 14:58:47 UTC, Atamert Ölçgen wrote:
>>
>> Hello,
>>
>> (take 1 fib-seq) => (1)
>>
>>
>> Which can also be seen as[*] (map + (0) (1))
>>
>> (map + '(0) '(1)) => (1)
>>
>>
>> Makes sense?
>>
>
> I'm afraid it still doesn't make sense; I still don't understand how (cons 0
> (cons 0 fib-seq)) evaluates to '(0) the first time; it should evaluate to
> '(0 0) since there are 2 cons statements.  That's the point I'm stuck on;
> any help would be gratefully received.

The code is this:

(def fib-seq
  (lazy-seq
(map +
  (cons 0 (cons 0 fib-seq))
  (cons 1 fib-seq

According to the documentation for map
http://clojuredocs.org/clojure_core/clojure.core/map
(map + x y)

where x and y are two collections adds the first element of x to the
first element of y, the second element of x to the second element of y
and so on until either x or y is exhausted.

The documentation for cons states that (cons x seq) returns a new
collection with x as the first element and seq as the rest so (cons 0
[1 2 3 4]) is '(0 1 2 3 4) and  (cons 0 (cons 0 [1 2 3 4])) is the
same as (cons 0 [0 1 2 3 4]) which gives '(0 0 1 2 3 4).

So the expression

(cons 0 (cons 0 fib-seq))

is the same as fib-seq with two zeros in front. It is not the same as
fib-seq with '(0 0) in front.

When the map maps over (cons 0 (cons 0 fib-seq)) and takes its first
element, it finds that the first element is 0, not '(0 0).

You seem to be trying to imagine how lazy-seqs work rather than
reading the documentation, which tells you how they behave when you
run a program or type an expression into the REPL. This is a bad idea
since it conflates the implementation with the behaviour. The
behaviour is in the documentation. If you want to know how functional
data structures work there are books about this kind of thing like
"Purely Functional Data Structures" by Okasaki.

Alan

-- 
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: Need help understanding a lazy sequence function

2014-03-10 Thread Asfand Yar Qazi
Hi,

On Sunday, 9 March 2014 14:58:47 UTC, Atamert Ölçgen wrote:
>
> Hello,
>
> (take 1 fib-seq) => (1)
>
>
> Which can also be seen as[*] (map + (0) (1))
>
> (map + '(0) '(1)) => (1)
>
>
> Makes sense?
>
>
I'm afraid it still doesn't make sense; I still don't understand how (cons 
0 (cons 0 fib-seq)) evaluates to '(0) the first time; it should evaluate to 
'(0 0) since there are 2 cons statements.  That's the point I'm stuck on; 
any help would be gratefully received.





On Sun, Mar 9, 2014 at 12:54 PM, Asfand Yar Qazi 
> > wrote:
>
>> Hi,
>>
>> I'm trying to understand the following function (from 
>> http://en.wikibooks.org/wiki/Clojure_Programming/Examples/Lazy_Fibonacci#Self-Referential_Version
>> ):
>>
>> (def fib-seq
>>   (lazy-seq
>> (map +
>>   (cons 0 (cons 0 fib-seq))
>>   (cons 1 fib-seq
>>
>> I'm trying to understand how this works.  In particular, I do not 
>> understand what the recursive call to fib-seq will return when the sequence 
>> is lazily evaluated.
>>
>> Here's my understanding so far:
>>
>> The first time fib-seq is invoked, it has no head, and the function is 
>> the tail.  So we go into the first collection, where we append 0 and 0 to 
>> fib-seq, which then '(0 0) .  This is then mapped with the second 
>> collection, which (because fib-seq has not returned anything yet) is '(1) . 
>>  Shouldn't map then raise an error because it is effectively being called 
>> as (map + (0 0) (1)) ?
>>
>> I would be very grateful for any insights.
>>
>> Thanks
>>
>>
>>  -- 
>> 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/d/optout.
>>
>
>
>
> -- 
> Kind Regards,
> Atamert Ölçgen
>
> -+-
> --+
> +++
>
> www.muhuk.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/d/optout.


Re: Need help understanding a lazy sequence function

2014-03-09 Thread Atamert Ölçgen
Hello,

(take 1 fib-seq) => (1)


Which can also be seen as[*] (map + (0) (1))

(map + '(0) '(1)) => (1)


Makes sense?

(take 2 fib-seq) => (1 1)


Here the recursive definition (note that it's not a function, fib-seq
simply a Var that holds a LazySeq object) comes into play. We already know
the first element in the sequence, so:

(map + '(0 0) '(1 1)) => (1 1)


The second 1 there is the first element of the fib-seq.


*: I'm using vague language intentionally here. Anyone who knows the right
term please correct me.


On Sun, Mar 9, 2014 at 12:54 PM, Asfand Yar Qazi  wrote:

> Hi,
>
> I'm trying to understand the following function (from
> http://en.wikibooks.org/wiki/Clojure_Programming/Examples/Lazy_Fibonacci#Self-Referential_Version
> ):
>
> (def fib-seq
>   (lazy-seq
> (map +
>   (cons 0 (cons 0 fib-seq))
>   (cons 1 fib-seq
>
> I'm trying to understand how this works.  In particular, I do not
> understand what the recursive call to fib-seq will return when the sequence
> is lazily evaluated.
>
> Here's my understanding so far:
>
> The first time fib-seq is invoked, it has no head, and the function is the
> tail.  So we go into the first collection, where we append 0 and 0 to
> fib-seq, which then '(0 0) .  This is then mapped with the second
> collection, which (because fib-seq has not returned anything yet) is '(1) .
>  Shouldn't map then raise an error because it is effectively being called
> as (map + (0 0) (1)) ?
>
> I would be very grateful for any insights.
>
> 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
> ---
> 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.
>



-- 
Kind Regards,
Atamert Ölçgen

-+-
--+
+++

www.muhuk.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/d/optout.


Need help understanding a lazy sequence function

2014-03-09 Thread Asfand Yar Qazi
Hi,

I'm trying to understand the following function (from 
http://en.wikibooks.org/wiki/Clojure_Programming/Examples/Lazy_Fibonacci#Self-Referential_Version):

(def fib-seq
  (lazy-seq
(map +
  (cons 0 (cons 0 fib-seq))
  (cons 1 fib-seq

I'm trying to understand how this works.  In particular, I do not 
understand what the recursive call to fib-seq will return when the sequence 
is lazily evaluated.

Here's my understanding so far:

The first time fib-seq is invoked, it has no head, and the function is the 
tail.  So we go into the first collection, where we append 0 and 0 to 
fib-seq, which then '(0 0) .  This is then mapped with the second 
collection, which (because fib-seq has not returned anything yet) is '(1) . 
 Shouldn't map then raise an error because it is effectively being called 
as (map + (0 0) (1)) ?

I would be very grateful for any insights.

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