totally_lazy 0.1.26 → 0.1.28
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 +8 -8
- data/lib/totally_lazy/either.rb +32 -7
- data/lib/totally_lazy/functions.rb +2 -2
- data/lib/totally_lazy/numbers.rb +2 -2
- data/spec/totally_lazy/either_spec.rb +21 -1
- data/spec/totally_lazy/option_spec.rb +4 -4
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
MWZkMGRlODJjYzAwMDBmMmFiMTE3NDk0N2QzNTA4NTJkMTgxZTNkMw==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
OWM4YjJjMmIxYmUxZjE1N2RiYTI1Mjg0ZThhZmU4MzhlNGM2MWYxOQ==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
Y2FiN2M4YzIzN2M5OGIzNWFjNmFiM2UyYmYxYTE0MmI4OTBiNDZiZjE3MTdj
|
10
|
+
ZTY0YTkwYTYzM2FiY2FlNWFkMzlhNzhhMGRjZmQzYmE1OWE4NjE0M2UzN2I2
|
11
|
+
ZDBiNTE4NzFlMTY5OWExMjE5YTNlZGVjNzMxZjNkYzZiOWExYjY=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
OTY2MDA1ZjI5MjAzOGIwOGI2NDJkZTQxNmZiZDk2MzJkODNlZTRjOWI1NWY5
|
14
|
+
MjU0YTAzZTYyMDdmYTdlYzY2ZDgzMDdiMjcwNTU1NzFjN2FiNzQ5NDUwMjk4
|
15
|
+
Yzk1MTJlNzk4MTgyYzg3OWFmODcyMTU5YTkwZTNjYmE0MTk2Yjk=
|
data/lib/totally_lazy/either.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require_relative 'lambda_block'
|
2
|
+
|
1
3
|
module Eithers
|
2
4
|
private
|
3
5
|
def left(value)
|
@@ -11,6 +13,7 @@ end
|
|
11
13
|
|
12
14
|
class Either
|
13
15
|
include Comparable
|
16
|
+
include LambdaBlock
|
14
17
|
|
15
18
|
def self.left(value)
|
16
19
|
Left.new(value)
|
@@ -19,7 +22,6 @@ class Either
|
|
19
22
|
def self.right(value)
|
20
23
|
Right.new(value)
|
21
24
|
end
|
22
|
-
|
23
25
|
end
|
24
26
|
|
25
27
|
class Left < Either
|
@@ -35,16 +37,25 @@ class Left < Either
|
|
35
37
|
false
|
36
38
|
end
|
37
39
|
|
38
|
-
def
|
40
|
+
def left_value
|
39
41
|
@value
|
40
42
|
end
|
41
43
|
|
42
|
-
def
|
44
|
+
def right_value
|
43
45
|
raise NoSuchElementException.new
|
44
46
|
end
|
45
47
|
|
48
|
+
def map_lr(fn_left, fn_right)
|
49
|
+
fn_left.(@value)
|
50
|
+
end
|
51
|
+
|
52
|
+
def map_left(fn=nil, &block)
|
53
|
+
assert_funcs(fn, block_given?)
|
54
|
+
left(block_given? ? block.call(@value) : fn.(@value))
|
55
|
+
end
|
56
|
+
|
46
57
|
def <=>(other)
|
47
|
-
@value <=> other.
|
58
|
+
@value <=> other.left_value
|
48
59
|
end
|
49
60
|
end
|
50
61
|
|
@@ -61,15 +72,29 @@ class Right < Either
|
|
61
72
|
true
|
62
73
|
end
|
63
74
|
|
64
|
-
def
|
75
|
+
def left_value
|
65
76
|
raise NoSuchElementException.new
|
66
77
|
end
|
67
78
|
|
68
|
-
def
|
79
|
+
def right_value
|
69
80
|
@value
|
70
81
|
end
|
71
82
|
|
83
|
+
def map(fn=nil, &block)
|
84
|
+
assert_funcs(fn, block_given?)
|
85
|
+
right(block_given? ? block.call(@value) : fn.(@value))
|
86
|
+
end
|
87
|
+
|
88
|
+
def map_lr(fn_left, fn_right)
|
89
|
+
fn_right.(@value)
|
90
|
+
end
|
91
|
+
|
92
|
+
def map_left(fn=nil, &block)
|
93
|
+
assert_funcs(fn, block_given?)
|
94
|
+
self
|
95
|
+
end
|
96
|
+
|
72
97
|
def <=>(other)
|
73
|
-
@value <=> other.
|
98
|
+
@value <=> other.right_value
|
74
99
|
end
|
75
100
|
end
|
data/lib/totally_lazy/numbers.rb
CHANGED
@@ -1,9 +1,29 @@
|
|
1
1
|
require_relative '../spec_helper'
|
2
2
|
|
3
3
|
describe 'Either' do
|
4
|
-
it '
|
4
|
+
it 'can be used in filter and map' do
|
5
5
|
eithers = sequence(left('error'), right(3))
|
6
6
|
expect(eithers.filter(is_left?).map(get_left)).to eq(sequence('error'))
|
7
7
|
expect(eithers.filter(is_right?).map(get_right)).to eq(sequence(3))
|
8
8
|
end
|
9
|
+
|
10
|
+
it 'should support map' do
|
11
|
+
expect(right(3).map(add(2))).to eq(right(5))
|
12
|
+
expect(right(3).map { |a| a+2 }).to eq(right(5))
|
13
|
+
expect(left(3).map_lr(add(2), nil)).to eq(5)
|
14
|
+
expect(right(3).map_lr(nil, add(2))).to eq(5)
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should support map_left' do
|
18
|
+
expect(right(3).map_left(add(2))).to eq(right(3))
|
19
|
+
expect(right(3).map_left { |a| a+2 }).to eq(right(3))
|
20
|
+
expect(left(3).map_left(add(2))).to eq(left(5))
|
21
|
+
expect(left(3).map_left { |a| a+2 }).to eq(left(5))
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'should raise exception if you try to use both lambda and block' do
|
25
|
+
expect { right(1).map(add(2)) { |a| a+2 } }.to raise_error(RuntimeError)
|
26
|
+
expect { right(1).map_left(add(2)) { |a| a+2 } }.to raise_error(RuntimeError)
|
27
|
+
expect { left(1).map_left(add(2)) { |a| a+2 } }.to raise_error(RuntimeError)
|
28
|
+
end
|
9
29
|
end
|
@@ -29,11 +29,11 @@ describe 'Option' do
|
|
29
29
|
end
|
30
30
|
|
31
31
|
it 'should support fold (aka fold_left)' do
|
32
|
-
expect(option(1).fold(1,
|
33
|
-
expect(option(1).fold_left(1,
|
32
|
+
expect(option(1).fold(1, sum)).to eq(2)
|
33
|
+
expect(option(1).fold_left(1, sum)).to eq(2)
|
34
34
|
expect(option(1).fold(1) { |a, b| a + b }).to eq(2)
|
35
|
-
expect(some(1).fold(1,
|
36
|
-
expect(none.fold(1,
|
35
|
+
expect(some(1).fold(1, sum)).to eq(2)
|
36
|
+
expect(none.fold(1, sum)).to eq(1)
|
37
37
|
end
|
38
38
|
|
39
39
|
it 'should support map' do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: totally_lazy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.28
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Raymond Barlow
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2016-
|
12
|
+
date: 2016-03-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|