trither 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/trither.rb +14 -0
- data/lib/trither/either.rb +47 -0
- data/lib/trither/option.rb +56 -1
- data/lib/trither/try.rb +34 -0
- data/lib/trither/version.rb +1 -1
- metadata +23 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 41180ec809ba98b117705827cace6e55a5a3ae39
|
4
|
+
data.tar.gz: 183b0d8e6939c387a882e34b8b6c16cd48ded687
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 85b7714989e60cdc50eea1ad3db219f607f2cd4f71aa5771a632996a7a4b344ced5e864c021798fd86b2af6b7ba648e31ea41a3ec95e7d3f19d745dd9e642a81
|
7
|
+
data.tar.gz: 74854dc880e1087e77fa419ced4b4810b856b21f581547b7f81031dd713fc28d711e6ce7596603d05fc88bdf460617ff792954baeb152a5880d242774a3913ea
|
data/lib/trither.rb
CHANGED
@@ -1,16 +1,30 @@
|
|
1
1
|
require 'trither/version'
|
2
|
+
require 'contracts'
|
2
3
|
|
3
4
|
module Trither
|
4
5
|
class Box
|
6
|
+
include Contracts::Core
|
7
|
+
C = Contracts
|
8
|
+
|
9
|
+
Contract C::Any => C::Any
|
5
10
|
def initialize(value)
|
6
11
|
@value = value
|
7
12
|
end
|
8
13
|
|
14
|
+
Contract C::Any => C::Bool
|
9
15
|
def ==(other)
|
10
16
|
(self.class == other.class) &&
|
11
17
|
(@value == other.instance_variable_get('@value'))
|
12
18
|
end
|
13
19
|
end
|
20
|
+
|
21
|
+
module BasicTypes
|
22
|
+
include Contracts::Core
|
23
|
+
C = Contracts
|
24
|
+
Predicate = C::Func[C::Any => C::Bool]
|
25
|
+
Func0 = C::Func[C::None => C::Any]
|
26
|
+
Func1 = C::Func[C::Any => C::Any]
|
27
|
+
end
|
14
28
|
end
|
15
29
|
|
16
30
|
require 'trither/try'
|
data/lib/trither/either.rb
CHANGED
@@ -1,45 +1,92 @@
|
|
1
|
+
require 'contracts'
|
2
|
+
|
1
3
|
module Either
|
4
|
+
include ::Trither::BasicTypes
|
5
|
+
|
2
6
|
class Left < Trither::Box
|
7
|
+
end
|
8
|
+
|
9
|
+
class Right < Trither::Box
|
10
|
+
end
|
11
|
+
|
12
|
+
EitherType = C::Or[Left, Right]
|
13
|
+
Func1toEither = C::Func[C::Any => EitherType]
|
14
|
+
|
15
|
+
class Left < Trither::Box
|
16
|
+
include ::Trither::BasicTypes
|
17
|
+
|
18
|
+
Contract C::None => true
|
3
19
|
def left?
|
4
20
|
true
|
5
21
|
end
|
6
22
|
|
23
|
+
Contract C::None => false
|
7
24
|
def right?
|
8
25
|
false
|
9
26
|
end
|
10
27
|
|
28
|
+
Contract C::None => C::Any
|
11
29
|
def left
|
12
30
|
@value
|
13
31
|
end
|
14
32
|
|
33
|
+
Contract Func1 => Left
|
15
34
|
def left_map
|
16
35
|
Left.new(yield @value)
|
17
36
|
end
|
18
37
|
|
38
|
+
Contract Func1 => Left
|
19
39
|
def right_map
|
20
40
|
self
|
21
41
|
end
|
42
|
+
|
43
|
+
Contract Func1toEither => EitherType
|
44
|
+
def left_flat_map
|
45
|
+
yield @value
|
46
|
+
end
|
47
|
+
|
48
|
+
Contract Func1toEither => Left
|
49
|
+
def right_flat_map
|
50
|
+
self
|
51
|
+
end
|
22
52
|
end
|
23
53
|
|
24
54
|
class Right < Trither::Box
|
55
|
+
include ::Trither::BasicTypes
|
56
|
+
|
57
|
+
Contract C::None => false
|
25
58
|
def left?
|
26
59
|
false
|
27
60
|
end
|
28
61
|
|
62
|
+
Contract C::None => true
|
29
63
|
def right?
|
30
64
|
true
|
31
65
|
end
|
32
66
|
|
67
|
+
Contract C::None => C::Any
|
33
68
|
def right
|
34
69
|
@value
|
35
70
|
end
|
36
71
|
|
72
|
+
Contract Func1 => Right
|
37
73
|
def left_map
|
38
74
|
self
|
39
75
|
end
|
40
76
|
|
77
|
+
Contract Func1 => Right
|
41
78
|
def right_map
|
42
79
|
Right.new(yield @value)
|
43
80
|
end
|
81
|
+
|
82
|
+
Contract Func1toEither => Right
|
83
|
+
def left_flat_map
|
84
|
+
self
|
85
|
+
end
|
86
|
+
|
87
|
+
Contract Func1toEither => EitherType
|
88
|
+
def right_flat_map
|
89
|
+
yield @value
|
90
|
+
end
|
44
91
|
end
|
45
92
|
end
|
data/lib/trither/option.rb
CHANGED
@@ -1,4 +1,20 @@
|
|
1
|
+
require 'contracts'
|
2
|
+
|
1
3
|
module Option
|
4
|
+
include Contracts::Core
|
5
|
+
C = Contracts
|
6
|
+
|
7
|
+
module None
|
8
|
+
end
|
9
|
+
|
10
|
+
class Some < Trither::Box
|
11
|
+
end
|
12
|
+
|
13
|
+
NoneType = ->(x) { x == None }
|
14
|
+
OptionType = C::Or[NoneType, Some]
|
15
|
+
Func1toOption = C::Func[C::Any => OptionType]
|
16
|
+
|
17
|
+
Contract C::Any => C::Or[->(x) { x == None }, Some]
|
2
18
|
def self.make(value)
|
3
19
|
if value.nil?
|
4
20
|
None
|
@@ -7,53 +23,90 @@ module Option
|
|
7
23
|
end
|
8
24
|
end
|
9
25
|
|
26
|
+
Contract C::ArrayOf[OptionType] => OptionType
|
27
|
+
def self.all(array_of_options)
|
28
|
+
array_of_options.reduce(Some.new([])) do |memo, option|
|
29
|
+
memo.flat_map do |array|
|
30
|
+
option.map { |value| array + [value] }
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
Contract C::ArrayOf[OptionType] => C::ArrayOf[C::Any]
|
36
|
+
def self.flatten(array_of_options)
|
37
|
+
array_of_options.reduce([]) do |array, option|
|
38
|
+
if option.empty?
|
39
|
+
array
|
40
|
+
else
|
41
|
+
array + [option.get_or_else { raise StandardError }]
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
10
46
|
module None
|
47
|
+
include Contracts::Core
|
48
|
+
include ::Trither::BasicTypes
|
49
|
+
|
50
|
+
Contract C::None => true
|
11
51
|
def self.empty?
|
12
52
|
true
|
13
53
|
end
|
14
54
|
|
55
|
+
Contract Predicate => NoneType
|
15
56
|
def self.filter
|
16
57
|
self
|
17
58
|
end
|
18
59
|
|
60
|
+
Contract C::Any => C::Any
|
19
61
|
def self.fetch(default)
|
20
62
|
default
|
21
63
|
end
|
22
64
|
|
65
|
+
Contract Func1 => NoneType
|
23
66
|
def self.map
|
24
67
|
self
|
25
68
|
end
|
26
69
|
|
70
|
+
Contract Func1toOption => OptionType
|
27
71
|
def self.flat_map
|
28
72
|
self
|
29
73
|
end
|
30
74
|
|
75
|
+
Contract Func1toOption => OptionType
|
31
76
|
def self.or_else
|
32
77
|
yield
|
33
78
|
end
|
34
79
|
|
80
|
+
Contract Func0 => C::Any
|
35
81
|
def self.get_or_else
|
36
82
|
yield
|
37
83
|
end
|
38
84
|
end
|
39
85
|
|
40
|
-
class Some
|
86
|
+
class Some
|
87
|
+
include ::Trither::BasicTypes
|
88
|
+
|
89
|
+
Contract C::None => false
|
41
90
|
def empty?
|
42
91
|
false
|
43
92
|
end
|
44
93
|
|
94
|
+
Contract Predicate => OptionType
|
45
95
|
def filter
|
46
96
|
yield(@value) ? self : None
|
47
97
|
end
|
48
98
|
|
99
|
+
Contract C::Any => C::Any
|
49
100
|
def fetch(_default)
|
50
101
|
@value
|
51
102
|
end
|
52
103
|
|
104
|
+
Contract Func1 => OptionType
|
53
105
|
def map
|
54
106
|
Option.make(yield @value)
|
55
107
|
end
|
56
108
|
|
109
|
+
Contract Func1toOption => OptionType
|
57
110
|
def flat_map
|
58
111
|
result = yield @value
|
59
112
|
if result.nil?
|
@@ -63,10 +116,12 @@ module Option
|
|
63
116
|
end
|
64
117
|
end
|
65
118
|
|
119
|
+
Contract Func1toOption => Some
|
66
120
|
def or_else
|
67
121
|
self
|
68
122
|
end
|
69
123
|
|
124
|
+
Contract Func0 => C::Any
|
70
125
|
def get_or_else
|
71
126
|
@value
|
72
127
|
end
|
data/lib/trither/try.rb
CHANGED
@@ -1,4 +1,20 @@
|
|
1
|
+
require 'contracts'
|
2
|
+
|
1
3
|
module Try
|
4
|
+
include Contracts::Core
|
5
|
+
include ::Trither::BasicTypes
|
6
|
+
|
7
|
+
class Failure < Trither::Box
|
8
|
+
end
|
9
|
+
|
10
|
+
class Success < Trither::Box
|
11
|
+
end
|
12
|
+
|
13
|
+
TryType = C::Or[Failure, Success]
|
14
|
+
Func0toTry = C::Func[C::None => TryType]
|
15
|
+
Func1toTry = C::Func[C::Any => TryType]
|
16
|
+
|
17
|
+
Contract Func1 => TryType
|
2
18
|
def self.make
|
3
19
|
Success.new(yield)
|
4
20
|
rescue StandardError => error
|
@@ -6,60 +22,78 @@ module Try
|
|
6
22
|
end
|
7
23
|
|
8
24
|
class Failure < Trither::Box
|
25
|
+
include ::Trither::BasicTypes
|
26
|
+
|
27
|
+
Contract C::None => true
|
9
28
|
def failure?
|
10
29
|
true
|
11
30
|
end
|
12
31
|
|
32
|
+
Contract Predicate => Failure
|
13
33
|
def filter
|
14
34
|
self
|
15
35
|
end
|
16
36
|
|
37
|
+
Contract Func1toTry => Failure
|
17
38
|
def flat_map
|
18
39
|
self
|
19
40
|
end
|
20
41
|
|
42
|
+
Contract Func1 => Failure
|
21
43
|
def map
|
22
44
|
self
|
23
45
|
end
|
24
46
|
|
47
|
+
Contract Func1 => Failure
|
25
48
|
def fail_map
|
26
49
|
Failure.new yield(@value)
|
27
50
|
end
|
28
51
|
|
52
|
+
Contract Func0toTry => TryType
|
29
53
|
def or_else
|
30
54
|
yield @value
|
31
55
|
end
|
32
56
|
|
57
|
+
Contract Func0 => C::Any
|
33
58
|
def get_or_else
|
34
59
|
yield @value
|
35
60
|
end
|
36
61
|
end
|
37
62
|
|
38
63
|
class Success < Trither::Box
|
64
|
+
include ::Trither::BasicTypes
|
65
|
+
|
66
|
+
Contract C::None => false
|
39
67
|
def failure?
|
40
68
|
false
|
41
69
|
end
|
42
70
|
|
71
|
+
Contract Predicate => TryType
|
43
72
|
def filter
|
44
73
|
yield(@value) ? self : Failure.new(Option::None)
|
45
74
|
end
|
46
75
|
|
76
|
+
Contract Func1toTry => TryType
|
47
77
|
def flat_map
|
48
78
|
yield @value
|
49
79
|
end
|
50
80
|
|
81
|
+
Contract Func1 => TryType
|
51
82
|
def map
|
52
83
|
Success.new yield(@value)
|
53
84
|
end
|
54
85
|
|
86
|
+
Contract Func1 => TryType
|
55
87
|
def fail_map
|
56
88
|
self
|
57
89
|
end
|
58
90
|
|
91
|
+
Contract Func0toTry => TryType
|
59
92
|
def or_else
|
60
93
|
self
|
61
94
|
end
|
62
95
|
|
96
|
+
Contract Func0 => C::Any
|
63
97
|
def get_or_else
|
64
98
|
@value
|
65
99
|
end
|
data/lib/trither/version.rb
CHANGED
metadata
CHANGED
@@ -1,57 +1,71 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: trither
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lyall Jonathan Di Trapani
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-01-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: contracts
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.14'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.14'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: bundler
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
16
30
|
requirements:
|
17
31
|
- - "~>"
|
18
32
|
- !ruby/object:Gem::Version
|
19
|
-
version: '1.
|
33
|
+
version: '1.13'
|
20
34
|
type: :development
|
21
35
|
prerelease: false
|
22
36
|
version_requirements: !ruby/object:Gem::Requirement
|
23
37
|
requirements:
|
24
38
|
- - "~>"
|
25
39
|
- !ruby/object:Gem::Version
|
26
|
-
version: '1.
|
40
|
+
version: '1.13'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: rake
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
30
44
|
requirements:
|
31
45
|
- - "~>"
|
32
46
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
47
|
+
version: '12.0'
|
34
48
|
type: :development
|
35
49
|
prerelease: false
|
36
50
|
version_requirements: !ruby/object:Gem::Requirement
|
37
51
|
requirements:
|
38
52
|
- - "~>"
|
39
53
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
54
|
+
version: '12.0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: rspec
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
44
58
|
requirements:
|
45
59
|
- - "~>"
|
46
60
|
- !ruby/object:Gem::Version
|
47
|
-
version: '3.
|
61
|
+
version: '3.5'
|
48
62
|
type: :development
|
49
63
|
prerelease: false
|
50
64
|
version_requirements: !ruby/object:Gem::Requirement
|
51
65
|
requirements:
|
52
66
|
- - "~>"
|
53
67
|
- !ruby/object:Gem::Version
|
54
|
-
version: '3.
|
68
|
+
version: '3.5'
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: rubocop
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -98,7 +112,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
98
112
|
version: '0'
|
99
113
|
requirements: []
|
100
114
|
rubyforge_project:
|
101
|
-
rubygems_version: 2.
|
115
|
+
rubygems_version: 2.6.8
|
102
116
|
signing_key:
|
103
117
|
specification_version: 4
|
104
118
|
summary: Simple implementations of Try, Either, and Option
|