Module: MLL
- Defined in:
- lib/mll.rb
Constant Summary collapse
- VERSION =
"2.6.2"
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
- .rest ⇒ Object
- .riffle ⇒ Object
- .subdivide ⇒ Object
- .table ⇒ Object
- .tally ⇒ Object
Class Method Details
.define_listable_function(name, &block) ⇒ Object
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/mll.rb', line 256 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
306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/mll.rb', line 306 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
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/mll.rb', line 62 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
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 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 |
# File 'lib/mll.rb', line 124 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 '#{[:alignment]}'") unless \ alignment = { nil => :center, :center => :center, :left => :ljust, :right => :rjust, }[[:alignment]] raise ArgumentError.new("unsupported value of :frame option '#{[: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
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/mll.rb', line 78 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
321 322 323 324 325 |
# File 'lib/mll.rb', line 321 def self.mean lambda do |list| divide[times[plus[*list], 1.0], list.size] end end |
.most ⇒ Object
234 235 236 237 238 239 |
# File 'lib/mll.rb', line 234 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 60 |
# File 'lib/mll.rb', line 54 def nest_while # TODO finish me lambda do |expr, f, test| expr = f[expr] while test[expr] expr end end |
.rest ⇒ Object
241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/mll.rb', line 241 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
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/mll.rb', line 206 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
192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/mll.rb', line 192 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
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/mll.rb', line 95 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 |