scaruby 0.0.2 → 0.0.3

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.
@@ -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
@@ -3,7 +3,6 @@
3
3
  require 'scaruby/seq'
4
4
 
5
5
  module Enumerable
6
-
7
6
  def method_missing(name, *args, &block)
8
7
  result = Scaruby::Seq.new(self).send(name, *args, &block)
9
8
  if result.is_a?(Scaruby::Seq) then
@@ -1,7 +1,13 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
3
  module Scaruby
4
- class Map
4
+ class Map < Hash
5
+
6
+ def each(&block)
7
+ @hash.each do |k,v|
8
+ yield k, v
9
+ end
10
+ end
5
11
 
6
12
  def self.empty
7
13
  {}
@@ -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
- filter(&predicate).size
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.inject(false) {|found,e|
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.inject(true) {|all_matched,e|
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,x|
242
- if yield x then
243
- [z[0].push(x),z[1]]
244
- else
245
- [z[0],z[1].push(x)]
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
@@ -1,6 +1,6 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
3
  module Scaruby
4
- VERSION = "0.0.2"
4
+ VERSION = "0.0.3"
5
5
  end
6
6
 
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
- You can extend Ruby with Scaruby. If the method is missing, the method in Scaruby will be invoked.
49
+ `scaruby/converter` might be useful.
50
50
 
51
- Already defined methods (i.e. flat_map, find and so on) are never replaced.
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
+
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'scaruby/core_ext'
4
4
 
5
- describe Seq do
5
+ describe Enumerable do
6
6
 
7
7
  one_to_five = 1.upto 5
8
8
  one_to_five_shuffled = one_to_five.sort_by {rand}
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'scaruby/core_ext'
4
4
 
5
- describe Map do
5
+ describe Hash do
6
6
 
7
7
  hash = {123 => 'abc', 234 => 'bcd', 345 => 'cde', 4 => 'd', 56 => 'ef', 7 => 'g', 89 => 'hi' }
8
8
 
@@ -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)
@@ -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.2
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