Sha256: a18038c69b0b5295ddc522cfaa321651b787d647ee72081430113a8a458c7c99

Contents?: true

Size: 1.07 KB

Versions: 4

Compression:

Stored size: 1.07 KB

Contents

# = TITLE:
#
#   Mapcar
#
# AUTHOR:
#
#   Cosmin Bonchis
#
# NOTES:
#
#   Google Summer of Code 2007 project for Ruby Central Inc.

require 'generator'
#
# Non-recursive mapcar (works on all Enumerables)
#
def mapcar(*enums)
  generators = enums.collect { |e| Generator.new(e) }
  result = []
  while true
    begin
      params = generators.collect { |g| g.current; g.next }
        rescue EOFError
      return result
    end
    result << yield(*params)
  end
end

def map(*enums)
  generators = []; enums.each { |e| generators << Generator.new(e) }
  while true
    begin
      params = []; generators.each { |g| g.current; params << g.next }
        rescue EOFError
      return
    end
    yield(*params)
  end
end

class Array
  def Array.map(n, *arrays)
    len = arrays.length
    if n == 0
      n = arrays[0].length
      1.upto(arrays.length - 1) do |i|
        al = arrays[i].length
        n = al if al < n
      end
    end
    0.upto(n - 1) do |i|
      params = []
      0.upto(len - 1) do |arr|
        params << arrays[arr][i]
      end
      yield(*params)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
stick-1.2.0 lib/stick/mapcar.rb
stick-1.3.2 lib/stick/mapcar.rb
stick-1.3.1 lib/stick/mapcar.rb
stick-1.3.0 lib/stick/mapcar.rb