mll 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +10 -1
- data/lib/mll.rb +35 -45
- data/mll.gemspec +1 -1
- data/spec/_spec.rb +55 -62
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 70676b9e4d6334e3e8391c256aa11c3ea5e9c033
|
4
|
+
data.tar.gz: 7e8b55eb690a4ee783358750fd25ca1045475508
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 449a0ca50feab9413c62cc1a67d37bb4a349ab97f0c842f676cea2ddea06ba03b29d96d8200bd846c9700d3388f60112ab3b900408f5a47aa7aa4b38d78342fd
|
7
|
+
data.tar.gz: 8f5f8a226918468f4e3f636df329f4520c686181fccca13c26b04bd493018156da4f79101b07a27dfbbfab32de5306b223505dbc52f914d5a8a44bad8b9dd780
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# MLL (Mathematica Language Library)
|
2
2
|
|
3
|
-
Этот гем не ставит перед собой цель полностью имитировать типы данных, синтаксис
|
3
|
+
Этот гем не ставит перед собой цель полностью имитировать типы данных, синтаксис Wolfram Mathematica Language, или научить Ruby крутым визуализациям. Целью является вложить в Ruby мощь стандартной библиотеки. Прежде всего, List Manipulation. В перспективе визуализация возможна при помощи других гемов.
|
4
4
|
|
5
5
|
## Usage
|
6
6
|
|
@@ -22,3 +22,12 @@
|
|
22
22
|
## Installation
|
23
23
|
|
24
24
|
$ gem install mll
|
25
|
+
|
26
|
+
### Testing with RSpec before contributing
|
27
|
+
|
28
|
+
rspec
|
29
|
+
|
30
|
+
or
|
31
|
+
|
32
|
+
rake
|
33
|
+
|
data/lib/mll.rb
CHANGED
@@ -5,7 +5,7 @@ module MLL
|
|
5
5
|
(class << self; self end).class_eval do
|
6
6
|
define_method name do |*args|
|
7
7
|
if args.size == 1 && args.first.respond_to?(:map)
|
8
|
-
args.first.map &method(name)
|
8
|
+
args.first.lazy.map &method(name)
|
9
9
|
else
|
10
10
|
block.call *args
|
11
11
|
end
|
@@ -14,54 +14,44 @@ module MLL
|
|
14
14
|
end
|
15
15
|
|
16
16
|
define_function_that_can_enumerate :range do |*args|
|
17
|
-
|
18
|
-
|
19
|
-
Range.new(args[0], args[1])
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
=begin
|
26
|
-
|
27
|
-
# http://reference.wolfram.com/language/guide/HandlingArraysOfData.html
|
28
|
-
# http://reference.wolfram.com/language/guide/ComputationWithStructuredDatasets.html
|
29
|
-
|
30
|
-
module Enumerable
|
31
|
-
STDERR.puts "WARNING: #{name}#range was already defined" if method_defined? "range"
|
32
|
-
def range
|
33
|
-
# if self.respond_to? :each
|
34
|
-
if self.first.respond_to? :each
|
35
|
-
raise ArgumentError.new("if the first #range parameter responds to :each, it should be the only argument") \
|
36
|
-
if self.size > 1
|
37
|
-
self.first.zip.map &:range
|
17
|
+
case args.size
|
18
|
+
when 1 ; range 1, args[0] # TODO do smth with #table(-n)
|
19
|
+
when 2 ; Range.new(args[0], args[1])
|
20
|
+
when 3
|
21
|
+
case args[2] <=> 0
|
22
|
+
when 0 ; raise ArgumentError.new("step can't be zero")
|
23
|
+
when 1 ; Range.new(args[0], args[1]).step args[2]
|
38
24
|
else
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
25
|
+
Enumerator.new do |e|
|
26
|
+
from, to, step = *args
|
27
|
+
while (step > 0) ? from <= to : from >= to
|
28
|
+
e << from
|
29
|
+
from += step
|
30
|
+
end
|
44
31
|
end
|
45
32
|
end
|
46
|
-
|
47
|
-
|
48
|
-
|
33
|
+
else
|
34
|
+
raise ArgumentError.new("wrong number of arguments (#{args.size} for 1..3)") # unless (1..3).include? args.size
|
35
|
+
end
|
49
36
|
end
|
50
|
-
end
|
51
37
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
38
|
+
def self.table f, *args
|
39
|
+
# TODO make it lazy?
|
40
|
+
|
41
|
+
[].tap do |result|
|
42
|
+
[[result, args.map{ |r| range(*r).to_a }]].tap do |stack|
|
43
|
+
stack.each do |ai, ri|
|
44
|
+
ai.replace ri.first.map{ |i|
|
45
|
+
if ri.size == 1
|
46
|
+
f.call(*ai, i)
|
47
|
+
else
|
48
|
+
[*ai.dup, i].tap{ |t| stack << [t, ri.drop(1)] }
|
49
|
+
end
|
50
|
+
}
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
64
55
|
end
|
65
|
-
end
|
66
56
|
|
67
|
-
|
57
|
+
end
|
data/mll.gemspec
CHANGED
data/spec/_spec.rb
CHANGED
@@ -11,7 +11,9 @@ describe MLL do
|
|
11
11
|
describe "Constructing Lists" do
|
12
12
|
|
13
13
|
# http://reference.wolfram.com/language/ref/Range.html
|
14
|
-
describe "range" do
|
14
|
+
describe "#range" do
|
15
|
+
|
16
|
+
# TODO negative step and add it to README.rb
|
15
17
|
|
16
18
|
example "range( >3 args )" do
|
17
19
|
expect{ MLL::range(1,2,3,4) }.to raise_error ArgumentError
|
@@ -20,43 +22,40 @@ describe MLL do
|
|
20
22
|
describe "Basic Examples" do
|
21
23
|
|
22
24
|
example "range(n)" do
|
23
|
-
expect(MLL::range(4)).to be_a
|
25
|
+
expect(MLL::range(4)).to be_a Range
|
24
26
|
expect(MLL::range(4).to_a).to eq [1,2,3,4]
|
25
27
|
end
|
26
28
|
example "range(imin, imax)" do
|
27
|
-
expect(MLL::range(2,5)).to be_a
|
29
|
+
expect(MLL::range(2,5)).to be_a Range
|
28
30
|
expect(MLL::range(2,5).to_a).to eq [2,3,4,5]
|
29
31
|
end
|
30
32
|
example "range(imin, imax, id)" do
|
31
33
|
expect(MLL::range(1,2,3)).to be_a Enumerator
|
32
|
-
expect(MLL::range(1,2,0.5).to_a).to eq [1,1.5,2]
|
34
|
+
expect(MLL::range(1,2,0.5).to_a).to eq [1,1.5,2] # can be precision problems
|
33
35
|
expect(MLL::range(2,6,2).to_a).to eq [2,4,6]
|
34
36
|
expect(MLL::range(-4,9,3).to_a).to eq [-4,-1,2,5,8]
|
37
|
+
# Ruby can't negative Range#step, haha!
|
38
|
+
expect(MLL::range(10,-5,-2).to_a).to eq [10,8,6,4,2,0,-2,-4]
|
39
|
+
expect(MLL::range(3,1,-1).to_a).to eq [3,2,1]
|
35
40
|
end
|
36
|
-
# example "range(imin, imax, id)" do
|
37
|
-
# expect(MLL::range(2,5,2)).to be_a Enumerator
|
38
|
-
# expect(MLL::range(2,5,2).to_a).to eq [2,4]
|
39
|
-
# expect(MLL::range(2,6,2).to_a).to eq [2,4,6]
|
40
|
-
# end
|
41
41
|
end
|
42
42
|
|
43
43
|
describe "Generalizations & Extensions" do
|
44
44
|
|
45
45
|
example "range([n1, n2, n3, n4])" do
|
46
|
-
|
46
|
+
expect(MLL::range([5,2,6,3])).to be_a Enumerator
|
47
47
|
MLL::range([5,2,6,3]).each do |i|
|
48
|
-
expect(i).to be_a
|
48
|
+
expect(i).to be_a Range
|
49
49
|
end
|
50
|
-
expect(MLL::range([5,2,6,3]).map(&:to_a)).to eq [[1,2,3,4,5],[1,2],[1,2,3,4,5,6],[1,2,3]]
|
50
|
+
expect(MLL::range([5,2,6,3]).to_a.map(&:to_a)).to eq [[1,2,3,4,5],[1,2],[1,2,3,4,5,6],[1,2,3]]
|
51
51
|
end
|
52
52
|
|
53
53
|
end
|
54
54
|
|
55
55
|
describe "Applications" do
|
56
56
|
|
57
|
-
example "power(
|
57
|
+
example "power(n1, range(n2))" do
|
58
58
|
pending "#power is yet to be implemented"
|
59
|
-
p power(2, range(5))
|
60
59
|
expect(power(2, range(5))).to eq [2,4,8,16,32]
|
61
60
|
end
|
62
61
|
|
@@ -64,19 +63,25 @@ describe MLL do
|
|
64
63
|
|
65
64
|
describe "Neat Examples" do
|
66
65
|
|
67
|
-
example "range(
|
68
|
-
|
66
|
+
example "range(imin..imax)" do
|
67
|
+
expect(MLL::range(MLL::range(3))).to be_a Enumerator
|
69
68
|
MLL::range(MLL::range(3)).each do |i|
|
70
|
-
expect(i).to be_a
|
69
|
+
expect(i).to be_a Range
|
71
70
|
end
|
72
|
-
expect(MLL::range(1..3).map(&:to_a)).to eq [[1],[1,2],[1,2,3]]
|
71
|
+
expect(MLL::range(1..3).to_a.map(&:to_a)).to eq [[1],[1,2],[1,2,3]]
|
73
72
|
end
|
74
73
|
example "range(range(range(n)))" do
|
75
|
-
#
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
74
|
+
# TODO do the same .tap thing in other type tests
|
75
|
+
MLL::range(MLL::range(MLL::range(3))).tap do |o|
|
76
|
+
expect(o).to be_a Enumerator
|
77
|
+
o.each do |i|
|
78
|
+
expect(i).to be_a Enumerator
|
79
|
+
i.each do |j|
|
80
|
+
expect(j).to be_a Range
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
expect(MLL::range(MLL::range(MLL::range(3))).to_a.map{ |i| i.to_a.map(&:to_a) }).to eq [[[1]],[[1],[1,2]],[[1],[1,2],[1,2,3]]]
|
80
85
|
end
|
81
86
|
|
82
87
|
# TODO Nest[Range,3,6]
|
@@ -88,55 +93,43 @@ describe MLL do
|
|
88
93
|
|
89
94
|
end
|
90
95
|
|
91
|
-
|
92
|
-
|
93
|
-
# http://reference.wolfram.com/language/guide/ElementsOfLists.html
|
94
|
-
# http://reference.wolfram.com/language/guide/RearrangingAndRestructuringLists.html
|
95
|
-
# http://reference.wolfram.com/language/guide/ApplyingFunctionsToLists.html
|
96
|
-
# http://reference.wolfram.com/language/guide/MathematicalAndCountingOperationsOnLists.html
|
96
|
+
# http://reference.wolfram.com/language/ref/Table.html
|
97
|
+
describe "#table" do
|
97
98
|
|
98
|
-
|
99
|
-
|
100
|
-
# http://reference.wolfram.com/language/guide/FunctionalProgramming.html
|
101
|
-
|
102
|
-
end
|
99
|
+
describe "Basic Examples" do
|
103
100
|
|
101
|
+
example "table(lambda, [n])" do
|
102
|
+
expect(MLL::table(->(i){ i**2 }, [10])).to eq [1,4,9,16,25,36,49,64,81,100]
|
103
|
+
end
|
104
|
+
example "table(lambda, [imin, imax, id])" do
|
105
|
+
expect(MLL::table(->(i){ i+2 }, [0, 20, 2])).to eq [2,4,6,8,10,12,14,16,18,20,22]
|
106
|
+
end
|
107
|
+
example "table(lambda, [n1], [n2])" do
|
108
|
+
expect(MLL::table(->(i, j){ 10*i + j }, [4], [3])).to eq [[11,12,13],[21,22,23],[31,32,33],[41,42,43]]
|
109
|
+
end
|
104
110
|
|
105
|
-
|
111
|
+
example "matrix_form table(lambda, [n1], [n2])" do
|
112
|
+
pending "#matrix_form is yet to be implemented"
|
113
|
+
expect(MLL::matrix_form MLL::table(->(i, j){ 10*i + j }, [4], [3])).to eq "
|
114
|
+
"
|
115
|
+
end
|
106
116
|
|
107
|
-
|
117
|
+
end
|
108
118
|
|
109
|
-
|
110
|
-
expect(5.range).to be_a Enumerator
|
111
|
-
expect(5.range.to_a).to eq [1,2,3,4,5]
|
112
|
-
end
|
119
|
+
end
|
113
120
|
|
114
|
-
example "[imin, imax].range" do
|
115
|
-
expect([2,5].range).to be_a Enumerator
|
116
|
-
expect([2,5].range.to_a).to eq [2,3,4,5]
|
117
|
-
end
|
118
|
-
example "[imin, imax, id].range" do
|
119
|
-
expect([2,5,2].range).to be_a Enumerator
|
120
|
-
expect([2,5,2].range.to_a).to eq [2,4]
|
121
|
-
expect([2,6,2].range.to_a).to eq [2,4,6]
|
122
|
-
end
|
123
|
-
example "[[n1,n2]].range" do
|
124
|
-
# expect([[2,3]].range).to be_a Enumerator # TODO revive me
|
125
|
-
[[2,3]].range.each do |i|
|
126
|
-
expect(i).to be_a Enumerator
|
127
121
|
end
|
128
|
-
# expect([[2,3]].range.map(&:to_a)).to eq [[1,2],[1,2,3]]
|
129
|
-
expect([[2,3]].range.map(&:to_a)).to eq [[1,2],[1,2,3]]
|
130
|
-
end
|
131
122
|
|
132
|
-
|
133
|
-
|
134
|
-
#
|
135
|
-
|
123
|
+
# http://reference.wolfram.com/language/guide/ElementsOfLists.html
|
124
|
+
# http://reference.wolfram.com/language/guide/RearrangingAndRestructuringLists.html
|
125
|
+
# http://reference.wolfram.com/language/guide/ApplyingFunctionsToLists.html
|
126
|
+
# http://reference.wolfram.com/language/guide/MathematicalAndCountingOperationsOnLists.html
|
127
|
+
|
136
128
|
end
|
137
129
|
|
138
|
-
#
|
130
|
+
# http://reference.wolfram.com/language/guide/FunctionalProgramming.html
|
139
131
|
|
140
132
|
end
|
141
133
|
|
142
|
-
|
134
|
+
# http://reference.wolfram.com/language/guide/HandlingArraysOfData.html
|
135
|
+
# http://reference.wolfram.com/language/guide/ComputationWithStructuredDatasets.html
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mll
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Victor Maslov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-05-
|
11
|
+
date: 2015-05-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|