Sha256: 8da60617c241773c58340f960b11aaba0d0e525f9bb0f8da75eb0a1c31a8c1d9

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 KB

Contents

# = TITLE:
#
#   Mapcar
#
# AUTHOR:
#
#   Cosmin Bonchis
#
# NOTES:
#
#   Google Summer of Code 2007 project for Ruby Central Inc.
#
# TODO:
#   - #mapcar should ne be defined in toplevel. But Matrix
#    is using it at class-level. WTF?

require 'generator'

#module Enumerable

  # 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

#end


class Array

  def self.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

2 entries across 2 versions & 2 rubygems

Version Path
facets-2.9.2 work/consider/mapcar.rb
stick-1.3.3 lib/stick/mapcar.rb