Module: MLL
- Defined in:
- lib/mll.rb
Constant Summary collapse
- VERSION =
"2.6.4"
Class Method Summary collapse
- .define_listable_function(name, &block) ⇒ Object
- .define_orderless_function(name, start, &block) ⇒ Object
- .dimensions ⇒ Object
- .fold_list ⇒ Object
- .grid ⇒ Object
- .map ⇒ Object
- .mean ⇒ Object
- .most ⇒ Object
- .nest ⇒ Object
- .nest_list ⇒ Object
- .nest_while ⇒ Object
- .nest_while_list ⇒ Object
- .rest ⇒ Object
- .riffle ⇒ Object
- .subdivide ⇒ Object
- .table ⇒ Object
- .tally ⇒ Object
Class Method Details
.define_listable_function(name, &block) ⇒ Object
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/mll.rb', line 265 def self.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
315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/mll.rb', line 315 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 |
.dimensions ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/mll.rb', line 7 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_list ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/mll.rb', line 71 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 |
.grid ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/mll.rb', line 133 def grid lambda do |table, spacings: [1, 1], **| # TODO negative spacings? spacing_horizontal, spacing_vertical = spacings spacing_vertical ||= 1 raise ArgumentError.new("unsupported value of :alignment option '#{options[:alignment]}'") unless \ alignment = { nil => :center, :center => :center, :left => :ljust, :right => :rjust, }[[:alignment]] raise ArgumentError.new("unsupported value of :frame option '#{options[:frame]}'") unless \ frames = { nil => " ", true => "┃ ┏┓ ┗┛━ ┃━━┃ ", :all => "┃┃┏┓╋┗┛━━┣┳┻┫ ", }[[:frame]] # TODO smth with this; maybe check out how Mathematica handles `Table[{1,{2,3},4}]` table = [table] unless table.all?{ |e| e.respond_to? :each } width = table.map(&:size).max table = table.map do |row| row.dup.tap do |row| row[width - 1] = row[width - 1] end.map &:to_s end rows = table.map{ |row| row.map{ |s| s.count ?\n }.max + 1 } cols = table.transpose.map{ |col| col.flat_map{ |s| s.scan(/.*/).map(&:size) }.max } chars = table.flat_map.with_index do |row, i| row.map.with_index do |s, j| lines = s.scan(/.+/) max = lines.map(&:size).max || 0 lines.map!{ |line| line.ljust(max).method(alignment).call(cols[j]) } self.table[ lambda do |k, l| m = k - spacing_vertical n = l - spacing_horizontal 0<=m && m<lines.size && 0<=n && n<cols[j] ? lines[m][n] : frames[( h = spacing_horizontal.zero? v = spacing_vertical.zero? k == 0 ? l == 0 ? h ? v ? ?O : 7 : v ? 0 : i.zero? ? j.zero? ? 2 : 10 : j.zero? ? 9 : 4 : v ? 13 : i.zero? ? 7 : 8 : l == 0 ? j.zero? ? 0 : 1 : 13 )] end, 0...[rows[i] + spacing_vertical * 2 - 1, 1].max, 0...[cols[j] + spacing_horizontal * 2 - 1, 1].max ] end.transpose.map{ |row| row.inject :+ } end borders_horizontal = fold_list[0, rows, ->i,j{ i + j + spacing_vertical * 2 - 1 }].to_a chars.each_with_index do |line, i| line.push frames[borders_horizontal.include?(i) && !spacing_vertical.zero? ? 12 : 0] line.unshift frames[i.zero? ? 2 : borders_horizontal.include?(i) && !spacing_vertical.zero? ? 9 : 0] if spacing_horizontal.zero? end borders_vertical = fold_list[0, cols, ->i,j{ i + j + spacing_horizontal * 2 - 1 }].to_a chars = chars.transpose.each_with_index do |line, i| line.push frames[i.zero? ? 5 : borders_vertical.include?(i) && !spacing_horizontal.zero? ? 11 : 7] line.unshift frames[i.zero? ? 2 : borders_vertical.include?(i) && !spacing_horizontal.zero? ? 10 : 7] if spacing_vertical.zero? end.transpose chars[-1][-1] = frames[6] chars[0][-1] = frames[3] chars.map{ |row| row.push ?\n }.join end end |
.map ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/mll.rb', line 87 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 |
.mean ⇒ Object
330 331 332 333 334 |
# File 'lib/mll.rb', line 330 def self.mean lambda do |list| divide[times[plus[*list], 1.0], list.size] end end |
.most ⇒ Object
243 244 245 246 247 248 |
# File 'lib/mll.rb', line 243 def most # not Enumerator because if the end is invisible we can't stop at -2 lambda do |list| list[0..-2] end end |
.nest ⇒ Object
36 37 38 39 40 41 |
# File 'lib/mll.rb', line 36 def nest lambda do |expr, n, f| n.times{ expr = f.call expr } expr end end |
.nest_list ⇒ Object
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/mll.rb', line 43 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_while ⇒ Object
54 55 56 57 58 59 |
# File 'lib/mll.rb', line 54 def nest_while lambda do |expr, f, test| expr = f.call expr while test.call expr expr end end |
.nest_while_list ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/mll.rb', line 61 def nest_while_list lambda do |expr, f, test| Enumerator.new do |e| e << expr e << expr = (f.is_a?(Symbol) ? expr.send(f) : f.call(expr)) while (test.is_a?(Symbol) ? expr.send(test) : test.call(expr)) # TODO test on this drunk Symbol thing end end end |
.rest ⇒ Object
250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/mll.rb', line 250 def rest lambda do |list| Enumerator.new do |e| begin enum = list.to_enum.tap &:next loop{ e << enum.next } rescue StopIteration next end end end end |
.riffle ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/mll.rb', line 215 def riffle lambda do |*args| case args.size when 2 next riffle[*args, [2,-2,2]] unless args[1].respond_to?(:each) && args[0].size == args[1].size Enumerator.new do |e| args[0].zip(args[1]){ |i, j| e << i << j } end when 3 args[2] = [args[2], -2, args[2]] unless args[2].respond_to? :each Enumerator.new do |e| min = (args[2][0] < 0) ? (args[0].size + args[2][0]) : args[2][0] - 1 max = (args[2][1] < 0) ? (args[0].size + args[2][1]) : args[2][1] min, max = max, min if args[2][0] < 0 step = args[2][2] pos = 0 args[0].each_with_index do |x, i| # TODO make it not destructive (pos += 1; e << x) (pos += 1; e << (args[1].respond_to?(:each) ? args[1].rotate!.last : args[1])) if min - 1 <= i && i <= max && (pos - min) % step == 0 end end else raise ArgumentError.new("wrong number of arguments (#{args.size} for 2..3)") end end end |
.subdivide ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/mll.rb', line 201 def 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 |
.table ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/mll.rb', line 104 def table lambda do |f, *args| [].tap do |result| [[result, args.map{ |r| # add lazy? if r.respond_to? :map next r if r.is_a? Range next r.first if r.first.respond_to? :map # TODO check r.size? end 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 |
.tally ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/mll.rb', line 23 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 |