[#1263] Draft of the updated Ruby FAQ — Dave Thomas <Dave@...>

33 messages 2000/02/08

[#1376] Re: Scripting versus programming — Andrew Hunt <andy@...>

Conrad writes:

13 messages 2000/02/15

[#1508] Ruby/GTK and the mainloop — Ian Main <imain@...>

17 messages 2000/02/19
[#1544] Re: Ruby/GTK and the mainloop — Yasushi Shoji <yashi@...> 2000/02/23

Hello Ian,

[#1550] Re: Ruby/GTK and the mainloop — Ian Main <imain@...> 2000/02/23

On Wed, Feb 23, 2000 at 02:56:10AM -0500, Yasushi Shoji wrote:

[#1516] Ruby: PLEASE use comp.lang.misc for all Ruby programming/technical questions/discussions!!!! — "Conrad Schneiker" <schneiker@...>

((FYI: This was sent to the Ruby mail list.))

10 messages 2000/02/19

[#1569] Re: Ruby: constructors, new and initialise — Yukihiro Matsumoto <matz@...>

The following message is a courtesy copy of an article

12 messages 2000/02/25

[ruby-talk:01364] Re: nice sample for functional stuff

From: gotoken@... (GOTO Kentaro)
Date: 2000-02-15 04:30:26 UTC
List: ruby-talk #1364
Hi,

In message "[ruby-talk:01355] nice sample for functional stuff"
    on 00/02/15, Pixel <pixel_@mandrakesoft.com> writes:

>what about having map in standard (and map_index too)?

As Matz said, methods in Enumerable module may help you. 
By the way, once I wrote `sum' with callcc as follows. 

Enjoy :-)

-- gotoken
______________________________________________________________________

module Enumerable
  class Cursor
    EOE = Object.new # end of stream
    def EOE.to_s; 'END_OF_ENUM' end

    def initialize(iter)
      @iter = iter
      @cont = nil
    end

    def next
      callcc{|@cc|
	@cont.call if @cont
	@iter.call {|obj|
	  callcc{|@cont| @cc.call(obj) }
	}
	@cc.call(EOE)
      }
    end
  end
  
  def sum_with_index
    i = 0
    c = Cursor.new(self.method(:each))
    if iterator?
      return nil if (s = c.next) == Cursor::EOE
      s = yield(s,i)
      until (elm = c.next) == Cursor::EOE
	s += yield(elm,i)
      end
      s
    else
      return nil if (s = c.next) == Cursor::EOE
      until (elm = c.next) == Cursor::EOE
	s += elm
      end
      s
    end
  end

  def sum(&blk)
    if iterator?
      sum_with_index{|elm,i| yield(elm)}
    else
      sum_with_index{|elm,i| elm}
    end
  end

  def average(&blk)
    if iterator?
      sum_with_index{|elm,i| yield(elm)}/size.to_f
    else
      sum_with_index{|elm,i| elm}/size.to_f
    end    
  end
end

def sum(*args, &blk)
  args.sum(&blk)
end

def average(*args, &blk)
  args.average(&blk)
end

####### examples #######

ary = [1,2,3,4]
    #=> [1, 2, 3, 4]
ary.sum
    #=> 10
ary.sum{|k| k*k}
    #=> 30
sum(ary)
    #=> [1, 2, 3, 4]
sum(*ary){|k| k*k}
    #=> 30
sum(1,2,3,4){|k| k*k}
    #=> 30

words = %w(foo bar baz)
    #=> ["foo", "bar", "baz"]
words.sum
    #=> "foobarbaz"
words.sum{|w| w.capitalize}
    #=> "FooBarBaz"

hash = {"Foo" => 1, "Bar" => 2, "Baz" => 3, "Quux" => 4}
    #=> {"Baz"=>3, "Bar"=>2, "Foo"=>1, "Quux"=>4}
hash.sum{|name,value| name =~ /^B/ ? value : 0}
    #=> 5

class Tree
  include Enumerable
  def Tree.[](*args)
    Tree.new(*args)
  end

  def initialize(value, children = [])
    children = [children] unless children.is_a? Array
    @value = value
    @children = children.collect{|c| c.is_a?(Tree) ? c : Tree.new(c)}
  end

  attr_reader :value, :children

  def each(&blk)
    yield(self)
    @children.each{|c| c.each(&blk)}
  end

  def leaf?
    @children.empty?
  end

  def method_missing(mid, *args, &blk)
    @value.__send__(mid, *args, &blk)
  end
end

tree = Tree[1,[Tree[2,[Tree[3]]],Tree[4]]]
    #=> #<Tree: @children=[#<Tree: @children=[#<Tree: @children=[], @value=3>], @value=2>, #<Tree: @children=[], @value=4>], @value=1>
tree.sum
    #=> 10
tree.sum{|n| if n.leaf? then n else 0 end}
    #=> 7

In This Thread