Omg get over yourself you have taken what i said completely the wrong way..
What arrogant plug was that?... Lets take your code and look at it shall we... 30+ lines for no reason other than "Hey look at me, i can write a function/class in 30 lines that only needs to be 5." Chill out dude, i wasn't knocking your code, it was just OTT. You can continue to think you are right which is arrogance in itself... there is more than one way to skin a cat. TJ's/My loop that took all of 30 seconds to create is faster no doubt, has less weight and does the job. I personaly have better things to do than write some amazing class to achieve what can be done in 30 seconds - evidently you do not so good luck with that. Alex Mcauley http://www.thevacancymarket.com ----- Original Message ----- From: "Matt Foster" <[email protected]> To: "Prototype & script.aculo.us" <[email protected]> Sent: Friday, September 25, 2009 9:16 PM Subject: [Proto-Scripty] Re: Range utility increment > That only gives you even or odd increments to the top number... Incorrect, notice the constructor method? It ensures that the number given is even such that you'll never get a range of odd numbers. Granted the last line of the Jojo's question was > Let's say count up by 2's: [2,4,6,8,10]. How do you do this? Hence why I went with this specific implementation, to demonstrate how you can create classes that the ObjectRange class can use to create proper ranges. I didn't feel it was necessary to abstract out the class such that it covers all bases but demonstrate how to do it one way and then Jojo could modify for their own purposes. > Better to go with my/TJ's loop What an arrogant plug of your own spaghetti code and hasty discredit to my contribution. Exposing your shortfalls isn't difficult, you aren't creating a range object, completely bypassing the ObjectRange's functionality and just simply creating a hacked out array. So to put the nail in the coffin, here is the classes abstracted out var IncrementNumber = Class.create({ initialize : function(num, increment){ this.num = num; this.increment = increment; }, succ : function(){ return this.clone(this.num + this.increment, this.increment); }, clone : function(num, increment){ return new IncrementNumber(num, increment); }, toString : function(){ return this.num; } }); var EvenNumber = Class.create(IncrementNumber, { initialize : function($super, num){ $super(num, 2); this.num = (num % 2 == 1) ? num - 1 : num; }, clone : function(num, increment){ return new EvenNumber(num); } }); var NumberRange = Class.create(ObjectRange, { _each: function(iterator) { var value = this.start; while (this.include(value.num)) { iterator(value.num); value = value.succ(); } }, }); var bottom = new IncrementNumber(-10, 2); //var bottom = new EvenNumber(-20); var top = new EvenNumber(25); var range = $A(new NumberRange(bottom, top)); console.log(range); nothing gets me fired up like a flame... On Sep 25, 2:25 pm, "Alex McAuley" <[email protected]> wrote: > That only gives you even or odd increments to the top number... > > WHat if you wanted every 5th number ... > > Better to go with my/TJ's loop > > Alex Mcauleyhttp://www.thevacancymarket.com > > ----- Original Message ----- > From: "Matt Foster" <[email protected]> > To: "Prototype & script.aculo.us" > <[email protected]> > Sent: Friday, September 25, 2009 5:57 PM > Subject: [Proto-Scripty] Re: Range utility increment > > I'd just use an 'iterator' class to do this... > > var EvenNumber = Class.create( > { > initialize : function(num){ > this.num = (num % 2 == 1) ? num - 1 : num; > }, > succ : function(){ > return new EvenNumber(this.num + 2); > }, > toString : function(){ > return this.num; > } > }); > > var bottom = new EvenNumber(1); > > var top = new EvenNumber(21); > var range = $A($R(bottom, top)); > > console.log(range); > > I had done this to a much greater extent in my date range selector > gadget. Where it became the master range, held a range of calendar > objects, which were in themselves ranges of date objects... fun stuff. > > https://positionabsolute.net/blog/2008/01/google-calendar-date-range-... > > ps just looking over that, should rename that class to > "PositiveEvenNumber", only going to work one way, but you get the idea > > On Sep 25, 4:15 am, "Alex McAuley" <[email protected]> > wrote: > > function everyOther(start, end, increment) { > > var n; > > var rv = []; > > for (n = start; n <= end; n += increment) { > > rv[rv.length] = n; > > } > > return rv; > > } > > > Just a mod to make it better to be able to change it to 3's, 4's, 5's > > > ;) > > > Alex Mcauleyhttp://www.thevacancymarket.com > > > ----- Original Message ----- > > From: "T.J. Crowder" <[email protected]> > > To: "Prototype & script.aculo.us" > > <[email protected]> > > Sent: Friday, September 25, 2009 8:29 AM > > Subject: [Proto-Scripty] Re: Range utility increment > > > Hi, > > > > I'm looking for a way to specify that I want to increment by any > > > value. Let's say count up by 2's: [2,4,6,8,10]. How do you do this? > > > ObjectRange itself is for ranges of consecutive values, but you can > > get an array of such values by applying Enumerable#collect to a range: > > > var twos; > > twos = $R(1, 5).collect(function(x) { return x * 2; }); > > > ...although frankly I'd probably go with a straight loop like Alex did > > (although a slightly different one): > > > function everyOther(start, end) { > > var n; > > var rv = []; > > for (n = start; n <= end; n += 2) { > > rv[rv.length] = n; > > } > > return rv; > > } > > > FWIW, > > -- > > T.J. Crowder > > tj / crowder software / comwww.crowdersoftware.com > > > On Sep 24, 11:15 pm, JoJo <[email protected]> wrote: > > >http://www.prototypejs.org/api/utility/dollar-r > > > > From the documentation of the Range utility, it seems like it can only > > > increment by 1's. For example, $A($R(1,10,true)) gives you: > > > [1,2,3,4,5,6,7,8,9,10]. > > > > I'm looking for a way to specify that I want to increment by any > > > value. Let's say count up by 2's: [2,4,6,8,10]. How do you do this? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~----------~----~----~----~------~----~------~--~---
