Underscore

>> >
>> > > > >
>> > > > >I was too fast (again). Even though the return value is used, I'd
>> > rather
>> > > > >do
>> > > > >
>> > > > >n.times { x = yield x }
>> > > > >
>> > > > >than using #inject which does more than needed in this case. :slight_smile:
>> > > > >
>> > > > >Now, did I look at all aspects...?
>> > > >
>> > > > Almost.
>> > > > you'd actually need
>> > > > n.times { x = yield x }
>> > > > x
>> > >
>> > > Almost almost :slight_smile:
>> > >
>> > > x = nil
>> > > n.times { x = yield x }
>> > > x
>> > >
>> > Robert was very quick indeed, he did not repeat the context of the
>> > expression which was
>> >
>> > def forgot_the_name(x,...)
>> > n.times{ x = yield x }
>>
>> Still need x here
>>
>> end
>> >
>> > Now it works :wink:
>>
>> No it doesn't :wink: n.times { ... } returns n.
> Obviously I am too stupid to test, sorry, I did test it, who knows how :wink:
> Seriously I do know now, n just was the expected result of the block :frowning:
>
> def anyhow(count, x)
> count.times{x=yield x}
> x
> end
>
> Maybe we can still agree that this is better than inspect :wink:
> And yes thanks for the Ruby classes I got.

Hm... Thinking a bit more about this I am not sure I'd stick with my
proposal. I think I'd probably do

irb(main):001:0> require 'enumerator'
=> true
irb(main):002:0> def rep(n,init=nil) n.to_enum(:times).inject(init)
{|x,| yield x} end
=> nil
irb(main):003:0> rep(5,"x") {|s| puts s; s<<"."}
x
x.
x..
x...
x....
=> "x....."

Hmm...

As we are fooling around :wink:

class Integer
  alias_method :good_old_times, :times
  def times
    r = nil
    good_old_times do
      >n>
      r = Proc.new.call n
    end
    r
  end

  def rep init = nil
    times do
       init = Proc.new.call init
    end
  end
end

puts 5.times{ |x| 2*x }
puts 5.rep(12){ |x| x+1 }
BTW what is this good for anyway :wink: for loops???

Cheers
Robert

···

On 5/31/07, Robert Klemme <shortcutter@googlemail.com> wrote:

On 30.05.2007 22:36, Robert Dober wrote:
> On 5/30/07, Logan Capaldo <logancapaldo@gmail.com> wrote:
>> On 5/30/07, Robert Dober <robert.dober@gmail.com> wrote:
>> > On 5/30/07, Brian Candler <B.Candler@pobox.com> wrote:
>> > > On Wed, May 30, 2007 at 07:50:05AM +0900, Logan Capaldo wrote:
>> > > > On 5/29/07, Robert Klemme <shortcutter@googlemail.com> wrote:

--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

>> >
>> > > > >
>> > > > >I was too fast (again). Even though the return value is used, I'd
>> > rather
>> > > > >do
>> > > > >
>> > > > >n.times { x = yield x }
>> > > > >
>> > > > >than using #inject which does more than needed in this case. :slight_smile:
>> > > > >
>> > > > >Now, did I look at all aspects...?
>> > > >
>> > > > Almost.
>> > > > you'd actually need
>> > > > n.times { x = yield x }
>> > > > x
>> > >
>> > > Almost almost :slight_smile:
>> > >
>> > > x = nil
>> > > n.times { x = yield x }
>> > > x
>> > >
>> > Robert was very quick indeed, he did not repeat the context of the
>> > expression which was
>> >
>> > def forgot_the_name(x,...)
>> > n.times{ x = yield x }
>>
>> Still need x here
>>
>> end
>> >
>> > Now it works :wink:
>>
>> No it doesn't :wink: n.times { ... } returns n.
> Obviously I am too stupid to test, sorry, I did test it, who knows how :wink:
> Seriously I do know now, n just was the expected result of the block :frowning:
>
> def anyhow(count, x)
> count.times{x=yield x}
> x
> end
>
> Maybe we can still agree that this is better than inspect :wink:
> And yes thanks for the Ruby classes I got.

Hm... Thinking a bit more about this I am not sure I'd stick with my
proposal. I think I'd probably do

irb(main):001:0> require 'enumerator'
=> true
irb(main):002:0> def rep(n,init=nil) n.to_enum(:times).inject(init)
{|x,| yield x} end
=> nil
irb(main):003:0> rep(5,"x") {|s| puts s; s<<"."}
x
x.
x..
x...
x....
=> "x....."

Hmm...

As we are fooling around :wink:

class Integer
alias_method :good_old_times, :times
def times
   r = nil
   good_old_times do
     >n>
     r = Proc.new.call n
   end
   r
end

You are aware that this does something different, especially will it return the last block value only.

Also, why do you use Proc.new.call instead of yield?

def rep init = nil
   times do
      init = Proc.new.call init
   end
end
end

puts 5.times{ |x| 2*x }
puts 5.rep(12){ |x| x+1 }
BTW what is this good for anyway :wink: for loops???

Probably. I tend to believe that usually #inject is better for calculations that involve repetition and want to process the result of the last calculation. Usually I don't think that the iteration value is ignored in such cases. Maybe the OP had a specific example in mind.

The initial question was what does the underscore do in

def nest(x, n = 2)
   (1..n).inject(x) { |acc, _| yield(acc) }
end

Basically it's completely superfluous, you can do this instead:

def nest(x, n = 2)
   (1..n).inject(x) { |acc,| yield acc }
end

Btw, I'd rather change the argument handling because n=2 is a pretty arbitrary default.

def nest(n, x = nil)
   (1..n).inject(x) { |acc,| yield acc }
end

Kind regards

  robert

···

On 31.05.2007 11:51, Robert Dober wrote:

On 5/31/07, Robert Klemme <shortcutter@googlemail.com> wrote:

On 30.05.2007 22:36, Robert Dober wrote:
> On 5/30/07, Logan Capaldo <logancapaldo@gmail.com> wrote:
>> On 5/30/07, Robert Dober <robert.dober@gmail.com> wrote:
>> > On 5/30/07, Brian Candler <B.Candler@pobox.com> wrote:
>> > > On Wed, May 30, 2007 at 07:50:05AM +0900, Logan Capaldo wrote:
>> > > > On 5/29/07, Robert Klemme <shortcutter@googlemail.com> wrote: