totally_lazy 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.
- checksums.yaml +4 -4
- data/README.md +3 -3
- data/VERSION +1 -1
- data/lib/pair.rb +15 -2
- data/lib/predicates.rb +4 -4
- data/lib/totally_lazy.rb +3 -0
- data/lib/type_check.rb +10 -2
- data/spec/pair_spec.rb +12 -4
- data/spec/predicate_spec.rb +37 -0
- data/spec/type_check_spec.rb +21 -0
- data/totally_lazy.gemspec +5 -5
- metadata +4 -4
- data/lib/tuple.rb +0 -5
- data/spec/tuple_spec.rb +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6e482f2fe314ee171197af7ff51b88e2d8358496
|
4
|
+
data.tar.gz: e937ed5db278d9fd2deb1d58b8e4b7090f7802cb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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.
|
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(
|
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
|
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.
|
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
|
-
|
35
|
+
sequence(a_map).map { |k, v| Pair.new(k, v) }
|
34
36
|
end
|
35
37
|
|
36
38
|
def to_s
|
37
|
-
|
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
|
17
|
+
def as_string
|
18
18
|
-> (v) { v.to_s }
|
19
19
|
end
|
20
20
|
|
21
|
-
def
|
21
|
+
def as_int
|
22
22
|
-> (v) { Type.responds(v, :to_i); v.to_i }
|
23
23
|
end
|
24
24
|
|
25
|
-
def
|
25
|
+
def as_float
|
26
26
|
-> (v) { Type.responds(v, :to_i); v.to_f }
|
27
27
|
end
|
28
28
|
|
29
|
-
def
|
29
|
+
def as_array
|
30
30
|
-> (v) { [v] }
|
31
31
|
end
|
32
32
|
|
data/lib/totally_lazy.rb
CHANGED
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
|
-
|
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.
|
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.
|
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-
|
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/
|
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.
|
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-
|
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/
|
123
|
+
- spec/type_check_spec.rb
|
124
124
|
- totally_lazy.gemspec
|
125
125
|
homepage: http://github.com/kingsleyh/totally_lazy
|
126
126
|
licenses:
|
data/spec/tuple_spec.rb
DELETED
File without changes
|