Sha256: 47fe2925c1c8481cd3d6c80521ba8a223a10450f4ef1a15c4494204215d9370f

Contents?: true

Size: 887 Bytes

Versions: 3

Compression:

Stored size: 887 Bytes

Contents

require 'ludy/version'

class Array
  if Ludy::ruby_before '1.9.0'
    require 'ludy/array/foldr'
    require 'ludy/symbol/to_proc' if Ludy::ruby_before '1.9.0'
    # for each combos
    #  [[0,1],[2,3]].combos
    #  => [[0,2],[0,3],[1,2],[1,3]]
    def combos
      result = []
      radixs = reverse.map(&:size)
      inject(1){|r, i| r * i.size}.times{ |step|
        result << foldr(lambda{ |i, r|
                          radix = radixs[r.size]
                          r.unshift i[step % radix]
                          step /= radix unless radix.nil?
                          r
                        }, [])
      }
      result
    end
  else
    require 'ludy/array/tail'
    # for each combos
    #  [[0,1],[2,3]].combos
    #  => [[0,2],[0,3],[1,2],[1,3]]
    # simply:
    #  array.first.product *array.tail
    def combos
      first.product(*tail)
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
godfat-ludy-0.1.13 lib/ludy/array/combos.rb
ludy-0.1.11 lib/ludy/array/combos.rb
ludy-0.1.13 lib/ludy/array/combos.rb