Re: Lazy sequence - how they work internally

2014-04-08 Thread A. Webb
On Tuesday, April 8, 2014 7:57:10 AM UTC-5, sorin cristea wrote: > > > What exactly you mean by '*The point was you aren't using lazy-seq as > intended here since you are always creating a singleton sequence*' ? In > my sum function...I intend to compute sum of elements of a collection. > L

Re: Lazy sequence - how they work internally

2014-04-08 Thread sorin cristea
Hi Webb, What exactly you mean by '*The point was you aren't using lazy-seq as intended here since you are always creating a singleton sequence*' ? In my sum function: ( defn test-fc "sum of all collection elements using recursion and laziness" [coll] (letfn [(sum-fc [sum coll]

Re: Lazy sequence - how they work internally

2014-04-07 Thread sorin cristea
Hi James, I'm new to clojure and maybe for this reason it's possible to put some 'stupid' questions, I came for Java so for me it's normal when I call a fc/method to execute the body of that fc/method and return a result; this is the reason for why I expect a result when I call (test-fc (ran

Re: Lazy sequence - how they work internally

2014-04-07 Thread A. Webb
The point was you aren't using lazy-seq as intended here since you are always creating a singleton sequence. What's going on behind the scenes here is in effect just trampolining thunks. (defn thunked-sum [sum coll] (if-let [[x & more] (seq coll)] (fn [] (thunked-sum (+ sum x) more))

Re: Lazy sequence - how they work internally

2014-04-07 Thread James Reeves
Why do you expect (test-fc (range 210432423543654675765876879)) to return a result? Even if each iteration of the loop takes only 1 nanosecond, your function would take 6 billion years to complete. - James On 7 April 2014 21:01, sorin cristea wrote: > > Hi Gianluca, > > I have a question ; w

Re: Lazy sequence - how they work internally

2014-04-07 Thread sorin cristea
Hi Gianluca, I have a question ; why when a run/execute command/code line (test-fc (range 210432423543654675765876879)) it's not executed the function test-fc and return the sum for all 210432423543654675765876879 elements? why should I put the test-fc reference to a variable, x, like you pr

Re: Lazy sequence - how they work internally

2014-04-07 Thread sorin cristea
Hi Ginaluca, I have a question ; why when a run/execute command/code line (test-fc (range 210432423543654675765876879)) it's not executed the function test-fc and return the sum for all 210432423543654675765876879 elements? why should I put the test-fc reference to a variable, x, like you p

Re: Lazy sequence - how they work internally

2014-04-06 Thread gianluca torta
Hi sorin, your function computes a sequence of just one element (the sum of the collection members), so I would say it is not a typical use of (lazy) seqs to see that the code is indeed lazy, you can try: (def x (test-fc (range 210432423543654675765876879))) and see that it returns immediately

Re: Lazy sequence - how they work internally

2014-04-06 Thread Jozef Wagner
First have a look at delay (and clojure.lang.Delay) and try to understand it. LazySeq is just a delay with a fancy interface (supporting seq, first and rest). There is no recursion per se in the LazySeq object. What makes it harder to understand is the idiom of using lazy-seq together with cons and

Lazy sequence - how they work internally

2014-04-06 Thread sorin cristea
Hi, maybe this question was already put it here, but can someone explain how exactly work internal a function wrapped with lazy-seq keyword. For example in the below code sample: ( defn test-fc "sum of all collection elements using recursion and laziness" [coll] (letfn [(sum-fc [sum