Module: MLL

Defined in:
lib/mll.rb

Class Method Summary collapse

Class Method Details

.define_listable_function(name, &block) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/mll.rb', line 150

def define_listable_function name, &block
  (class << self; self end).class_eval do
    define_method name do
      lambda do |*args|
        case args.map{ |i| i.respond_to? :map }
          when [true] ; args.first.lazy.map &method(name).call
          when [true, true] ; args.first.lazy.zip(args.last).map{ |i, j| send(name)[i, j] }
          when [true, false] ; args.first.lazy.map{ |i| send(name)[i, args.last] }
          when [false, true] ; args.last.lazy.map{ |i| send(name)[args.first, i] }
        else
          block.call *args
        end
      end
    end
  end
end

.define_orderless_function(name, start, &block) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
# File 'lib/mll.rb', line 215

def self.define_orderless_function name, start, &block
  (class << self; self end).class_eval do # http://stackoverflow.com/a/12792313/322020
    define_method name do
     lambda do |*args|
        args.inject(start) do |memo, obj|
          block.call memo, obj
        end
      end
    end
  end
end

.dimensionsObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/mll.rb', line 5

def dimensions
  lambda do |list, limit = nil|
    list = [list]
    enumerator = Enumerator.new do |e|
      # String.size shall not pass
      while list.all?{ |i| i.respond_to? :each } &&
            list.map(&:size).uniq.size == 1
        # TODO refactor into depth-first yielding
        e << list.first.size
        list.flatten! 1
      end
    end
    limit ? enumerator.lazy.take(limit) : enumerator
  end
end

.fold_listObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/mll.rb', line 60

def fold_list
  lambda do |x, list, f = nil|
    unless f
      f = list
      x, *list = x.to_a
    end
    # TODO use Ruby#inject ?
    Enumerator.new do |e|
      e << x
      list.each do |i|
        e << x = f.call(x, i)
      end
    end
  end
end

.gridObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/mll.rb', line 119

def grid
  lambda do |table, **options|
    raise ArgumentError.new("unknown value of :alignment option '#{options[:alignment]}'") unless \
      alignment = {
        nil => :center,
        :center => :center,
        :left => :ljust,
        :right => :rjust,
      }[options[:alignment]]
    frames = {
      nil  => "              ",
      true => "┃ ┏┓ ┗┛━ ┃━━┃ ",
      :all => "┃┃┏┓╋┗┛━━┣┳┻┫ ",
    }[options[:frame]]
    raise ArgumentError.new("unknown value of :frame option '#{options[:frame]}'") if options[:frame] && !frames
    # TODO smth with this #.all?
    table = [table] unless table.all?{ |e| e.respond_to? :each }
    width = table.map(&:size).max - 1
    strings = table.map{ |row| row.dup.tap{ |a| a[width] = a[width] }.map(&:to_s) }
    sizes = strings.transpose.map{ |col| col.map(&:size).max }
    # TODO https://reference.wolfram.com/language/ref/Alignment.html
    [
      [frames[2], sizes.map{ |size| frames[7] * size }.join(frames[10]), frames[3]].join,
      strings.map{ |row| [frames[0], row.zip(sizes).map{ |str, size| str.method(alignment).call size }.join(frames[1]), frames[0]].join }.join(
        [?\n, frames[9], sizes.map{ |size| frames[8] * size }.join(frames[4]), frames[12], ?\n].join
      ),
      [frames[5], sizes.map{ |size| frames[7] * size }.join(frames[11]), frames[6]].join,
    ].join(?\n) + ?\n
  end
end

.mapObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/mll.rb', line 76

def map
  # TODO validate depths
  # TODO break on passing all depths
  lambda do |list, depths, f = nil|
    depths, f = [1], depths unless f
    depths = Range.new(*depths) if depths.size == 2
    depths = Range.new(1,depths) if depths.is_a? Integer
    g = lambda do |list, depth|
      next list unless list.is_a? Array
      temp = list.lazy.map{ |i| g[i, depth + 1] }
      temp = temp.lazy.map &f if depths.include? depth
      temp
    end
    g[list, 1]
  end
end

.meanObject



230
231
232
233
234
# File 'lib/mll.rb', line 230

def self.mean
  lambda do |list|
    divide[times[plus[*list], 1.0], list.size]
  end
end

.nestObject



34
35
36
37
38
39
# File 'lib/mll.rb', line 34

def nest
  lambda do |expr, n, f|
    n.times{ expr = f.call expr }
    expr
  end
end

.nest_listObject



41
42
43
44
45
46
47
48
49
50
# File 'lib/mll.rb', line 41

def nest_list
  lambda do |expr, n, f|
    Enumerator.new do |e|
      e << expr
      n.times do
        e << expr = f.call(expr)
      end
    end
  end
end

.nest_whileObject



52
53
54
55
56
57
58
# File 'lib/mll.rb', line 52

def nest_while
  # TODO finish me
  lambda do |expr, f, test|
    expr = f[expr] while test[expr]
    expr
  end
end

.subdivideObject



193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/mll.rb', line 193

def self.subdivide
  lambda do |*args|
    case args.size
      when 1 ; subdivide[1, args[0]]
      when 2 ; subdivide[0, args[0], args[1]]
      when 3
        # raise ArgumentError.new("can't divide into 0 parts") if args[2].zero?
        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
end

.tableObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/mll.rb', line 93

def table
  lambda do |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.respond_to?(:call) ? f.call(*ai, i) : f
            else
              [*ai.dup, i].tap{ |t| stack << [t, ri.drop(1)] }
            end
          }
        end
      end
    end
  end
end

.tallyObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mll.rb', line 21

def tally
  lambda do |list, test = nil| # implement #sameq ?
    return Hash[ list.group_by{ |i| i }.map{ |k, v| [k, v.size] } ] unless test
    Hash.new{ 0 }.tap do |result|
      list.each do |item|
        result[
          list.find{ |key| test[key, item] }
        ] += 1
      end
    end
  end
end