Sha256: 9ad08097f97fa4476d116c83d0bfb02f42f7020e598fc76a2daf982a38a9b2d2

Contents?: true

Size: 931 Bytes

Versions: 4

Compression:

Stored size: 931 Bytes

Contents

# Copyright: Copyright (c) 2004  Nicolas Pouillard. All rights reserved.
# Author: Nicolas Pouillard  <ertai@lrde.epita.fr>.
# License: Gnu General Public License.

# $LastChangedBy: ertai $
# $Id: fold.rb 186 2005-04-03 00:07:45Z ertai $


class Object

  def rec_fold
    if respond_to?(:fold)
      return fold({}) do |k1, v1, acc1|
        yield(k1, v1.rec_fold { |k2, v2, acc2| yield(k2, v2, acc2) }, acc1)
      end
    else
      return self
    end
  end

end # class Object


class Hash

  def fold(init, &block)
    acc = init
    self.each do |k,v|
      acc = block[k, v, acc]
    end
    acc
  end

end # class Hash


class Array

  def fold(init, &block)
    acc = init
    self.each do |elt|
      acc = block[elt, self[elt], acc]
    end
    acc
  end

  def reverse_fold(init, &block)
    acc = init
    self.reverse_each do |elt|
      acc = block[elt, self[elt], acc]
    end
    acc
  end

end # class Array



Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
ttk-0.1.576 ruby_ex/fold.rb
ttk-0.1.579 ruby_ex/fold.rb
ttk-0.1.580 ruby_ex/fold.rb
vcs-0.2.148 ruby_ex/fold.rb