mll 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +11 -15
- data/lib/mll.rb +57 -20
- data/mll.gemspec +2 -2
- data/spec/_spec.rb +132 -16
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d8b4f658534716d142d0dedb616584ab5a016b71
|
4
|
+
data.tar.gz: 5253d8e36e96f2e4dba389268d64215a41b2befb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 34e4ede0875ae7a9e5500bf10f081f4cb5dcecf9b357bc39b420886a190480b0e0ee07b499be9bf59cbb93cdea70aeeba5cb3a6b946a00cce7d7077076b96ef9
|
7
|
+
data.tar.gz: fe4688d9b0b2bcba1383bd7f8f6cfb51ff69e96be8d55d970adbe84fab34f80cdbfd7ee86f4d3d39840b097aa37c40effbe3ebcb37e15eebcfdfab70d884fe38
|
data/README.md
CHANGED
@@ -4,25 +4,21 @@
|
|
4
4
|
|
5
5
|
## Usage
|
6
6
|
|
7
|
-
|
7
|
+
~~Планируется несколько способов использования этой библиотеки -- как доступ к ней через `MLL::`, так и манкипатчинг стандартных типов, таких как Numeric и Array.~~
|
8
8
|
|
9
9
|
### Examples:
|
10
10
|
|
11
|
-
MLL::range(3).to_a
|
12
|
-
MLL::range(
|
11
|
+
MLL::range(3).to_a #=> [1, 2, 3]
|
12
|
+
MLL::range(2, 3).to_a #=> [
|
13
|
+
#<Enumerator: 1..2:step(1)>,
|
14
|
+
#<Enumerator: 1..3:step(1)>,
|
15
|
+
]
|
16
|
+
MLL::range(1..3) #=> [
|
17
|
+
#<Enumerator: 1..1:step(1)>,
|
18
|
+
#<Enumerator: 1..2:step(1)>,
|
19
|
+
#<Enumerator: 1..3:step(1)>
|
20
|
+
]
|
13
21
|
|
14
22
|
## Installation
|
15
23
|
|
16
|
-
Add this line to your application's Gemfile:
|
17
|
-
|
18
|
-
```ruby
|
19
|
-
gem 'mll'
|
20
|
-
```
|
21
|
-
|
22
|
-
And then execute:
|
23
|
-
|
24
|
-
$ bundle
|
25
|
-
|
26
|
-
Or install it yourself as:
|
27
|
-
|
28
24
|
$ gem install mll
|
data/lib/mll.rb
CHANGED
@@ -1,30 +1,67 @@
|
|
1
|
-
# http://reference.wolfram.com/language/guide/LanguageOverview.html
|
2
1
|
module MLL
|
3
2
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
# see also http://ruby-doc.org/core-1.9.3/Range.html#method-i-step
|
11
|
-
raise ArgumentError.new("wrong number of arguments (#{args.size} for 1..3)") unless (1..3).include? args.size
|
12
|
-
if args.size == 1
|
13
|
-
if args[0].respond_to?(:map)
|
14
|
-
args[0].map &method(:range)
|
3
|
+
def self.define_function_that_can_enumerate name, &block
|
4
|
+
# http://stackoverflow.com/a/12792313/322020
|
5
|
+
(class << self; self end).class_eval do
|
6
|
+
define_method name do |*args|
|
7
|
+
if args.size == 1 && args.first.respond_to?(:map)
|
8
|
+
args.first.map &method(name)
|
15
9
|
else
|
16
|
-
|
10
|
+
block.call *args
|
17
11
|
end
|
18
|
-
else
|
19
|
-
Range.new(args[0], args[1]).step(args[2] || 1)
|
20
12
|
end
|
21
13
|
end
|
14
|
+
end
|
15
|
+
|
16
|
+
define_function_that_can_enumerate :range do |*args|
|
17
|
+
raise ArgumentError.new("wrong number of arguments (#{args.size} for 1..3)") unless (1..3).include? args.size
|
18
|
+
next range 1, args[0] if args.size == 1
|
19
|
+
Range.new(args[0], args[1]).step(args[2] || 1)
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
22
24
|
|
23
|
-
|
24
|
-
# http://reference.wolfram.com/language/guide/RearrangingAndRestructuringLists.html
|
25
|
-
# http://reference.wolfram.com/language/guide/ApplyingFunctionsToLists.html
|
26
|
-
# http://reference.wolfram.com/language/guide/MathematicalAndCountingOperationsOnLists.html
|
25
|
+
=begin
|
27
26
|
|
28
|
-
|
27
|
+
# http://reference.wolfram.com/language/guide/HandlingArraysOfData.html
|
28
|
+
# http://reference.wolfram.com/language/guide/ComputationWithStructuredDatasets.html
|
29
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
|
38
|
+
else
|
39
|
+
case self.size
|
40
|
+
when 1 ; [1,self.first].range
|
41
|
+
when 2 ; Range.new(*self.take(2)).step(1) # to convert Rage into Enumerator
|
42
|
+
when 3 ; Range.new(*self.take(2)).step(self.drop(2).first)
|
43
|
+
when 4 ; raise ArgumentError.new("wrong number of arguments (#{self.size} for 1..3)")
|
44
|
+
end
|
45
|
+
end
|
46
|
+
# else
|
47
|
+
# [1, self].range
|
48
|
+
# end
|
49
|
+
end
|
30
50
|
end
|
51
|
+
|
52
|
+
class Fixnum
|
53
|
+
STDERR.puts "WARNING: #{name}#range was already defined" if method_defined? "range"
|
54
|
+
def range
|
55
|
+
# if self.respond_to? :each
|
56
|
+
# if self.first.respond_to? :each
|
57
|
+
# self.map &:range
|
58
|
+
# else
|
59
|
+
# Range.new(*self[0,2]).step(self[2] || 1)
|
60
|
+
# end
|
61
|
+
# else
|
62
|
+
[self].range
|
63
|
+
# end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
=end
|
data/mll.gemspec
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
Gem::Specification.new do |spec|
|
2
2
|
spec.name = "mll"
|
3
|
-
spec.version = "0.0
|
3
|
+
spec.version = "0.1.0"
|
4
4
|
spec.authors = ["Victor Maslov"]
|
5
5
|
spec.email = ["nakilon@gmail.com"]
|
6
6
|
spec.summary = "Mathematica Language Library in Ruby"
|
7
7
|
spec.description = ""
|
8
|
-
|
8
|
+
spec.homepage = "https://github.com/Nakilon/mll"
|
9
9
|
spec.license = "MIT"
|
10
10
|
|
11
11
|
spec.files = `git ls-files -z`.split("\x0")
|
data/spec/_spec.rb
CHANGED
@@ -1,26 +1,142 @@
|
|
1
1
|
require_relative File.join "..", "lib", "mll"
|
2
2
|
|
3
|
-
describe "mll" do
|
4
3
|
|
5
|
-
|
4
|
+
# http://reference.wolfram.com/language/guide/LanguageOverview.html
|
5
|
+
describe MLL do
|
6
|
+
|
7
|
+
# http://reference.wolfram.com/language/guide/ListManipulation.html
|
8
|
+
describe "List Manipulation" do
|
9
|
+
|
10
|
+
# http://reference.wolfram.com/language/guide/ConstructingLists.html
|
11
|
+
describe "Constructing Lists" do
|
12
|
+
|
13
|
+
# http://reference.wolfram.com/language/ref/Range.html
|
14
|
+
describe "range" do
|
15
|
+
|
16
|
+
example "range( >3 args )" do
|
17
|
+
expect{ MLL::range(1,2,3,4) }.to raise_error ArgumentError
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "Basic Examples" do
|
21
|
+
|
22
|
+
example "range(n)" do
|
23
|
+
expect(MLL::range(4)).to be_a Enumerator
|
24
|
+
expect(MLL::range(4).to_a).to eq [1,2,3,4]
|
25
|
+
end
|
26
|
+
example "range(imin, imax)" do
|
27
|
+
expect(MLL::range(2,5)).to be_a Enumerator
|
28
|
+
expect(MLL::range(2,5).to_a).to eq [2,3,4,5]
|
29
|
+
end
|
30
|
+
example "range(imin, imax, id)" do
|
31
|
+
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]
|
33
|
+
expect(MLL::range(2,6,2).to_a).to eq [2,4,6]
|
34
|
+
expect(MLL::range(-4,9,3).to_a).to eq [-4,-1,2,5,8]
|
35
|
+
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
|
+
end
|
42
|
+
|
43
|
+
describe "Generalizations & Extensions" do
|
44
|
+
|
45
|
+
example "range([n1, n2, n3, n4])" do
|
46
|
+
# expect(MLL::range([2,3])).to be_a Enumerator # TODO revive me
|
47
|
+
MLL::range([5,2,6,3]).each do |i|
|
48
|
+
expect(i).to be_a Enumerator
|
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]]
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
describe "Applications" do
|
56
|
+
|
57
|
+
example "power(2, range(5))" do
|
58
|
+
pending "#power is yet to be implemented"
|
59
|
+
p power(2, range(5))
|
60
|
+
expect(power(2, range(5))).to eq [2,4,8,16,32]
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
describe "Neat Examples" do
|
66
|
+
|
67
|
+
example "range(1..3)" do
|
68
|
+
# expect(MLL::range(MLL::range(3))).to be_a Enumerator # TODO revive me
|
69
|
+
MLL::range(MLL::range(3)).each do |i|
|
70
|
+
expect(i).to be_a Enumerator
|
71
|
+
end
|
72
|
+
expect(MLL::range(1..3).map(&:to_a)).to eq [[1],[1,2],[1,2,3]]
|
73
|
+
end
|
74
|
+
example "range(range(range(n)))" do
|
75
|
+
# expect(MLL::range(MLL::range(3))).to be_a Enumerator # TODO revive me
|
76
|
+
# MLL::range(MLL::range(MLL::range(3))).each do |i|
|
77
|
+
# expect(i).to be_a Enumerator
|
78
|
+
# end
|
79
|
+
expect(MLL::range(MLL::range(MLL::range(3))).map{ |i| i.map(&:to_a) }).to eq [[[1]],[[1],[1,2]],[[1],[1,2],[1,2,3]]]
|
80
|
+
end
|
81
|
+
|
82
|
+
# TODO Nest[Range,3,6]
|
83
|
+
|
84
|
+
end
|
85
|
+
|
86
|
+
# TODO "Find an inverse permutation:"
|
87
|
+
# TODO "Use Range or Span (;;) as Part specification:"
|
88
|
+
|
89
|
+
end
|
6
90
|
|
7
|
-
example "range(n)" do
|
8
|
-
expect(MLL::range(5).to_a).to eq [1,2,3,4,5]
|
9
|
-
end
|
10
|
-
example "range(imin, imax)" do
|
11
|
-
expect(MLL::range(2,5).to_a).to eq [2,3,4,5]
|
12
|
-
end
|
13
|
-
example "range(imin, imax, id)" do
|
14
|
-
expect(MLL::range(2,5,2).to_a).to eq [2,4]
|
15
|
-
expect(MLL::range(2,6,2).to_a).to eq [2,4,6]
|
16
|
-
end
|
17
|
-
example "range([n1,n2])" do
|
18
|
-
expect(MLL::range([2,3]).map(&:to_a)).to eq [[1,2],[1,2,3]]
|
19
91
|
end
|
20
|
-
|
21
|
-
|
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
|
97
|
+
|
98
|
+
end
|
99
|
+
|
100
|
+
# http://reference.wolfram.com/language/guide/FunctionalProgramming.html
|
101
|
+
|
102
|
+
end
|
103
|
+
|
104
|
+
|
105
|
+
=begin
|
106
|
+
|
107
|
+
describe "range" do
|
108
|
+
|
109
|
+
example "n.range" do
|
110
|
+
expect(5.range).to be_a Enumerator
|
111
|
+
expect(5.range.to_a).to eq [1,2,3,4,5]
|
112
|
+
end
|
113
|
+
|
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
|
22
127
|
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
|
23
131
|
|
132
|
+
# using methods instead of functions helps us with method_missing but forces to add []
|
133
|
+
example "n.range.range" do
|
134
|
+
# expect([3.range].range).to be_a Enumerator # TODO revive me
|
135
|
+
expect([3.range].range.map(&:to_a)).to eq [[1],[1,2],[1,2,3]]
|
24
136
|
end
|
25
137
|
|
138
|
+
# TODO 4 args should fail
|
139
|
+
|
26
140
|
end
|
141
|
+
|
142
|
+
=end
|
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.0
|
4
|
+
version: 0.1.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-
|
11
|
+
date: 2015-05-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,7 +52,7 @@ files:
|
|
52
52
|
- lib/mll.rb
|
53
53
|
- mll.gemspec
|
54
54
|
- spec/_spec.rb
|
55
|
-
homepage:
|
55
|
+
homepage: https://github.com/Nakilon/mll
|
56
56
|
licenses:
|
57
57
|
- MIT
|
58
58
|
metadata: {}
|