[RCR] Array.step

A

ara.t.howard

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]





-a
 
L

Logan Capaldo

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
 
L

Logan Capaldo

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?
 
A

ara.t.howard

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

you'll have to write almost as many lines as i did to make all of it work
though, still - i had forgotten about that:



harp:~ > cat a.rb
require 'enumerator'
class Array
def self.step(i, *a, &block)
b = block || lambda { |x| x }
i.to_enum:)step, *a).map(&b)
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)
==>[] # <- this one's wrong
Array.step(1,5,2)
==>[1, 3, 5]
Array.step(-1,-5,-2)
==>[-1, -3, -5]
Array.step 2
ArgumentError: wrong number of arguments
from a.rb:17:in `step'
from a.rb:17:in `step'
from (irb):1
Array.step -2
ArgumentError: wrong number of arguments
from a.rb:17:in `step'
from a.rb:17:in `step'
from (irb):1
Array.step(0,7){|i| 2 ** i}
==>[1, 2, 4, 8, 16, 32, 64, 128]
even = Array.step(9){|i| i.modulo(2).zero?}
ArgumentError: wrong number of arguments
from a.rb:17:in `step'
from a.rb:17:in `step'
from (irb):1


regards.


-a
 
A

ara.t.howard

What's wrong with the block form of Array.new?

Array.new(5) { |i| i }
Array.new(5) { |i| -i }
Array.new(3) { |i| 1+2*i }
Array.new(3) { |i| -(1+2*i) }
Array.new(3) { |i| i }
Array.new(3) { |i| -i }
Array.new(8){ |i| 2 ** i }
Array.new(10){ |i| i.modulo(2).zero? }

nothing. just verbosity and brain power requirements ;-)

-a
 
L

Logan Capaldo

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

Trans

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

ara.t.howard

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.

regards.

-a
 
L

Logan Capaldo

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

nobu

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]
 
L

Logan Capaldo

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]
 
A

ara.t.howard

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]

indeed and agreed.

-a
 
M

Martin DeMello

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?}'

I think this would be a perfect use for the new automatic enumerators,
to avoid passing two opaque parameters to a method:

# to generate an array
5.upto(10).step(2).to_a

# or the other way around
5.step(2).upto(10).to_a

# or if you'd rather use a range
(5..10).step(2).to_a

# to step over an existing array
ary.step(3)
ary[0..5].step(2)

etc.

martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top