scaruby 0.0.3 → 0.0.4
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.
- data/.travis.yml +11 -0
- data/lib/scaruby/core_ext.rb +0 -1
- data/lib/scaruby/map.rb +9 -34
- data/lib/scaruby/seq.rb +5 -4
- data/lib/scaruby/version.rb +1 -1
- data/readme.md +19 -3
- data/scaruby.gemspec +2 -3
- data/spec/scaruby/core_ext/enumerable_spec.rb +94 -94
- data/spec/scaruby/core_ext/hash_spec.rb +27 -27
- data/spec/scaruby/map_spec.rb +5 -3
- data/spec/scaruby/seq_spec.rb +4 -2
- metadata +21 -28
data/.travis.yml
ADDED
data/lib/scaruby/core_ext.rb
CHANGED
data/lib/scaruby/map.rb
CHANGED
@@ -7,6 +7,7 @@ module Scaruby
|
|
7
7
|
@hash.each do |k,v|
|
8
8
|
yield k, v
|
9
9
|
end
|
10
|
+
nil
|
10
11
|
end
|
11
12
|
|
12
13
|
def self.empty
|
@@ -30,24 +31,11 @@ module Scaruby
|
|
30
31
|
end
|
31
32
|
|
32
33
|
def count(&predicate)
|
33
|
-
count
|
34
|
-
@hash.each do |k,v|
|
35
|
-
if yield k, v then
|
36
|
-
count += 1
|
37
|
-
end
|
38
|
-
end
|
39
|
-
count
|
34
|
+
@hash.count(&predicate)
|
40
35
|
end
|
41
36
|
|
42
37
|
def exists(&predicate)
|
43
|
-
@hash.
|
44
|
-
if found then
|
45
|
-
true
|
46
|
-
else
|
47
|
-
k, v = kv[0], kv[1]
|
48
|
-
yield k, v
|
49
|
-
end
|
50
|
-
}
|
38
|
+
@hash.any?(&predicate)
|
51
39
|
end
|
52
40
|
|
53
41
|
def filter(&predicate)
|
@@ -63,31 +51,18 @@ module Scaruby
|
|
63
51
|
end
|
64
52
|
|
65
53
|
def find(&predicate)
|
66
|
-
Option.new(@hash.
|
67
|
-
found, matched_kv = z[0], z[1]
|
68
|
-
if found then
|
69
|
-
z
|
70
|
-
elsif yield kv[0], kv[1] then
|
71
|
-
[true,kv]
|
72
|
-
else
|
73
|
-
[false,[]]
|
74
|
-
end
|
75
|
-
}[1])
|
54
|
+
Option.new(@hash.find(&predicate))
|
76
55
|
end
|
77
56
|
|
78
57
|
def forall(&predicate)
|
79
|
-
@hash.
|
80
|
-
if !still_matched then
|
81
|
-
false
|
82
|
-
else
|
83
|
-
k, v = kv[0], kv[1]
|
84
|
-
yield k, v
|
85
|
-
end
|
86
|
-
}
|
58
|
+
@hash.all?(&predicate)
|
87
59
|
end
|
88
60
|
|
89
61
|
def foreach(&block)
|
90
|
-
@hash.each do |k,v|
|
62
|
+
@hash.each do |k,v|
|
63
|
+
yield k, v
|
64
|
+
end
|
65
|
+
nil
|
91
66
|
end
|
92
67
|
|
93
68
|
def get_or_else(key, default_value)
|
data/lib/scaruby/seq.rb
CHANGED
@@ -8,6 +8,7 @@ module Scaruby
|
|
8
8
|
@array.each do |e|
|
9
9
|
yield e
|
10
10
|
end
|
11
|
+
nil
|
11
12
|
end
|
12
13
|
|
13
14
|
def self.apply(enumerable)
|
@@ -146,6 +147,7 @@ module Scaruby
|
|
146
147
|
@array.each do |e|
|
147
148
|
yield e
|
148
149
|
end
|
150
|
+
nil
|
149
151
|
end
|
150
152
|
|
151
153
|
def group_by(&block)
|
@@ -244,10 +246,9 @@ module Scaruby
|
|
244
246
|
end
|
245
247
|
|
246
248
|
def patch(from, patch, replaced)
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
.concat(@array.drop(from).drop(replaced)))
|
249
|
+
# -- compatible with Ruby 1.8.7
|
250
|
+
#Seq.new(@array.take(from).concat(patch).concat(@array.drop(from).drop(replaced)))
|
251
|
+
Seq.new(@array.take(from) + patch + @array.drop(from).drop(replaced))
|
251
252
|
end
|
252
253
|
|
253
254
|
def reverse
|
data/lib/scaruby/version.rb
CHANGED
data/readme.md
CHANGED
@@ -1,9 +1,17 @@
|
|
1
1
|
# Scala API in Ruby
|
2
2
|
|
3
|
+
https://rubygems.org/gems/scaruby
|
4
|
+
|
3
5
|
Surely it works fine.
|
4
6
|
|
5
7
|
But it mainly serves as a Ruby reference for Scala programmers.
|
6
8
|
|
9
|
+
|
10
|
+
## Supported Ruby versions
|
11
|
+
|
12
|
+
Scaruby runs on Ruby 1.9.2, 1.9.3.
|
13
|
+
|
14
|
+
|
7
15
|
## Gemfile
|
8
16
|
|
9
17
|
```
|
@@ -12,10 +20,12 @@ source "http://rubygems.org/"
|
|
12
20
|
gem 'scaruby'
|
13
21
|
```
|
14
22
|
|
23
|
+
|
15
24
|
## Usage
|
16
25
|
|
17
26
|
See specs.
|
18
27
|
|
28
|
+
|
19
29
|
## Trying on irb
|
20
30
|
|
21
31
|
```sh
|
@@ -23,7 +33,7 @@ bundle console
|
|
23
33
|
```
|
24
34
|
|
25
35
|
```ruby
|
26
|
-
irb(main):008:0>
|
36
|
+
irb(main):008:0> require 'scaruby'
|
27
37
|
=> false
|
28
38
|
|
29
39
|
irb(main):009:0> Option.new(nil).is_defined
|
@@ -73,9 +83,9 @@ In this case, the methods already defined (i.e. flat_map, find and so on) are ne
|
|
73
83
|
```ruby
|
74
84
|
irb(main):015:0> require 'scaruby/core_ext'
|
75
85
|
=> true
|
76
|
-
irb(main):016:0>
|
86
|
+
irb(main):016:0> 1.upto(5).filter {|i| i < 3 }
|
77
87
|
=> [1, 2]
|
78
|
-
irb(main):020:0>
|
88
|
+
irb(main):020:0> 1.upto(5).filter {|i| i < 3 }.foreach do |i|
|
79
89
|
irb(main):021:1* puts i
|
80
90
|
irb(main):022:1> end
|
81
91
|
1
|
@@ -86,6 +96,12 @@ irb(main):027:0> {123=>'abc',23=>'bc',345=>'cde'}.filter {|k,v| k.to_s.size == 3
|
|
86
96
|
=> {123=>"abc", 345=>"cde"}
|
87
97
|
```
|
88
98
|
|
99
|
+
|
100
|
+
## Build Status
|
101
|
+
|
102
|
+
[](http://travis-ci.org/seratch/scaruby)
|
103
|
+
|
104
|
+
|
89
105
|
## License
|
90
106
|
|
91
107
|
MIT License
|
data/scaruby.gemspec
CHANGED
@@ -18,7 +18,6 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
19
|
s.require_paths = ["lib"]
|
20
20
|
|
21
|
-
|
22
|
-
|
23
|
-
# s.add_runtime_dependency "rest-client"
|
21
|
+
s.required_ruby_version = '>= 1.9.2'
|
22
|
+
|
24
23
|
end
|
@@ -8,64 +8,64 @@ describe Enumerable do
|
|
8
8
|
one_to_five_shuffled = one_to_five.sort_by {rand}
|
9
9
|
|
10
10
|
it 'has #to_a' do
|
11
|
-
|
11
|
+
one_to_five.to_a.should eq([1,2,3,4,5])
|
12
12
|
end
|
13
13
|
it 'has #corresponds' do
|
14
|
-
|
15
|
-
|
14
|
+
[1,2,3].corresponds([1,2,3]) {|a,b| a == b }.should eq(true)
|
15
|
+
[1,2,3].corresponds([3,1,2]) {|a,b| a == b }.should eq(false)
|
16
16
|
end
|
17
17
|
it 'has #count' do
|
18
|
-
|
18
|
+
one_to_five.count {|i| i > 2 }.should eq(3)
|
19
19
|
end
|
20
20
|
it 'has #diff' do
|
21
|
-
|
21
|
+
[1,2,3].diff([2,3,4]).to_a.should eq([1])
|
22
22
|
end
|
23
23
|
it 'has #distinct' do
|
24
|
-
|
24
|
+
[1,2,4,1,3,3,3,2,4,1].distinct.to_a.should eq([1,2,4,3])
|
25
25
|
end
|
26
26
|
it 'has #drop' do
|
27
|
-
|
28
|
-
|
27
|
+
one_to_five.drop(3).to_a.should eq([4,5])
|
28
|
+
one_to_five.drop(7).to_a.should eq([])
|
29
29
|
end
|
30
30
|
it 'has #drop_right' do
|
31
|
-
|
32
|
-
|
31
|
+
one_to_five.drop_right(3).to_a.should eq([1,2])
|
32
|
+
one_to_five.drop_right(7).to_a.should eq([])
|
33
33
|
end
|
34
34
|
it 'has #drop_while' do
|
35
|
-
|
35
|
+
[5,3,2,4,1].drop_while {|e| e > 2 }.to_a.should eq([2,4,1])
|
36
36
|
end
|
37
37
|
it 'has #ends_with' do
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
38
|
+
[1,2,3].ends_with([1,2]).should eq(false)
|
39
|
+
[1,2,3].ends_with([1,2,3]).should eq(true)
|
40
|
+
[1,2,3].ends_with([1,1,2,3]).should eq(false)
|
41
|
+
[1,2,3].ends_with([2,3]).should eq(true)
|
42
|
+
[1,2,3].ends_with([3]).should eq(true)
|
43
43
|
end
|
44
44
|
it 'has #exists' do
|
45
|
-
|
46
|
-
|
47
|
-
|
45
|
+
[1,2,3].exists {|i| i < 2 }.should eq(true)
|
46
|
+
[1,2,3].exists {|i| i < 4 }.should eq(true)
|
47
|
+
[2,3,4].exists {|i| i > 4 }.should eq(false)
|
48
48
|
end
|
49
49
|
it 'has #filter' do
|
50
|
-
|
51
|
-
|
50
|
+
one_to_five.filter {|i| i > 3 }.to_a.should eq([4,5])
|
51
|
+
one_to_five.filter {|i| i > 10 }.to_a.should eq([])
|
52
52
|
end
|
53
53
|
it 'has #filter_not' do
|
54
|
-
|
55
|
-
|
54
|
+
one_to_five.filter_not {|i| i > 3 }.to_a.should eq([1,2,3])
|
55
|
+
one_to_five.filter_not {|i| i > 10 }.to_a.should eq([1,2,3,4,5])
|
56
56
|
end
|
57
57
|
it 'has #find' do
|
58
|
-
some =
|
58
|
+
some = one_to_five.find {|i| i < 3 }
|
59
59
|
# the already defined method is called
|
60
60
|
#some.get.should eq(1)
|
61
61
|
some.should eq(1)
|
62
|
-
none =
|
62
|
+
none = one_to_five.find {|i| i > 10 }
|
63
63
|
# the already defined method is called
|
64
64
|
#none.is_defined.should eq(false)
|
65
65
|
none.should eq(nil)
|
66
66
|
end
|
67
67
|
it 'has #flat_map and it works with nested arrays' do
|
68
|
-
|
68
|
+
[[1,2],[3,4],[5]].flat_map {|i| i }.to_a.should eq([1,2,3,4,5])
|
69
69
|
end
|
70
70
|
it 'has #flat_map and it works with Option elements' do
|
71
71
|
# the already defined method is called
|
@@ -78,7 +78,7 @@ describe Enumerable do
|
|
78
78
|
input = [1,2,3]
|
79
79
|
expected = [1,2,3]
|
80
80
|
idx = 0
|
81
|
-
|
81
|
+
input.fold_left(0) {|z,x|
|
82
82
|
x.should eq(expected[idx])
|
83
83
|
idx += 1
|
84
84
|
z + x
|
@@ -88,42 +88,42 @@ describe Enumerable do
|
|
88
88
|
input = [1,2,3]
|
89
89
|
expected = [3,2,1]
|
90
90
|
idx = 0
|
91
|
-
|
91
|
+
input.fold_right(0) {|z,x|
|
92
92
|
x.should eq(expected[idx])
|
93
93
|
idx += 1
|
94
94
|
z + x
|
95
95
|
}.should eq(6)
|
96
96
|
end
|
97
97
|
it 'has #flatten' do
|
98
|
-
|
99
|
-
|
98
|
+
[[1,2],[3,4],[5]].flatten.to_a.should eq([1,2,3,4,5])
|
99
|
+
[Option.new(1),
|
100
100
|
Option.new(2),
|
101
101
|
Option.new(nil),
|
102
|
-
Option.new(3)]
|
103
|
-
).flatten.to_a.each do |opt|
|
102
|
+
Option.new(3)].flatten.to_a.each do |opt|
|
104
103
|
opt.is_a?(Option).should eq(true)
|
105
104
|
end
|
106
105
|
end
|
107
106
|
it 'has #forall' do
|
108
|
-
|
109
|
-
|
107
|
+
[1,2,3].forall {|i| i > 0 }.should eq(true)
|
108
|
+
[1,2,3].forall {|i| i > 1 }.should eq(false)
|
110
109
|
end
|
111
110
|
it 'has #foreach' do
|
112
111
|
count = 0
|
113
|
-
([1,2,3]).foreach do |i|
|
112
|
+
returned = ([1,2,3]).foreach do |i|
|
114
113
|
count += 1
|
115
114
|
end
|
116
115
|
count.should eq(3)
|
116
|
+
returned.should eq(nil)
|
117
117
|
end
|
118
118
|
it 'has #group_by' do
|
119
119
|
expected = {3=>[3,3,3], 1=>[1,1,1], 2=>[2,2]}
|
120
|
-
|
120
|
+
[1,1,1,2,3,2,3,3].group_by {|i| i }.to_hash.should eq(expected)
|
121
121
|
end
|
122
122
|
it 'has #head' do
|
123
|
-
|
123
|
+
one_to_five.head.should eq(1)
|
124
124
|
end
|
125
125
|
it 'has #head_option and it works with Some' do
|
126
|
-
some =
|
126
|
+
some = one_to_five.head_option
|
127
127
|
some.get_or_else(999).should eq(1)
|
128
128
|
end
|
129
129
|
it 'has #head_option and it works with None' do
|
@@ -131,30 +131,30 @@ describe Enumerable do
|
|
131
131
|
none.get_or_else(999).should eq(999)
|
132
132
|
end
|
133
133
|
it 'has #indices' do
|
134
|
-
|
134
|
+
[1,2,3].indices.to_a.should eq([0,1,2])
|
135
135
|
end
|
136
136
|
it 'has #init' do
|
137
|
-
|
137
|
+
[1,2,3].init.to_a.should eq([1,2])
|
138
138
|
end
|
139
139
|
it 'has #intersect' do
|
140
|
-
|
140
|
+
[1,2,3].intersect([2,3,4]).to_a.should eq([2,3])
|
141
141
|
end
|
142
142
|
it 'has #is_empty' do
|
143
|
-
|
144
|
-
|
145
|
-
|
143
|
+
[1,2,3].is_empty.should eq(false)
|
144
|
+
[nil].is_empty.should eq(false)
|
145
|
+
[].is_empty.should eq(true)
|
146
146
|
end
|
147
147
|
it 'has #last' do
|
148
|
-
|
148
|
+
[1,2,3].last.should eq(3)
|
149
149
|
end
|
150
150
|
it 'has #last_option' do
|
151
|
-
some =
|
151
|
+
some = [1,2,3].last_option
|
152
152
|
some.get.should eq(3)
|
153
153
|
none = ([]).last_option
|
154
154
|
none.is_defined.should eq(false)
|
155
155
|
end
|
156
156
|
it 'has #lift' do
|
157
|
-
seq_lift =
|
157
|
+
seq_lift = [1,2,3].lift
|
158
158
|
seq_lift.apply(0).get.should eq(1)
|
159
159
|
seq_lift.apply(1).get.should eq(2)
|
160
160
|
seq_lift.apply(2).get.should eq(3)
|
@@ -165,50 +165,50 @@ describe Enumerable do
|
|
165
165
|
seq_lift.call(3).is_defined.should eq(false)
|
166
166
|
end
|
167
167
|
it 'has #map' do
|
168
|
-
|
168
|
+
[1,2,3].map {|i| i + i }.to_a.should eq([2,4,6])
|
169
169
|
end
|
170
170
|
it 'has #max' do
|
171
|
-
|
171
|
+
one_to_five_shuffled.max.should eq(5)
|
172
172
|
end
|
173
173
|
it 'has #min' do
|
174
|
-
|
174
|
+
one_to_five_shuffled.min.should eq(1)
|
175
175
|
end
|
176
176
|
it 'has #mk_string' do
|
177
|
-
|
178
|
-
|
179
|
-
|
177
|
+
one_to_five.mk_string.should eq('12345')
|
178
|
+
one_to_five.mk_string(',').should eq('1,2,3,4,5')
|
179
|
+
one_to_five.mk_string('^',',','$').should eq('^1,2,3,4,5$')
|
180
180
|
begin
|
181
|
-
|
181
|
+
one_to_five.mk_string('a','b').should eq(nil)
|
182
182
|
rescue ArgumentError
|
183
183
|
end
|
184
|
-
|
184
|
+
one_to_five.mk_string('^',',','$','zzz').should eq('^1,2,3,4,5$')
|
185
185
|
end
|
186
186
|
it 'has #non_empty' do
|
187
|
-
|
188
|
-
|
187
|
+
one_to_five.non_empty.should eq(true)
|
188
|
+
[].non_empty.should eq(false)
|
189
189
|
end
|
190
190
|
it 'has #partition' do
|
191
|
-
|
191
|
+
[5,2,3,1,4,2,3].partition {|i| i < 3 }.to_a.should eq([[2,1,2],[5,3,4,3]])
|
192
192
|
end
|
193
193
|
it 'has #patch' do
|
194
|
-
|
194
|
+
[5,2,3,1,4,2,3].patch(3,[111,222],3).to_a.should eq([5,2,3,111,222,3])
|
195
195
|
end
|
196
196
|
it 'has #reverse' do
|
197
|
-
|
197
|
+
[1,2,3].reverse.to_a.should eq([3,2,1])
|
198
198
|
end
|
199
199
|
it 'has #reverse_map' do
|
200
|
-
|
200
|
+
[1,2,3].reverse_map {|i| i + i }.to_a.should eq([6,4,2])
|
201
201
|
end
|
202
202
|
it 'has #same_elements' do
|
203
|
-
|
204
|
-
|
205
|
-
|
203
|
+
[1,2,3].same_elements([1,2,3]).should eq(true)
|
204
|
+
[1,2,3].same_elements([1,3,2]).should eq(false)
|
205
|
+
[1,2,3].same_elements([1,2]).should eq(false)
|
206
206
|
end
|
207
207
|
it 'has #scan_left' do
|
208
|
-
|
208
|
+
[1,2,3].scan_left(1) {|a,b| a + b }.to_a.should eq([1,2,4,7])
|
209
209
|
end
|
210
210
|
it 'has #scan_right' do
|
211
|
-
|
211
|
+
[1,2,3].scan_right(1) {|a,b| a + b }.to_a.should eq([7,6,4,1])
|
212
212
|
end
|
213
213
|
it 'has #slice' do
|
214
214
|
# the already defined method is called
|
@@ -222,54 +222,54 @@ describe Enumerable do
|
|
222
222
|
[1,2,3,4,5].slice(1,4).should eq([2,3,4,5])
|
223
223
|
end
|
224
224
|
it 'has #sliding' do
|
225
|
-
|
225
|
+
[1,2,3,4,5].sliding(2).to_a.should eq([[1,2],[2,3],[3,4],[4,5]])
|
226
226
|
end
|
227
227
|
it 'has #sort_with' do
|
228
|
-
|
228
|
+
[1,3,2,4,5].sort_with {|a,b| b <=> a }.to_a.should eq([5,4,3,2,1])
|
229
229
|
end
|
230
230
|
it 'has #span' do
|
231
|
-
|
231
|
+
[1,2,3,2,1].span {|i| i < 3 }.to_a.should eq([[1,2],[3,2,1]])
|
232
232
|
end
|
233
233
|
it 'has #split_at' do
|
234
|
-
|
234
|
+
[1,2,3,2,1].split_at(3).to_a.should eq([[1,2,3],[2,1]])
|
235
235
|
end
|
236
236
|
it 'has #starts_with' do
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
237
|
+
[1,2,3].starts_with([1]).should eq(true)
|
238
|
+
[1,2,3].starts_with([1,2]).should eq(true)
|
239
|
+
[1,2,3].starts_with([1,2,3]).should eq(true)
|
240
|
+
[1,2,3].starts_with([1,2,3,4]).should eq(false)
|
241
|
+
[1,2,3].starts_with([2,3]).should eq(false)
|
242
|
+
[1,2,3].starts_with([4,1,2,3]).should eq(false)
|
243
243
|
end
|
244
244
|
it 'has #sum' do
|
245
|
-
|
245
|
+
[1,2,3].sum.should eq(6)
|
246
246
|
end
|
247
247
|
it 'has #tail' do
|
248
|
-
|
249
|
-
|
248
|
+
[1,2,3].tail.to_a.should eq([2,3])
|
249
|
+
[].tail.to_a.should eq([])
|
250
250
|
end
|
251
251
|
it 'has #take' do
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
252
|
+
[1,2,3].take(0).to_a.should eq([])
|
253
|
+
[1,2,3].take(1).to_a.should eq([1])
|
254
|
+
[1,2,3].take(2).to_a.should eq([1,2])
|
255
|
+
[1,2,3].take(3).to_a.should eq([1,2,3])
|
256
|
+
[1,2,3].take(4).to_a.should eq([1,2,3])
|
257
257
|
end
|
258
258
|
it 'has #take_right' do
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
259
|
+
[1,2,3].take_right(0).to_a.should eq([])
|
260
|
+
[1,2,3].take_right(1).to_a.should eq([3])
|
261
|
+
[1,2,3].take_right(2).to_a.should eq([2,3])
|
262
|
+
[1,2,3].take_right(3).to_a.should eq([1,2,3])
|
263
|
+
[1,2,3].take_right(4).to_a.should eq([1,2,3])
|
264
264
|
end
|
265
265
|
it 'has #take_while' do
|
266
|
-
|
266
|
+
[5,3,2,4,1].take_while {|e| e > 2 }.to_a.should eq([5,3])
|
267
267
|
end
|
268
268
|
it 'has #union' do
|
269
|
-
|
269
|
+
[1,2,3].union([2,3,4]).to_a.should eq([1,2,3,2,3,4])
|
270
270
|
end
|
271
271
|
it 'has #updated' do
|
272
|
-
|
272
|
+
[1,2,3].updated(1,999).to_a.should eq([1,999,3])
|
273
273
|
end
|
274
274
|
it 'has #zip' do
|
275
275
|
# the already defined method is called
|
@@ -281,8 +281,8 @@ describe Enumerable do
|
|
281
281
|
[1,2,3].zip([2,3,4,5]).should eq([[1,2],[2,3],[3,4]])
|
282
282
|
end
|
283
283
|
it 'has #zip_with_index' do
|
284
|
-
|
285
|
-
|
284
|
+
[].zip_with_index.to_a.should eq([])
|
285
|
+
[1,2,3].zip_with_index.to_a.should eq([[1,0],[2,1],[3,2]])
|
286
286
|
end
|
287
287
|
|
288
288
|
end
|
@@ -7,54 +7,54 @@ describe Hash do
|
|
7
7
|
hash = {123 => 'abc', 234 => 'bcd', 345 => 'cde', 4 => 'd', 56 => 'ef', 7 => 'g', 89 => 'hi' }
|
8
8
|
|
9
9
|
it 'has #contains' do
|
10
|
-
|
11
|
-
|
10
|
+
hash.contains(123).should eq(true)
|
11
|
+
hash.contains(999).should eq(false)
|
12
12
|
end
|
13
13
|
it 'has #count' do
|
14
|
-
|
14
|
+
hash.count {|k,v| k.to_s.length >= 2 }.should eq(5)
|
15
15
|
end
|
16
16
|
it 'hash Map.empty' do
|
17
17
|
Map.empty.should eq({})
|
18
18
|
end
|
19
19
|
it 'has #exists' do
|
20
|
-
|
20
|
+
hash.exists {|k,v| k.to_s.length == 1 }.should eq(true)
|
21
21
|
end
|
22
22
|
it 'has #filter' do
|
23
|
-
|
23
|
+
hash.filter {|k,v| k.to_s.length < 3 }.to_hash.size.should eq(4)
|
24
24
|
end
|
25
25
|
it 'has #filter_keys' do
|
26
|
-
|
26
|
+
hash.filter_keys {|k| k.to_s.length < 3 }.to_hash.size.should eq(4)
|
27
27
|
end
|
28
28
|
it 'has #filter_not' do
|
29
|
-
|
29
|
+
hash.filter_not {|k,v| k.to_s.length < 3 }.to_hash.to_s.should eq('{123=>"abc", 234=>"bcd", 345=>"cde"}')
|
30
30
|
end
|
31
31
|
it 'has #find' do
|
32
32
|
# the already defined method is called
|
33
|
-
#
|
34
|
-
|
33
|
+
#hash.find {|k,v| k.to_s.length == 2 }.get[1].should eq('ef')
|
34
|
+
hash.find {|k,v| k.to_s.length == 2 }[1].should eq('ef')
|
35
35
|
end
|
36
36
|
it 'has #forall' do
|
37
|
-
|
38
|
-
|
37
|
+
hash.forall {|k,v| k.to_s.length <= 3 }.should eq(true)
|
38
|
+
hash.forall {|k,v| k.to_s.length >= 2 }.should eq(false)
|
39
39
|
end
|
40
40
|
it 'has #foreach' do
|
41
|
-
|
41
|
+
hash.foreach do |k,v|
|
42
42
|
hash.include?(k).should eq(true)
|
43
43
|
end
|
44
44
|
end
|
45
45
|
it 'has #get_or_else' do
|
46
|
-
|
47
|
-
|
46
|
+
hash.get_or_else(123, 'xxx').should eq('abc')
|
47
|
+
hash.get_or_else(999, 'xxx').should eq('xxx')
|
48
48
|
end
|
49
49
|
it 'has #is_empty' do
|
50
|
-
|
51
|
-
|
50
|
+
hash.is_empty.should eq(false)
|
51
|
+
{}.is_empty.should eq(true)
|
52
52
|
end
|
53
53
|
it 'has #key_set' do
|
54
|
-
|
54
|
+
hash.key_set.should eq(hash.keys)
|
55
55
|
end
|
56
56
|
it 'has #lift' do
|
57
|
-
lifted =
|
57
|
+
lifted = hash.lift
|
58
58
|
lifted.apply(123).get.should eq('abc')
|
59
59
|
lifted.apply(999).is_defined.should eq(false)
|
60
60
|
lifted.call(123).get.should eq('abc')
|
@@ -62,29 +62,29 @@ describe Hash do
|
|
62
62
|
end
|
63
63
|
it 'has #map' do
|
64
64
|
# the already defined method is called
|
65
|
-
#new_map =
|
65
|
+
#new_map = hash.map {|k,v| [k+k,v] }.to_hash
|
66
66
|
#new_map.include?(246).should eq(true)
|
67
|
-
seq =
|
67
|
+
seq = hash.map {|k,v| [k+k,v] }
|
68
68
|
seq.include?([246,'abc']).should eq(true)
|
69
69
|
end
|
70
70
|
it 'has #minus' do
|
71
|
-
|
71
|
+
hash.minus(123,234,345).to_hash.should eq({4=>'d',56=>'ef',7=>'g',89=>'hi'})
|
72
72
|
end
|
73
73
|
it 'has #mk_string' do
|
74
|
-
|
74
|
+
hash.mk_string().should eq('{123=>abc, 234=>bcd, 345=>cde, 4=>d, 56=>ef, 7=>g, 89=>hi}')
|
75
75
|
end
|
76
76
|
it 'has #non_empty' do
|
77
|
-
|
78
|
-
|
77
|
+
hash.non_empty.should eq(true)
|
78
|
+
{}.non_empty.should eq(false)
|
79
79
|
end
|
80
80
|
it 'has #plus' do
|
81
|
-
|
81
|
+
{123=>'abc',234=>'bcd'}.plus([[345,'cde']]).to_hash.should eq({123=>'abc',234=>'bcd',345=>'cde'})
|
82
82
|
end
|
83
83
|
it 'has #updated' do
|
84
|
-
|
84
|
+
{123=>'abc',234=>'bcd'}.updated(345,'cde').to_hash.should eq({123=>'abc',234=>'bcd',345=>'cde'})
|
85
85
|
end
|
86
86
|
it 'has #unzip' do
|
87
|
-
unzipped =
|
87
|
+
unzipped = {123=>'abc',234=>'bcd'}.unzip.to_a
|
88
88
|
unzipped[0].should eq([123,234])
|
89
89
|
unzipped[1].should eq(['abc','bcd'])
|
90
90
|
end
|
data/spec/scaruby/map_spec.rb
CHANGED
@@ -15,12 +15,13 @@ describe Map do
|
|
15
15
|
it 'has #each' do
|
16
16
|
expected_key = 1
|
17
17
|
expected_value = 10
|
18
|
-
Map.new({1=>10,2=>20}).each do |k,v|
|
18
|
+
returned = Map.new({1=>10,2=>20}).each do |k,v|
|
19
19
|
k.should eq(expected_key)
|
20
20
|
v.should eq(expected_value)
|
21
21
|
expected_key += 1
|
22
22
|
expected_value += 10
|
23
23
|
end
|
24
|
+
returned.should eq(nil)
|
24
25
|
end
|
25
26
|
|
26
27
|
# defined
|
@@ -44,7 +45,7 @@ describe Map do
|
|
44
45
|
Map.new(hash).filter_keys {|k| k.to_s.length < 3 }.to_hash.size.should eq(4)
|
45
46
|
end
|
46
47
|
it 'has #filter_not' do
|
47
|
-
Map.new(hash).filter_not {|k| k.to_s.length < 3 }.to_hash.to_s.should eq('{123=>"abc", 234=>"bcd", 345=>"cde"}')
|
48
|
+
Map.new(hash).filter_not {|k,v| k.to_s.length < 3 }.to_hash.to_s.should eq('{123=>"abc", 234=>"bcd", 345=>"cde"}')
|
48
49
|
end
|
49
50
|
it 'has #find' do
|
50
51
|
Map.new(hash).find {|k,v| k.to_s.length == 2 }.get[1].should eq('ef')
|
@@ -54,9 +55,10 @@ describe Map do
|
|
54
55
|
Map.new(hash).forall {|k,v| k.to_s.length >= 2 }.should eq(false)
|
55
56
|
end
|
56
57
|
it 'has #foreach' do
|
57
|
-
Map.new(hash).foreach do |k,v|
|
58
|
+
returned = Map.new(hash).foreach do |k,v|
|
58
59
|
hash.include?(k).should eq(true)
|
59
60
|
end
|
61
|
+
returned.should eq(nil)
|
60
62
|
end
|
61
63
|
it 'has #get_or_else' do
|
62
64
|
Map.new(hash).get_or_else(123, 'xxx').should eq('abc')
|
data/spec/scaruby/seq_spec.rb
CHANGED
@@ -21,10 +21,11 @@ describe Seq do
|
|
21
21
|
# as a sub type of Enumerable
|
22
22
|
it 'has #each' do
|
23
23
|
expected = 0
|
24
|
-
Seq.new([0,1,2,3]).each do |e|
|
24
|
+
returned = Seq.new([0,1,2,3]).each do |e|
|
25
25
|
e.should eq(expected)
|
26
26
|
expected += 1
|
27
27
|
end
|
28
|
+
returned.should eq(nil)
|
28
29
|
end
|
29
30
|
it 'has #all?' do
|
30
31
|
Seq.new([1,2,3]).all? {|e| e < 4 }.should eq(true)
|
@@ -126,10 +127,11 @@ describe Seq do
|
|
126
127
|
end
|
127
128
|
it 'has #foreach' do
|
128
129
|
count = 0
|
129
|
-
Seq.new([1,2,3]).foreach do |i|
|
130
|
+
returned = Seq.new([1,2,3]).foreach do |i|
|
130
131
|
count += 1
|
131
132
|
end
|
132
133
|
count.should eq(3)
|
134
|
+
returned.should eq(nil)
|
133
135
|
end
|
134
136
|
it 'has #group_by' do
|
135
137
|
expected = {3=>[3,3,3], 1=>[1,1,1], 2=>[2,2]}
|
metadata
CHANGED
@@ -1,29 +1,25 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: scaruby
|
3
|
-
version: !ruby/object:Gem::Version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.4
|
4
5
|
prerelease:
|
5
|
-
version: 0.0.3
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
7
|
+
authors:
|
8
8
|
- Kazuhiro Sera
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
|
13
|
-
date: 2012-03-03 00:00:00 Z
|
12
|
+
date: 2012-03-05 00:00:00.000000000 Z
|
14
13
|
dependencies: []
|
15
|
-
|
16
14
|
description: Scala API in Ruby
|
17
|
-
email:
|
15
|
+
email:
|
18
16
|
- seratch@gmail.com
|
19
17
|
executables: []
|
20
|
-
|
21
18
|
extensions: []
|
22
|
-
|
23
19
|
extra_rdoc_files: []
|
24
|
-
|
25
|
-
files:
|
20
|
+
files:
|
26
21
|
- .gitignore
|
22
|
+
- .travis.yml
|
27
23
|
- Gemfile
|
28
24
|
- LICENSE.txt
|
29
25
|
- Rakefile
|
@@ -49,32 +45,29 @@ files:
|
|
49
45
|
- spec/scaruby_spec.rb
|
50
46
|
homepage: https://github.com/seratch/scaruby
|
51
47
|
licenses: []
|
52
|
-
|
53
48
|
post_install_message:
|
54
49
|
rdoc_options: []
|
55
|
-
|
56
|
-
require_paths:
|
50
|
+
require_paths:
|
57
51
|
- lib
|
58
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
53
|
none: false
|
60
|
-
requirements:
|
61
|
-
- -
|
62
|
-
- !ruby/object:Gem::Version
|
63
|
-
version:
|
64
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ! '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: 1.9.2
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
59
|
none: false
|
66
|
-
requirements:
|
67
|
-
- -
|
68
|
-
- !ruby/object:Gem::Version
|
69
|
-
version:
|
60
|
+
requirements:
|
61
|
+
- - ! '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
70
64
|
requirements: []
|
71
|
-
|
72
65
|
rubyforge_project: scaruby
|
73
|
-
rubygems_version: 1.8.
|
66
|
+
rubygems_version: 1.8.17
|
74
67
|
signing_key:
|
75
68
|
specification_version: 3
|
76
69
|
summary: Scala API in Ruby
|
77
|
-
test_files:
|
70
|
+
test_files:
|
78
71
|
- spec/scaruby/converter_spec.rb
|
79
72
|
- spec/scaruby/core_ext/enumerable_spec.rb
|
80
73
|
- spec/scaruby/core_ext/hash_spec.rb
|