scaruby 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/scaruby/converter.rb +22 -0
- data/lib/scaruby/core_ext/enumerable.rb +0 -1
- data/lib/scaruby/map.rb +7 -1
- data/lib/scaruby/seq.rb +16 -20
- data/lib/scaruby/version.rb +1 -1
- data/readme.md +24 -2
- data/spec/scaruby/converter_spec.rb +27 -0
- data/spec/scaruby/core_ext/enumerable_spec.rb +1 -1
- data/spec/scaruby/core_ext/hash_spec.rb +1 -1
- data/spec/scaruby/map_spec.rb +14 -0
- data/spec/scaruby/seq_spec.rb +15 -0
- metadata +4 -1
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'scaruby/seq'
|
4
|
+
require 'scaruby/map'
|
5
|
+
|
6
|
+
module Enumerable
|
7
|
+
def to_scaruby
|
8
|
+
Scaruby::Seq.new(self)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class Hash
|
13
|
+
def to_scaruby
|
14
|
+
Scaruby::Map.new(self)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class Object
|
19
|
+
def to_option
|
20
|
+
Scaruby::Option.new(self)
|
21
|
+
end
|
22
|
+
end
|
data/lib/scaruby/map.rb
CHANGED
data/lib/scaruby/seq.rb
CHANGED
@@ -2,6 +2,13 @@
|
|
2
2
|
|
3
3
|
module Scaruby
|
4
4
|
class Seq
|
5
|
+
include Enumerable
|
6
|
+
|
7
|
+
def each(&block)
|
8
|
+
@array.each do |e|
|
9
|
+
yield e
|
10
|
+
end
|
11
|
+
end
|
5
12
|
|
6
13
|
def self.apply(enumerable)
|
7
14
|
Seq.new(enumerable)
|
@@ -32,7 +39,7 @@ module Scaruby
|
|
32
39
|
end
|
33
40
|
|
34
41
|
def count(&predicate)
|
35
|
-
|
42
|
+
@array.count(&predicate)
|
36
43
|
end
|
37
44
|
|
38
45
|
def diff(that)
|
@@ -81,13 +88,7 @@ module Scaruby
|
|
81
88
|
end
|
82
89
|
|
83
90
|
def exists(&predicate)
|
84
|
-
@array.
|
85
|
-
if found then
|
86
|
-
true
|
87
|
-
else
|
88
|
-
yield e
|
89
|
-
end
|
90
|
-
}
|
91
|
+
@array.any?(&predicate)
|
91
92
|
end
|
92
93
|
|
93
94
|
def filter(&predicate)
|
@@ -138,13 +139,7 @@ module Scaruby
|
|
138
139
|
end
|
139
140
|
|
140
141
|
def forall(&predicate)
|
141
|
-
@array.
|
142
|
-
if ! all_matched then
|
143
|
-
false
|
144
|
-
else
|
145
|
-
yield e
|
146
|
-
end
|
147
|
-
}
|
142
|
+
@array.all?(&predicate)
|
148
143
|
end
|
149
144
|
|
150
145
|
def foreach(&block)
|
@@ -238,11 +233,12 @@ module Scaruby
|
|
238
233
|
end
|
239
234
|
|
240
235
|
def partition(&predicate)
|
241
|
-
Seq.new(@array.inject([[],[]]) {|z,
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
236
|
+
Seq.new(@array.chunk(&predicate).inject([[],[]]) {|z, matched_and_elm|
|
237
|
+
matched, elm = matched_and_elm[0], matched_and_elm[1][0]
|
238
|
+
if matched then
|
239
|
+
[z[0].push(elm), z[1]]
|
240
|
+
else
|
241
|
+
[z[0],z[1].push(elm)]
|
246
242
|
end
|
247
243
|
})
|
248
244
|
end
|
data/lib/scaruby/version.rb
CHANGED
data/readme.md
CHANGED
@@ -46,9 +46,29 @@ irb(main):026:0> Map.new({123=>'abc',23=>'bc',345=>'cde'}).filter {|k,v| k.to_s.
|
|
46
46
|
=> {123=>"abc", 345=>"cde"}
|
47
47
|
```
|
48
48
|
|
49
|
-
|
49
|
+
`scaruby/converter` might be useful.
|
50
50
|
|
51
|
-
|
51
|
+
```ruby
|
52
|
+
irb(main):001:0> require 'scaruby/converter'
|
53
|
+
=> true
|
54
|
+
irb(main):002:0> 'abc'.to_option.is_defined
|
55
|
+
=> true
|
56
|
+
irb(main):003:0> 'abc'.to_option.get_or_else('zzz')
|
57
|
+
=> "abc"
|
58
|
+
irb(main):004:0> nil.to_option.is_defined
|
59
|
+
=> false
|
60
|
+
irb(main):005:0> [1,2,3].to_scaruby.foreach do |e| puts e end
|
61
|
+
1
|
62
|
+
2
|
63
|
+
3
|
64
|
+
=> [1, 2, 3]
|
65
|
+
```
|
66
|
+
|
67
|
+
It is also possible to extend Ruby with Scaruby.
|
68
|
+
|
69
|
+
If the method is missing, the method in Scaruby will be invoked.
|
70
|
+
|
71
|
+
In this case, the methods already defined (i.e. flat_map, find and so on) are never replaced.
|
52
72
|
|
53
73
|
```ruby
|
54
74
|
irb(main):015:0> require 'scaruby/core_ext'
|
@@ -70,4 +90,6 @@ irb(main):027:0> {123=>'abc',23=>'bc',345=>'cde'}.filter {|k,v| k.to_s.size == 3
|
|
70
90
|
|
71
91
|
MIT License
|
72
92
|
|
93
|
+
https://github.com/seratch/scaruby/blob/master/LICENSE.txt
|
94
|
+
|
73
95
|
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'scaruby/converter'
|
4
|
+
|
5
|
+
describe Enumerable do
|
6
|
+
it 'has #to_scaruby' do
|
7
|
+
[1,2,3].to_scaruby.filter {|e| e > 1 }.to_a.should eq([2,3])
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe Hash do
|
12
|
+
it 'has #to_scaruby' do
|
13
|
+
{123=>'abc',2=>'b',34=>'cd'}.to_scaruby.filter {|k,v|
|
14
|
+
k.to_s.length == 1
|
15
|
+
}.to_hash.should eq({2=>'b'})
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe Object do
|
20
|
+
it 'has #to_option' do
|
21
|
+
'abc'.to_option.is_defined.should eq(true)
|
22
|
+
'abc'.to_option.get_or_else('zzz').should eq('abc')
|
23
|
+
nil.to_option.is_defined.should eq(false)
|
24
|
+
nil.to_option.get_or_else('zzz').should eq('zzz')
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
data/spec/scaruby/map_spec.rb
CHANGED
@@ -10,6 +10,20 @@ describe Map do
|
|
10
10
|
map = Map.new({1 => 'a', 2 => 'b'})
|
11
11
|
map.should_not eq(nil)
|
12
12
|
end
|
13
|
+
|
14
|
+
# as a sub type of Hash
|
15
|
+
it 'has #each' do
|
16
|
+
expected_key = 1
|
17
|
+
expected_value = 10
|
18
|
+
Map.new({1=>10,2=>20}).each do |k,v|
|
19
|
+
k.should eq(expected_key)
|
20
|
+
v.should eq(expected_value)
|
21
|
+
expected_key += 1
|
22
|
+
expected_value += 10
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# defined
|
13
27
|
it 'has #contains' do
|
14
28
|
Map.new(hash).contains(123).should eq(true)
|
15
29
|
Map.new(hash).contains(999).should eq(false)
|
data/spec/scaruby/seq_spec.rb
CHANGED
@@ -17,6 +17,21 @@ describe Seq do
|
|
17
17
|
rescue ArgumentError
|
18
18
|
end
|
19
19
|
end
|
20
|
+
|
21
|
+
# as a sub type of Enumerable
|
22
|
+
it 'has #each' do
|
23
|
+
expected = 0
|
24
|
+
Seq.new([0,1,2,3]).each do |e|
|
25
|
+
e.should eq(expected)
|
26
|
+
expected += 1
|
27
|
+
end
|
28
|
+
end
|
29
|
+
it 'has #all?' do
|
30
|
+
Seq.new([1,2,3]).all? {|e| e < 4 }.should eq(true)
|
31
|
+
Seq.new([1,2,3]).all? {|e| e > 2 }.should eq(false)
|
32
|
+
end
|
33
|
+
|
34
|
+
# defined
|
20
35
|
it 'has #to_a' do
|
21
36
|
Seq.new(one_to_five).to_a.should eq([1,2,3,4,5])
|
22
37
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: scaruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.3
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Kazuhiro Sera
|
@@ -29,6 +29,7 @@ files:
|
|
29
29
|
- Rakefile
|
30
30
|
- lib/scaruby.rb
|
31
31
|
- lib/scaruby/appliable_proc.rb
|
32
|
+
- lib/scaruby/converter.rb
|
32
33
|
- lib/scaruby/core_ext.rb
|
33
34
|
- lib/scaruby/core_ext/enumerable.rb
|
34
35
|
- lib/scaruby/core_ext/hash.rb
|
@@ -39,6 +40,7 @@ files:
|
|
39
40
|
- lib/scaruby/version.rb
|
40
41
|
- readme.md
|
41
42
|
- scaruby.gemspec
|
43
|
+
- spec/scaruby/converter_spec.rb
|
42
44
|
- spec/scaruby/core_ext/enumerable_spec.rb
|
43
45
|
- spec/scaruby/core_ext/hash_spec.rb
|
44
46
|
- spec/scaruby/map_spec.rb
|
@@ -73,6 +75,7 @@ signing_key:
|
|
73
75
|
specification_version: 3
|
74
76
|
summary: Scala API in Ruby
|
75
77
|
test_files:
|
78
|
+
- spec/scaruby/converter_spec.rb
|
76
79
|
- spec/scaruby/core_ext/enumerable_spec.rb
|
77
80
|
- spec/scaruby/core_ext/hash_spec.rb
|
78
81
|
- spec/scaruby/map_spec.rb
|