[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[RCR] Array.step

Ara.T.Howard

8/30/2006 5:12:00 PM

13 Answers

Logan Capaldo

8/30/2006 5:43:00 PM

0


On Aug 30, 2006, at 1:12 PM, ara.t.howard@noaa.gov wrote:

>
>
>
> harp:~ > cat a.rb
> class Array
> def self.step i, *a, &b
> j, s, ignored = *a
> i, j = 0, i if j.nil?
> s ||= (j < i ? -1 : 1)
> list = new
> i.step(j,s){|k| list << k}
> list.map! &b if b
> list
> end
> end
>

require 'enumerator'
class Array
def self.step(i, *a, &block)
b = block || lambda { |x| x }
i.to_enum(:step, *a).map(&b)
end
end



Logan Capaldo

8/30/2006 5:51:00 PM

0


On Aug 30, 2006, at 1:43 PM, Logan Capaldo wrote:

>
> On Aug 30, 2006, at 1:12 PM, ara.t.howard@noaa.gov wrote:
>
>>
>>
>>
>> harp:~ > cat a.rb
>> class Array
>> def self.step i, *a, &b
>> j, s, ignored = *a
>> i, j = 0, i if j.nil?
>> s ||= (j < i ? -1 : 1)
>> list = new
>> i.step(j,s){|k| list << k}
>> list.map! &b if b
>> list
>> end
>> end
>>
>
Oops, minor addition follows:

> require 'enumerator'
> class Array
> def self.step(i, *a, &block)
> b = block || lambda { |x| x }
+ i, a[0] = 0, i if a.empty?
> i.to_enum(:step, *a).map(&b)
> end
> end
>
>


>


Ara.T.Howard

8/30/2006 5:53:00 PM

0

Ara.T.Howard

8/30/2006 5:54:00 PM

0

Logan Capaldo

8/30/2006 6:02:00 PM

0


On Aug 30, 2006, at 1:52 PM, ara.t.howard@noaa.gov wrote:

> Array.step(0,-4)
> ==>[] # <- this one's wrong

I'd almost rather make the RCR for changing step rather than adding
Array.step. It's more general, and with 1.9 magic iterators I think
it would obviate the need to even have Array.step, e.g.: 1.step
(3).to_a. Make n.step { ... } work as well and make it walk backwards
if its from high to low.

Trans

8/30/2006 6:02:00 PM

0


ara.t.howard@noaa.gov wrote:
> harp:~ > cat a.rb
> class Array
> def self.step i, *a, &b
> j, s, ignored = *a
> i, j = 0, i if j.nil?
> s ||= (j < i ? -1 : 1)
> list = new
> i.step(j,s){|k| list << k}
> list.map! &b if b
> list
> end
> end
>
> require 'irb/xmp'
>
> xmp 'Array.step(0,4)'
> xmp 'Array.step(0,-4)'
>
> xmp 'Array.step(1,5,2)'
> xmp 'Array.step(-1,-5,-2)'
>
> xmp 'Array.step 2'
> xmp 'Array.step -2'
>
> xmp 'Array.step(0,7){|i| 2 ** i}'
> xmp 'even = Array.step(9){|i| i.modulo(2).zero?}'
>
>
>
> harp:~ > ruby a.rb
> Array.step(0,4)
> ==>[0, 1, 2, 3, 4]
> Array.step(0,-4)
> ==>[0, -1, -2, -3, -4]
> Array.step(1,5,2)
> ==>[1, 3, 5]
> Array.step(-1,-5,-2)
> ==>[-1, -3, -5]
> Array.step 2
> ==>[0, 1, 2]
> Array.step -2
> ==>[0, -1, -2]
> Array.step(0,7){|i| 2 ** i}
> ==>[1, 2, 4, 8, 16, 32, 64, 128]
> even = Array.step(9){|i| i.modulo(2).zero?}
> ==>[true, false, true, false, true, false, true, false, true, false]

I like it. Although also:

require 'facet/interval'

Interval.new(0,4).to_a => [ 0, 1, 2, 3, 4]
Interval.new(0,4).to_a(2) => [ 0, 2, 4 ]

etc. And it has many other applications.

T.

Ara.T.Howard

8/30/2006 9:45:00 PM

0

Logan Capaldo

8/30/2006 9:59:00 PM

0


On Aug 30, 2006, at 5:45 PM, ara.t.howard@noaa.gov wrote:

> On Thu, 31 Aug 2006, Logan Capaldo wrote:
>
>>
>> On Aug 30, 2006, at 1:52 PM, ara.t.howard@noaa.gov wrote:
>>
>>> Array.step(0,-4)
>>> ==>[] # <- this one's wrong
>>
>> I'd almost rather make the RCR for changing step rather than
>> adding Array.step. It's more general, and with 1.9 magic iterators
>> I think it would obviate the need to even have Array.step, e.g.:
>> 1.step(3).to_a. Make n.step { ... } work as well and make it walk
>> backwards if its from high to low.
>
> indeed. just to be clear, however, the RCR is for Array.step not
> the exact
> impl. the reason i wrote the one i did is because it can be
> translated to C
> easily.
>
Well I don't really care persay about the implementation (I was just
having fun with the golf), just the semantics. I still think this RCR
would be better served as an addition of these capabilities to
Numeric#step, and then if desired also an Array.step. That's just my
opinion though.

> regards.
>
> -a
> --
> what science finds to be nonexistent, we must accept as
> nonexistent; but what
> science merely does not find is a completely different matter... it
> is quite
> clear that there are many, many mysterious things.
> - h.h. the 14th dalai lama
>


Nobuyoshi Nakada

8/31/2006 2:23:00 AM

0

Hi,

At Thu, 31 Aug 2006 03:02:14 +0900,
Logan Capaldo wrote in [ruby-talk:211561]:
> I'd almost rather make the RCR for changing step rather than adding
> Array.step. It's more general, and with 1.9 magic iterators I think
> it would obviate the need to even have Array.step, e.g.: 1.step
> (3).to_a. Make n.step { ... } work as well and make it walk backwards
> if its from high to low.

Do you mean this?

$ ruby -v -e 'p 1.step(5,2).to_a'
ruby 1.9.0 (2006-08-29) [i686-linux]
[1, 3, 5]

--
Nobu Nakada

Logan Capaldo

8/31/2006 2:32:00 AM

0


On Aug 30, 2006, at 10:22 PM, nobu@ruby-lang.org wrote:

> Hi,
>
> At Thu, 31 Aug 2006 03:02:14 +0900,
> Logan Capaldo wrote in [ruby-talk:211561]:
>> I'd almost rather make the RCR for changing step rather than adding
>> Array.step. It's more general, and with 1.9 magic iterators I think
>> it would obviate the need to even have Array.step, e.g.: 1.step
>> (3).to_a. Make n.step { ... } work as well and make it walk backwards
>> if its from high to low.
>
> Do you mean this?
>
> $ ruby -v -e 'p 1.step(5,2).to_a'
> ruby 1.9.0 (2006-08-29) [i686-linux]
> [1, 3, 5]
>
Pretty much, except possibly supporting Ara's additional requirements
of 9.step.to_a and 1.step(-2).to_a #=> [1, 0, -1, -2]

> --
> Nobu Nakada
>