zx-monads 0.0.1 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/maybe.rb +0 -2
- data/lib/zx/version.rb +1 -1
- data/zx-monads.gemspec +1 -1
- metadata +2 -4
- data/lib/either.rb +0 -80
- data/lib/zx/either.rb +0 -29
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5813fb9b40773da4e1b045030b8cd3086ac78b19792860d78d7a62b139892d06
|
4
|
+
data.tar.gz: 0a8923287676072ccec22427a2ea564a318c1ad895bdce66d1c2a1557f8804cb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 21598b32f355806f176afd8e6fdab613d3d999410943ef570cbfdda8500bd443f28e00e7b35073bdb75dc46fd44d888efe6b3fe8bac82bbb9bec97d873d13e77
|
7
|
+
data.tar.gz: 64d5dac9dfaabb44fc9227f01fdcec0c7ddfb18a2f8e01c307266eb235438a1ce34af4e078c8f2675fb441b7544ce148a898a89dc281f5206761e0b6c8f6609e
|
data/lib/maybe.rb
CHANGED
data/lib/zx/version.rb
CHANGED
data/zx-monads.gemspec
CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
|
|
11
11
|
spec.authors = ['Thadeu Esteves']
|
12
12
|
spec.email = ['tadeuu@gmail.com']
|
13
13
|
spec.summary = 'FP Monads for Ruby'
|
14
|
-
spec.description = 'Use
|
14
|
+
spec.description = 'Use Maybe to handle errors in your code'
|
15
15
|
spec.homepage = 'https://github.com/thadeu/zx-monads'
|
16
16
|
spec.license = 'MIT'
|
17
17
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: zx-monads
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Thadeu Esteves
|
@@ -66,7 +66,7 @@ dependencies:
|
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0.70'
|
69
|
-
description: Use
|
69
|
+
description: Use Maybe to handle errors in your code
|
70
70
|
email:
|
71
71
|
- tadeuu@gmail.com
|
72
72
|
executables: []
|
@@ -85,10 +85,8 @@ files:
|
|
85
85
|
- Rakefile
|
86
86
|
- bin/console
|
87
87
|
- bin/setup
|
88
|
-
- lib/either.rb
|
89
88
|
- lib/maybe.rb
|
90
89
|
- lib/zx.rb
|
91
|
-
- lib/zx/either.rb
|
92
90
|
- lib/zx/maybe.rb
|
93
91
|
- lib/zx/steps.rb
|
94
92
|
- lib/zx/version.rb
|
data/lib/either.rb
DELETED
@@ -1,80 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class Either
|
4
|
-
class ArgumentError < ArgumentError
|
5
|
-
end
|
6
|
-
|
7
|
-
class ValueError < ArgumentError
|
8
|
-
end
|
9
|
-
|
10
|
-
def self.[](...)
|
11
|
-
new(...)
|
12
|
-
end
|
13
|
-
|
14
|
-
def success?
|
15
|
-
raise NotImplementedError
|
16
|
-
end
|
17
|
-
|
18
|
-
def failure?
|
19
|
-
!success?
|
20
|
-
end
|
21
|
-
|
22
|
-
def value!
|
23
|
-
@value || raise(ValueError, 'value is empty')
|
24
|
-
end
|
25
|
-
|
26
|
-
def on_success(&block)
|
27
|
-
return self if failure?
|
28
|
-
|
29
|
-
block.call(@value)
|
30
|
-
|
31
|
-
self
|
32
|
-
end
|
33
|
-
|
34
|
-
def on_failure(&block)
|
35
|
-
return self if success?
|
36
|
-
|
37
|
-
block.call(@value)
|
38
|
-
|
39
|
-
self
|
40
|
-
end
|
41
|
-
|
42
|
-
def match(right:, left:)
|
43
|
-
case self
|
44
|
-
in Right then right.call(@value)
|
45
|
-
else left.call(@error)
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
class Left < Either
|
50
|
-
attr_reader :error
|
51
|
-
|
52
|
-
def initialize(error)
|
53
|
-
@error = error
|
54
|
-
end
|
55
|
-
|
56
|
-
def deconstruct
|
57
|
-
[@error]
|
58
|
-
end
|
59
|
-
|
60
|
-
def success?
|
61
|
-
false
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
class Right < Either
|
66
|
-
attr_reader :value
|
67
|
-
|
68
|
-
def initialize(value)
|
69
|
-
@value = value
|
70
|
-
end
|
71
|
-
|
72
|
-
def deconstruct
|
73
|
-
[@value]
|
74
|
-
end
|
75
|
-
|
76
|
-
def success?
|
77
|
-
true
|
78
|
-
end
|
79
|
-
end
|
80
|
-
end
|
data/lib/zx/either.rb
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
module Zx
|
3
|
-
module Either
|
4
|
-
module Eitherable
|
5
|
-
Left = ->(*kwargs) { ::Either::Left.new(*kwargs) }
|
6
|
-
Right = ->(*kwargs) { ::Either::Right.new(*kwargs) }
|
7
|
-
|
8
|
-
def Right(*kwargs)
|
9
|
-
::Either::Right.new(*kwargs)
|
10
|
-
end
|
11
|
-
|
12
|
-
def Left(*kwargs)
|
13
|
-
::Either::Left.new(*kwargs)
|
14
|
-
end
|
15
|
-
|
16
|
-
def Try(default = nil, options = {})
|
17
|
-
Right yield
|
18
|
-
rescue StandardError => e
|
19
|
-
Left default || options.fetch(:or, nil)
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
def self.included(klass)
|
24
|
-
klass.include(Eitherable)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
include Either
|
29
|
-
end
|