[RCR] Numeric#of

i threw this out before. thought i'd try again:

   harp:~ > irb
   irb(main):001:0> class Numeric; def of(&b) Array::new(self).map(&b) end; end
   => nil

   irb(main):002:0> 42.of{ String::new }
   => ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]

regards.

-a

···

--
happiness is not something ready-made. it comes from your own actions.
- h.h. the 14th dali lama

Hi,

···

On 2/1/06, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> wrote:

i threw this out before. thought i'd try again:

   harp:~ > irb
   irb(main):001:0> class Numeric; def of(&b) Array::new(self).map(&b) end; end
   => nil

   irb(main):002:0> 42.of{ String::new }
   => ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]

Using my not-yet-committed 1.9, you can type

  42.times.collect{String::new}

to get the same result.

matz.

class Fixnum
  def of
    (0...self).map{ yield }
  end
end

p 3.of { "" }

===> ["", "", ""]

···

ara.t.howard@noaa.gov wrote:

i threw this out before. thought i'd try again:

   harp:~ > irb
   irb(main):001:0> class Numeric; def of(&b) Array::new(self).map(&b) end; end
   => nil

   irb(main):002:0> 42.of{ String::new }
   => ["", "", "", "", "", "", "", "", "", "", . . .

--
future, n. That period of time in which our affairs prosper, our
friends are true, and our happiness is assured.
  --- Ambrose Bierce

Cool!

So does times still work with a block? I'm assuming that it switches
behavior, being a generator itself if a block is present, and creating
a generator object (which includes Enumerable) when no block is
provided.

Eg. (simplified):

  class Numeric
    def times( &blk )
      generator = # some code to create the generator
                  # that runs from 0 to self-1
      generator.each &blk if block_given?
      generator
    end
  end

If that's the case, this is awesome!

Jacob Fugal

···

On 1/31/06, Yukihiro Matsumoto <matz@ruby-lang.org> wrote:

On 2/1/06, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> wrote:
> harp:~ > irb
> irb(main):001:0> class Numeric; def of(&b) Array::new(self).map(&b) end; end
> => nil
>
> irb(main):002:0> 42.of{ String::new }
> => ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]

Using my not-yet-committed 1.9, you can type

  42.times.collect{String::new}

to get the same result.

great! i'm still partial to the shorter version since

   42.times.collect{String::new}

isn't that much shorter/prettier than my current idiom of

   Array::new(42).map{String::new}

but it's the capability that's needed so this is good news.

kind regards.

-a

···

On Wed, 1 Feb 2006, Yukihiro Matsumoto wrote:

Hi,

On 2/1/06, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> wrote:

i threw this out before. thought i'd try again:

   harp:~ > irb
   irb(main):001:0> class Numeric; def of(&b) Array::new(self).map(&b) end; end
   => nil

   irb(main):002:0> 42.of{ String::new }
   => ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]

Using my not-yet-committed 1.9, you can type

42.times.collect{String::new}

to get the same result.

--
happiness is not something ready-made. it comes from your own actions.
- h.h. the 14th dali lama

The coming dawn of Role-Oriented Progamming is upon us (such is the way
of the Functor).

All I did was

class Integer
  def times
    if block_given?
      (0...self).each { |i| yield i }
    else
      (0...self)
    end
  end
end

3.times.collect { String.new }
=> ["", "", ""]

  Array::new(42).map{String::new}

That can be shortened to:

>> Array.new(42) { String.new }
=> ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]

James Edward Gray II

···

On Jan 31, 2006, at 6:58 PM, ara.t.howard@noaa.gov wrote:

Hi,

> Using my not-yet-committed 1.9, you can type
>
> 42.times.collect{String::new}
>
> to get the same result.

So does times still work with a block?

Yes.

I'm assuming that it switches
behavior, being a generator itself if a block is present, and creating
a generator object (which includes Enumerable) when no block is
provided.

If returns a enumerator object just like other enumerable methods do
if no block is given to them.

matz.

···

On 2/1/06, Jacob Fugal <lukfugl@gmail.com> wrote:

On 1/31/06, Yukihiro Matsumoto <matz@ruby-lang.org> wrote:

Yeah, that's pretty much the same thing as my pseudo-code if you let
generator = (0...self) which, given the special implementation of
Fixnum ranges, is probably the best solution. Note that it Matz
probably did do it in Fixnum, not Numeric (How would you do something
Math::PI.times? :slight_smile: or Integer.

Jacob Fugal

···

On 1/31/06, Matthew Moss <matthew.moss.coder@gmail.com> wrote:

class Integer
  def times
    if block_given?
      (0...self).each { |i| yield i }
    else
      (0...self)
    end
  end
end

indeed. my bad.

none still as pretty as '42.of' though! :wink:

-a

···

On Wed, 1 Feb 2006, James Edward Gray II wrote:

On Jan 31, 2006, at 6:58 PM, ara.t.howard@noaa.gov wrote:

  Array::new(42).map{String::new}

That can be shortened to:

Array.new(42) { String.new }

=> ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]

--
happiness is not something ready-made. it comes from your own actions.
- h.h. the 14th dali lama

How deep does this rabbit hole go?

Does map return an enumerable without a block? And if so, can we type
obj.map.map.map... til the end of time?

···

On Feb 1, 2006, at 1:26 AM, Yukihiro Matsumoto wrote:

If returns a enumerator object just like other enumerable methods do
if no block is given to them.

matz.

Sweet! I've wanted this sort of enumerator generation for a while. Is
this new across the board in 1.9, or have I just been stupid in
assuming it didn't work when it has this whole time?

Jacob Fugal

···

On 1/31/06, Yukihiro Matsumoto <matz@ruby-lang.org> wrote:

If returns a enumerator object just like other enumerable methods do
if no block is given to them.

"James Edward Gray II" <james@grayproductions.net> wrote in message
news:798D769A-AE90-4EE6-8D6D-F9A7A89C0CFE@grayproductions.net...

···

On Jan 31, 2006, at 6:58 PM, ara.t.howard@noaa.gov wrote:

  Array::new(42).map{String::new}

That can be shortened to:

>> Array.new(42) { String.new }
=> ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", ""]

James Edward Gray II

Thank for these!!

I was looking for a clean (Ruby) way of creating an array of ascending
integer values, with the final value zero. IE [ 1, 2, 3, 4, 0 ]
i = 0; a = Array.new( 4 ) { i += 1 }.push 0
does what I want.

--
Phil
remove all of the (at)'s to send email

"James Edward Gray II" <james@grayproductions.net> wrote in message
news:798D769A-AE90-4EE6-8D6D-F9A7A89C0CFE@grayproductions.net...

···

On Jan 31, 2006, at 6:58 PM, ara.t.howard@noaa.gov wrote:

  Array::new(42).map{String::new}

That can be shortened to:

>> Array.new(42) { String.new }
=> ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", ""]

James Edward Gray II

Thank for these!!

I was looking for a clean (Ruby) way of creating an array of ascending
integer values, with the final value zero. IE [ 1, 2, 3, 4, 0 ]
i = 0; a = Array.new( 4 ) { i += 1 }.push 0
does what I want.

--
Phil
remove all of the (at)'s to send email

Hi,

How deep does this rabbit hole go?

As deep as you want, and the machine allows

Does map return an enumerable without a block? And if so, can we type
obj.map.map.map... til the end of time?

Yes.

matz.

···

On 2/1/06, Logan Capaldo <logancapaldo@gmail.com> wrote:

(1..4).to_a.push(0)

also Array.new passes in the index to the block, so you can do

Array.new(4) {|i| i+1}

martin

···

Phil Duby <newshoundat@atphriendly.atnet> wrote:

I was looking for a clean (Ruby) way of creating an array of ascending
integer values, with the final value zero. IE [ 1, 2, 3, 4, 0 ]
i = 0; a = Array.new( 4 ) { i += 1 }.push 0
does what I want.

Bah, too long!
  [""] * 42
Sorry, just had to jump in :slight_smile:
  .adam sanderson

a = (1..4).to_a << 0

-Levin

···

On 2/1/06, Phil Duby <newshoundat@atphriendly.atnet> wrote:

I was looking for a clean (Ruby) way of creating an array of ascending
integer values, with the final value zero. IE [ 1, 2, 3, 4, 0 ]
i = 0; a = Array.new( 4 ) { i += 1 }.push 0

> If returns a enumerator object just like other enumerable methods do
> if no block is given to them.

Sweet! I've wanted this sort of enumerator generation for a while. Is
this new across the board in 1.9, or have I just been stupid in
assuming it didn't work when it has this whole time?

It's been working for Enumerable (and similar) methods for a while (in 1.9):

···

On Thu, Feb 02, 2006 at 01:03:30AM +0900, Jacob Fugal wrote:

On 1/31/06, Yukihiro Matsumoto <matz@ruby-lang.org> wrote:

Fri Jul 15 00:11:36 2005 Nobuyoshi Nakada <nobu@ruby-lang.org>

  * enum.c (enumeratorize): create new enumerator for current method if
    no block is given.

  * enumerator.c: moved from ext/enumerator.

batsman@tux-chan:~/src/ruby/ruby.head$ grep -B 8 RETURN_ENUMERA -h *.c | egrep '^\w+\('
rb_ary_index(int argc, VALUE *argv, VALUE ary)
rb_ary_rindex(int argc, VALUE *argv, VALUE ary)
rb_ary_each(VALUE ary)
rb_ary_each_index(VALUE ary)
rb_ary_reverse_each(VALUE ary)
rb_ary_collect(VALUE ary)
rb_ary_collect_bang(VALUE ary)
rb_ary_select(VALUE ary)
rb_ary_reject_bang(VALUE ary)
rb_ary_reject(VALUE ary)
dir_each(VALUE dir)
enum_find(int argc, VALUE *argv, VALUE obj)
enum_find_all(VALUE obj)
enum_reject(VALUE obj)
enum_collect(VALUE obj)
enum_partition(VALUE obj)
enum_sort_by(VALUE obj)
enum_min_by(VALUE obj)
enum_max_by(VALUE obj)
enum_each_with_index(VALUE obj)
rb_hash_select(VALUE hash)
rb_hash_each_value(VALUE hash)
rb_hash_each_key(VALUE hash)
rb_hash_each_pair(VALUE hash)
rb_hash_each(VALUE hash)
env_each_key(VALUE ehash)
env_each_value(VALUE ehash)
env_each(VALUE ehash)
env_each_pair(VALUE ehash)
env_select(VALUE ehash)
rb_io_each_line(int argc, VALUE *argv, VALUE io)
rb_io_each_byte(VALUE io)
rb_io_s_foreach(int argc, VALUE *argv, VALUE self)
argf_each_line(int argc, VALUE *argv, VALUE self)
argf_each_byte(VALUE self)
range_step(int argc, VALUE *argv, VALUE range)
range_each(VALUE range)
rb_str_each_byte(VALUE str)
rb_struct_each(VALUE s)
rb_struct_each_pair(VALUE s)

--
Mauricio Fernandez