Module: MLL

Defined in:
lib/mll.rb

Class Method Summary collapse

Class Method Details

.define_function_that_can_enumerate(name, &block) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/mll.rb', line 3

def self.define_function_that_can_enumerate name, &block
  # http://stackoverflow.com/a/12792313/322020
  (class << self; self end).class_eval do
    define_method name do |*args|
      if args.size == 1 && args.first.respond_to?(:map)
        args.first.lazy.map &method(name)
      else
        block.call *args
      end
    end
  end
end

.table(f, *args) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/mll.rb', line 38

def self.table f, *args
  # TODO make it lazy?

  [].tap do |result|
    [[result, args.map{ |r| range(*r).to_a }]].tap do |stack|
      stack.each do |ai, ri|
        ai.replace ri.first.map{ |i|
          if ri.size == 1
            f.call(*ai, i)
          else
            [*ai.dup, i].tap{ |t| stack << [t, ri.drop(1)] }
          end
        }
      end
    end
  end

end