Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #30546

Re: Layered object

Newsgroups comp.lang.javascript
Date 2016-05-25 17:13 -0700
References <89e1dd41-b971-4f04-aad2-e726abe20d56@googlegroups.com> <3aa6bb6e-c513-4228-ada6-095c58d67022@googlegroups.com> <0984346c-891a-4f03-96c5-c983f96aeb1d@googlegroups.com> <1426456b-46cd-4a18-8d3b-1447508dd3e6@googlegroups.com> <f1336942-b7c2-4cfa-8b28-bf1f4b62a918@googlegroups.com>
Message-ID <aa4d7adc-33fb-4976-8400-6c1941a083bc@googlegroups.com> (permalink)
Subject Re: Layered object
From "Michael Haufe (TNO)" <tno@thenewobjective.com>

Show all headers | View raw


On Monday, May 23, 2016 at 12:46:12 AM UTC-5, jonas.t...@gmail.com wrote:
> Den måndag 23 maj 2016 kl. 00:57:57 UTC+2 skrev Michael Haufe (TNO):
> > On Sunday, May 22, 2016 at 4:59:50 PM UTC-5, jonas.t...@gmail.com wrote:
> > > Den söndag 22 maj 2016 kl. 18:03:24 UTC+2 skrev Michael Haufe (TNO):
> > > > On Saturday, May 21, 2016 at 12:41:36 PM UTC-5, jonas.t...@gmail.com wrote:
> > > > > I have an object storing integers, many formulas may of course generate the same integer. And for some expressions for example on form a*x^j i would like to store each occurence. Now all my numbers are printed out in a square grid.
> > > > 
> > > > Don't. It's going to be slower.
> > > 
> > > Well just storing them within the array can hardly slow it down or?
> > 
> > Memoization isn't free and your approach would be even less so. I've written a little about this topic in the past:
> > 
> > <https://thenewobjective.com/blog/2013/01/dynamic-programming-for-great-justice/>
> 
> Beside for the last example, what is really the benefit of the recursive examples compared to the iterative?

From the standpoint of speed and memory? They're equivalent as I explained:

O(n)/O(1) time/space

From the standpoint of brevity, the recursive form is 3 times shorter. ES6 evolved since I wrote the original. Here is a more modern comparison:

//before
function fib(n){
    if(n < 2)   
        return n;

	var f0 = 0, f1 = 1, f;

    for(var i = 1; i < n; i++){
        f  = f0 + f1;
        f0 = f1;
        f1 = f;
    }
    return f;
}

//after
var fib = n => {
  let f = (n0,n1,step) => step == n ? n1 : f(n1, n0 + n1, step + 1)
  return f(0,1,1)
}


> For me that is not used to recursive calls the iterative naive approach using for and while is much easier to read and understand.

Easy != Simple. Easy just means familiar. With practice you can understand recursion just as well

> What is the main benefit with recursive calls, speed?

The main benefit IMO is the ability to write concise algorithms that solve a problem in terms of itself.

Back to comp.lang.javascript | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Layered object jonas.thornvall@gmail.com - 2016-05-21 10:41 -0700
  Re: Layered object Aleksandro <aleksandro@gmx.com> - 2016-05-21 19:41 -0400
    Re: Layered object jonas.thornvall@gmail.com - 2016-05-21 23:42 -0700
      Re: Layered object jonas.thornvall@gmail.com - 2016-05-22 03:50 -0700
  Re: Layered object "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-05-22 09:03 -0700
    Re: Layered object jonas.thornvall@gmail.com - 2016-05-22 14:59 -0700
      Re: Layered object "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-05-22 15:57 -0700
        Re: Layered object jonas.thornvall@gmail.com - 2016-05-22 22:46 -0700
          Re: Layered object "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-05-25 17:13 -0700
        Re: Layered object jonas.thornvall@gmail.com - 2016-05-22 23:05 -0700
          Re: Layered object "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-05-25 17:15 -0700
        Re: Layered object jonas.thornvall@gmail.com - 2016-05-22 23:08 -0700
        Re: Layered object jonas.thornvall@gmail.com - 2016-05-23 08:31 -0700
          Re: Layered object jonas.thornvall@gmail.com - 2016-05-24 05:38 -0700
            Re: Layered object "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-05-25 17:22 -0700
              Re: Layered object jonas.thornvall@gmail.com - 2016-05-25 22:56 -0700
                Re: Layered object jonas.thornvall@gmail.com - 2016-05-25 23:07 -0700
                Re: Layered object jonas.thornvall@gmail.com - 2016-05-25 23:10 -0700
                Re: Layered object jonas.thornvall@gmail.com - 2016-05-25 23:24 -0700
                Re: Layered object jonas.thornvall@gmail.com - 2016-05-26 01:47 -0700
                Re: Layered object jonas.thornvall@gmail.com - 2016-05-26 01:53 -0700
                Re: Layered object jonas.thornvall@gmail.com - 2016-05-26 02:58 -0700
                Re: Layered object "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-05-26 06:42 -0700
                Re: Layered object Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-05-26 11:30 +0100
                Re: Layered object jonas.thornvall@gmail.com - 2016-05-26 03:47 -0700
                Re: Layered object Tim Streater <timstreater@greenbee.net> - 2016-05-26 12:43 +0100
                Re: Layered object Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-05-26 14:13 +0100
                Re: Layered object "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-05-26 07:05 -0700
                Re: Layered object jonas.thornvall@gmail.com - 2016-05-26 03:51 -0700
                Re: Layered object jonas.thornvall@gmail.com - 2016-05-26 04:00 -0700
                Re: Layered object Dr J R Stockton <reply1600@merlyn.demon.co.uk.invalid> - 2016-05-27 22:14 +0100
                Re: Layered object "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-05-26 06:40 -0700
          Re: Layered object "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-05-25 17:21 -0700

csiph-web