Module: MLL

Defined in:
lib/mll.rb

Class Method Summary collapse

Class Method Details

.define_listable_function(name, &block) ⇒ Object



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

def self.define_listable_function name, &block
  (class << self; self end).class_eval do # http://stackoverflow.com/a/12792313/322020
    define_method name do |*args|
# p [name, args]
      next block.call *args unless args[0].respond_to? :map
      next args[0].lazy.map{ |i| send name, i, *args.drop(1) } unless args[1].respond_to? :map
      # raise "idk how to list this function" if args.size > 2
      next args[0].lazy.zip(args[1]).map{ |i,j| send name, i, j, *args.drop(2) }
    end
  end
end

.subdivide(*args) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/mll.rb', line 65

def self.subdivide *args
  case args.size
  when 1 ; subdivide 1, args[0]
  when 2 ; subdivide 0, args[0], args[1]
  when 3 ; range(args[0], args[1], (args[1] - args[0]) * 1.0 / args[2])
  else
    raise ArgumentError.new("wrong number of arguments (#{args.size} for 1..3)")
  end
end

.table(f, *args) ⇒ Object



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

def self.table f, *args
  [].tap do |result|
    [[result, args.map{ |r| # add lazy?
      r.respond_to?(:map) && r.first.respond_to?(:map) ?
        r.first : range(*r)
    }]].tap do |stack|
      stack.each do |ai, ri|
        # TODO try to make #table lazy (Enumerator instead of Array)
        # "no implicit conversion of Enumerator::Lazy into Array"
        # "undefined method `replace' for #<Enumerator::Lazy: []>"
        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
        }#.to_a # WTF
      end
    end
  end
end