totally_lazy 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 580d257ae0a5c6db629420f25fc435870cbd09d7
4
- data.tar.gz: 428a631231457eb51abd105a07a91b8ad5517b26
3
+ metadata.gz: 6e482f2fe314ee171197af7ff51b88e2d8358496
4
+ data.tar.gz: e937ed5db278d9fd2deb1d58b8e4b7090f7802cb
5
5
  SHA512:
6
- metadata.gz: 831ab147b15383e7fc2ff86c4d85636cc3a93ae14c64efa2b0a4a2cf06c0cb89ff6f43ffe747273252f595213f4c0e13c5943646e4b2e786c2095272a428de4d
7
- data.tar.gz: aeff00bdcac329c9554adca869f9c022d7c9e11eef72958ae61e434a0a590dcff45b14a9aad03ce0af3ed15d685b2718c0ce10b992420e9886ed747a6e0bc6d5
6
+ metadata.gz: 52347e5e2810a2a807f175df3cca581713426f7708530e764994a9b19525bc38488a3de8f9d0a0fdfbecc0c6b81dcc93b3aca60a4ff490131862000a6b225975
7
+ data.tar.gz: 17aba7a1fc6b30ffddf49aa80db50d516aecb729e7d5ed2f6c4d83124ebb4c118f5c37cc8596cdac0c338360a1ae963fc0883bc742d4b0d5a389924b7fdb47b1
data/README.md CHANGED
@@ -13,7 +13,7 @@ This is a port of the java functional library [Totally Lazy](https://code.google
13
13
  In your bundler Gemfile
14
14
 
15
15
  ```ruby
16
- gem totally_lazy, '~>0.0.3'
16
+ gem totally_lazy, '~>0.0.4'
17
17
  ```
18
18
 
19
19
  Or with rubygems
@@ -30,7 +30,7 @@ The following are some simple examples of the currently implemented functionalit
30
30
  require 'totally_lazy'
31
31
 
32
32
  sequence(1,2,3,4).filter(even) # lazily returns 2,4
33
- sequence(1,2).map(to_string) # lazily returns "1","2"
33
+ sequence(1,2).map(as_string) # lazily returns "1","2"
34
34
  sequence(1,2,3).take(2) # lazily returns 1,2
35
35
  sequence(1,2,3).drop(2) # lazily returns 3
36
36
  sequence(1,2,3).tail # lazily returns 2,3
@@ -42,6 +42,6 @@ some(sequence(1,2,3)).get_or_else(empty) # eagerly returns value or else empty s
42
42
  Naturally you can combine these operations together:
43
43
 
44
44
  ```ruby
45
- option(1).join(sequence(2,3,4)).join(sequence(5,6)).filter{|i| i.odd?}.take(2)
45
+ option(1).join(sequence(2,3,4)).join(sequence(5,6)).filter(odd).take(2)
46
46
  # lazily returns 1,3
47
47
  ```
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.4
data/lib/pair.rb CHANGED
@@ -18,11 +18,13 @@ module Pair
18
18
  def first
19
19
  @first.call
20
20
  end
21
+
21
22
  alias key first
22
23
 
23
24
  def second
24
25
  @second.call
25
26
  end
27
+
26
28
  alias value second
27
29
 
28
30
  def to_map
@@ -30,11 +32,22 @@ module Pair
30
32
  end
31
33
 
32
34
  def self.from_map(a_map)
33
- sequence(a_map).map{|k,v| Pair.new(k,v)}
35
+ sequence(a_map).map { |k, v| Pair.new(k, v) }
34
36
  end
35
37
 
36
38
  def to_s
37
- to_map.inspect
39
+ Type.responds_all(sequence(first, second), :to_s)
40
+ {first.to_s => second.to_s}
41
+ end
42
+
43
+ def to_i
44
+ Type.responds_all(sequence(first, second), :to_i)
45
+ {first.to_i => second.to_i}
46
+ end
47
+
48
+ def to_f
49
+ Type.responds_all(sequence(first, second), :to_f)
50
+ {first.to_f => second.to_f}
38
51
  end
39
52
 
40
53
  def values
data/lib/predicates.rb CHANGED
@@ -14,19 +14,19 @@ module Predicates
14
14
 
15
15
  module Conversions
16
16
 
17
- def to_string
17
+ def as_string
18
18
  -> (v) { v.to_s }
19
19
  end
20
20
 
21
- def to_int
21
+ def as_int
22
22
  -> (v) { Type.responds(v, :to_i); v.to_i }
23
23
  end
24
24
 
25
- def to_float
25
+ def as_float
26
26
  -> (v) { Type.responds(v, :to_i); v.to_f }
27
27
  end
28
28
 
29
- def to_array
29
+ def as_array
30
30
  -> (v) { [v] }
31
31
  end
32
32
 
data/lib/totally_lazy.rb CHANGED
@@ -11,3 +11,6 @@ include Pair
11
11
  include Predicates::Numbers
12
12
  include Predicates::Conversions
13
13
 
14
+
15
+
16
+
data/lib/type_check.rb CHANGED
@@ -2,10 +2,18 @@ class Type
2
2
 
3
3
  def self.check(actual, expected)
4
4
  raise(UnsupportedTypeException.new, "Target must be of type: #{expected} but was: #{actual.class}") unless actual.kind_of?(expected)
5
+ actual
5
6
  end
6
7
 
7
- def self.responds(value,meth)
8
- raise(UnsupportedTypeException.new, "Target must respond to: #{meth} - but did not with: #{value.class} of: #{value}") unless value.respond_to?(meth)
8
+ def self.responds(value, meth)
9
+ raise(UnsupportedTypeException.new, "Target must respond to: #{meth} - but did not with: #{value.class} of: #{value.inspect}") unless value.respond_to?(meth)
10
+ value
11
+ end
12
+
13
+ def self.responds_all(values, meth)
14
+ values.each do |value|
15
+ raise(UnsupportedTypeException.new, "Target must respond to: #{meth} - but did not with: #{value.class} of: #{value.inspect}") unless value.respond_to?(meth)
16
+ end
9
17
  end
10
18
 
11
19
  end
data/spec/pair_spec.rb CHANGED
@@ -14,19 +14,27 @@ describe 'Pair' do
14
14
  end
15
15
 
16
16
  it 'should return sequence of values' do
17
- expect(pair(1,2).values).to eq(sequence(1,2))
17
+ expect(pair(1, 2).values).to eq(sequence(1, 2))
18
18
  end
19
19
 
20
20
  it 'should convert to string' do
21
- expect(pair('apples',2).to_s).to eq('{"apples"=>2}')
21
+ expect(pair('apples', 2).to_s).to eq({'apples' => '2'})
22
22
  end
23
23
 
24
24
  it 'should convert to a sequence of pairs from a map' do
25
- expect(Pair.from_map({apples:'10'}).to_a).to eq(sequence(pair(:apples,'10')).to_a)
25
+ expect(Pair.from_map({apples: '10'}).to_a).to eq(sequence(pair(:apples, '10')).to_a)
26
26
  end
27
27
 
28
28
  it 'should convert a pair to a map' do
29
- expect(pair(1,2).to_map).to eq({1=>2})
29
+ expect(pair(1, 2).to_map).to eq({1 => 2})
30
+ end
31
+
32
+ it 'should convert to int' do
33
+ expect(pair('1', '2').to_i).to eq({1 => 2})
34
+ end
35
+
36
+ it 'should convert to float' do
37
+ expect(pair('1', '2').to_f).to eq({1.0 => 2.0})
30
38
  end
31
39
 
32
40
  end
@@ -0,0 +1,37 @@
1
+ require 'rspec'
2
+ require_relative '../lib/totally_lazy'
3
+
4
+ describe 'Predicates' do
5
+
6
+ it 'should return only even numbers' do
7
+ expect(sequence(1,2,3,4,5,6).filter(even)).to eq(sequence(2,4,6))
8
+ expect { sequence(pair(1,2),pair(3,4)).filter(even).entries }.to raise_error(UnsupportedTypeException)
9
+ end
10
+
11
+ it 'should return only even numbers' do
12
+ expect(sequence(1,2,3,4,5,6).filter(even)).to eq(sequence(2,4,6))
13
+ expect { sequence(pair(1,2),pair(3,4)).filter(even).entries }.to raise_error(UnsupportedTypeException)
14
+ end
15
+
16
+ it 'should return content as string' do
17
+ expect(sequence(1,2).map(as_string)).to eq(sequence("1","2"))
18
+ expect(sequence(pair(1,2),pair(3,4)).map(as_string).entries).to eq([{'1'=>'2'},{'3'=>'4'}])
19
+ end
20
+
21
+ it 'should return content as int' do
22
+ expect(sequence('1','2').map(as_int)).to eq(sequence(1,2))
23
+ expect(sequence(pair('1','2'),pair('3','4')).map(as_int).entries).to eq([{1=>2}, {3=>4}])
24
+ end
25
+
26
+ it 'should return content as float' do
27
+ expect(sequence(1,2).map(as_float)).to eq(sequence(1.0,2.0))
28
+ expect(sequence(pair(1,2),pair(3,4)).map(as_float).entries).to eq([{1.0=>2.0}, {3.0=>4.0}])
29
+ end
30
+
31
+ it 'should return content as array' do
32
+ expect(sequence(1,2).map(as_array)).to eq(sequence([1],[2]))
33
+ expect(sequence(pair(1,2),pair(3,4)).map(as_array).head.class).to eq(Array)
34
+ end
35
+
36
+
37
+ end
@@ -0,0 +1,21 @@
1
+ require 'rspec'
2
+ require_relative '../lib/totally_lazy'
3
+
4
+ describe 'Type Check' do
5
+
6
+ it 'should raise UnsupportedTypeException exception if value is not of specified type' do
7
+ expect(Type.check(1, Fixnum)).to eq(1)
8
+ expect { Type.check('1', Fixnum) }.to raise_error(UnsupportedTypeException)
9
+ end
10
+
11
+ it 'should raise UnsupportedTypeException exception if value does not respond to specified method' do
12
+ expect(Type.responds(1, :even?)).to eq(1)
13
+ expect { Type.responds('1', :even?) }.to raise_error(UnsupportedTypeException)
14
+ end
15
+
16
+ it 'should raise UnsupportedTypeException exception if values do not respond to specified method' do
17
+ expect(Type.responds_all(sequence(1,2,3), :to_s)).to eq([1,2,3])
18
+ expect { Type.responds_all(sequence(1,2,3), :is_cool) }.to raise_error(UnsupportedTypeException)
19
+ end
20
+
21
+ end
data/totally_lazy.gemspec CHANGED
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: totally_lazy 0.0.3 ruby lib
5
+ # stub: totally_lazy 0.0.4 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "totally_lazy"
9
- s.version = "0.0.3"
9
+ s.version = "0.0.4"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
13
13
  s.authors = ["Kingsley Hendrickse"]
14
- s.date = "2014-08-16"
14
+ s.date = "2014-08-17"
15
15
  s.description = "Port of java functional library totally lazy to ruby"
16
16
  s.email = "kingsleyhendrickse@me.com"
17
17
  s.extra_rdoc_files = [
@@ -32,12 +32,12 @@ Gem::Specification.new do |s|
32
32
  "lib/predicates.rb",
33
33
  "lib/sequence.rb",
34
34
  "lib/totally_lazy.rb",
35
- "lib/tuple.rb",
36
35
  "lib/type_check.rb",
37
36
  "spec/option_spec.rb",
38
37
  "spec/pair_spec.rb",
38
+ "spec/predicate_spec.rb",
39
39
  "spec/sequence_spec.rb",
40
- "spec/tuple_spec.rb",
40
+ "spec/type_check_spec.rb",
41
41
  "totally_lazy.gemspec"
42
42
  ]
43
43
  s.homepage = "http://github.com/kingsleyh/totally_lazy"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: totally_lazy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kingsley Hendrickse
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-16 00:00:00.000000000 Z
11
+ date: 2014-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -115,12 +115,12 @@ files:
115
115
  - lib/predicates.rb
116
116
  - lib/sequence.rb
117
117
  - lib/totally_lazy.rb
118
- - lib/tuple.rb
119
118
  - lib/type_check.rb
120
119
  - spec/option_spec.rb
121
120
  - spec/pair_spec.rb
121
+ - spec/predicate_spec.rb
122
122
  - spec/sequence_spec.rb
123
- - spec/tuple_spec.rb
123
+ - spec/type_check_spec.rb
124
124
  - totally_lazy.gemspec
125
125
  homepage: http://github.com/kingsleyh/totally_lazy
126
126
  licenses:
data/lib/tuple.rb DELETED
@@ -1,5 +0,0 @@
1
- class Tuple
2
-
3
-
4
-
5
- end
data/spec/tuple_spec.rb DELETED
File without changes