zx-monads 0.0.5 → 0.0.6

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
  SHA256:
3
- metadata.gz: 92ddc57d4126dfd37ebaeeb3695eeb9bd32dfedd4a1a973b1eefe6cc9f082f88
4
- data.tar.gz: 1baafdd45d22c064368e9a8bd789fed4b23cc4cffb493d903441442e26e47502
3
+ metadata.gz: '0391e47fa921d99cc0295cd52c3dd364a6f2bbe69af850b8cce890f7d74510ab'
4
+ data.tar.gz: b662943acb8c80009cc12d2322495169aca0bcda8bd52d7d1aa8ae5808f93998
5
5
  SHA512:
6
- metadata.gz: 9969b07e28b5a4ca988e416bddcde3360e51c6f572b0712c247aedb7f78717e23e9821c2c83a4fafa4bea660e7c20ada2a43c75d07ec6159c09984f27941d471
7
- data.tar.gz: 3fffca4170917b53e657cb010727cbdc3fd94f4f752893f01f61022714b5e9fda604b9dac1de3ec11dd642d451ccc6a41427e410894e8105dab63cc05e4c6cb9
6
+ metadata.gz: 4eac7c508d2eeb24fb0cdcda5ef86fe55536773533acb22ff3e4865e0190379ddc723a00f6ed6ee2bae3183040e258cce8740e1f90931fd2064cf9a30ddb36d4
7
+ data.tar.gz: 47b496f06b771727f6b5d542dda7c303bb178b64253ef6dbbe9d978937671c28a78e433ca312ac488c67cbdf2385781bb5db5fb8016b48ce990d1a81bb9c64b7
data/lib/zx/maybe.rb CHANGED
@@ -1,34 +1,199 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Zx
4
- module Maybe
5
- module Maybeable
6
- None = ->(*kwargs) { ::Maybe::None.new(*kwargs) }
7
- Some = ->(*kwargs) { ::Maybe::Some.new(*kwargs) }
8
- Maybe = ->(*kwargs) { ::Maybe.of(*kwargs) }
4
+ class Maybe
5
+ attr_reader :value
6
+
7
+ IsBlank = ->(value) { value.nil? || value.to_s.strip&.empty? || !value }
8
+
9
+ def self.of(...)
10
+ new.of(...)
11
+ end
12
+
13
+ def self.[](...)
14
+ of(...)
15
+ end
16
+
17
+ def of(value)
18
+ return None.new if IsBlank[value]
19
+
20
+ Some.new(value)
21
+ rescue StandardError
22
+ None.new
23
+ end
24
+
25
+ def type
26
+ to_s.downcase.to_sym
27
+ end
28
+
29
+ def some?
30
+ type == :some
31
+ end
32
+
33
+ def none?
34
+ type == :none
35
+ end
36
+
37
+ def unwrap
38
+ @value
39
+ end
40
+
41
+ def or(value)
42
+ IsBlank[@value] ? value : @value
43
+ end
44
+
45
+ def >>(other)
46
+ self > other
47
+ end
48
+ alias | >>
49
+
50
+ def fmap(_)
51
+ self
52
+ end
53
+
54
+ def >(_other)
55
+ self
56
+ end
57
+
58
+ def map(arg = nil, &block)
59
+ return Maybe[block.call(@value)] if block_given?
60
+ return Maybe[arg.arity > 1 ? arg.curry.call(@value) : arg.call(@value)] if arg.respond_to?(:call)
61
+
62
+ case arg
63
+ in None then self
64
+ in Symbol | String then dig(arg)
65
+ end
66
+ rescue StandardError => e
67
+ None.new(e.message)
68
+ end
69
+ alias apply map
70
+
71
+ def map!(&block)
72
+ @value = block.call(@value)
73
+
74
+ Maybe[@value]
75
+ end
76
+
77
+ def apply!(...)
78
+ apply(...).unwrap
79
+ end
80
+
81
+ def dig(...)
82
+ Maybe[@value&.dig(...)]
83
+ end
84
+
85
+ def dig!(...)
86
+ dig(...).unwrap
87
+ end
88
+
89
+ def match(some:, none:)
90
+ case self
91
+ in Some then some.call(@value)
92
+ else none.call
93
+ end
94
+ end
95
+
96
+ def on_success(&block)
97
+ return self if none?
98
+
99
+ block.call(Some[@value])
100
+
101
+ self
102
+ end
103
+
104
+ def on_failure(&block)
105
+ return self if some?
106
+
107
+ block.call(None[@value])
108
+
109
+ self
110
+ end
111
+
112
+ def on(ontype, &block)
113
+ case ontype.to_sym
114
+ when :success then on_success(&block)
115
+ when :failure then on_failure(&block)
116
+ end
117
+ end
118
+
119
+ class Some < Maybe
120
+ def self.[](...)
121
+ new(...)
122
+ end
123
+
124
+ def initialize(value = nil)
125
+ @value = value
126
+ end
127
+
128
+ def deconstruct
129
+ [@value]
130
+ end
131
+
132
+ def inspect
133
+ format("#<Zx::Maybe::#{self}:0x%x value=%s>", object_id, @value.inspect)
134
+ end
135
+
136
+ def to_s
137
+ 'Some'
138
+ end
139
+
140
+ def >(other)
141
+ other.respond_to?(:call) ? other.call(@value) : other
142
+ end
143
+
144
+ def fmap(&block)
145
+ Maybe[block.call(@value)]
146
+ end
147
+ end
148
+
149
+ class None < Maybe
150
+ def self.[](...)
151
+ new(...)
152
+ end
153
+
154
+ def initialize(value = nil)
155
+ @value = value
156
+ end
157
+
158
+ def deconstruct
159
+ [nil]
160
+ end
161
+
162
+ def inspect
163
+ format("#<Zx::Maybe::#{self}:0x%x value=%s>", object_id, @value.inspect)
164
+ end
165
+
166
+ def to_s
167
+ 'None'
168
+ end
169
+
170
+ def map
171
+ self
172
+ end
173
+ end
174
+
175
+ module ClassMethods
176
+ None = ->(*kwargs) { Zx::Maybe::None.new(*kwargs) }
177
+ Some = ->(*kwargs) { Zx::Maybe::Some.new(*kwargs) }
178
+ Maybe = ->(*kwargs) { Zx::Maybe.of(*kwargs) }
9
179
 
10
180
  def Maybe(*kwargs)
11
- ::Maybe.of(*kwargs)
181
+ Zx::Maybe.of(*kwargs)
12
182
  end
13
183
 
14
184
  def Some(*kwargs)
15
- ::Maybe::Some.new(*kwargs)
185
+ Zx::Maybe::Some.new(*kwargs)
16
186
  end
17
187
 
18
188
  def None(*kwargs)
19
- ::Maybe::None.new(*kwargs)
189
+ Zx::Maybe::None.new(*kwargs)
20
190
  end
21
191
 
22
192
  def Try(default = nil, options = {})
23
- Some yield
193
+ Some[yield]
24
194
  rescue StandardError => e
25
195
  None[default || options.fetch(:or, nil)]
26
196
  end
27
197
  end
28
-
29
- def self.included(klass)
30
- klass.include(Maybeable)
31
- klass.extend(Maybeable)
32
- end
33
198
  end
34
199
  end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Zx::Maybeable
4
+ include Zx::Maybe::ClassMethods
5
+ extend Zx::Maybe::ClassMethods
6
+ end
data/lib/zx/steps.rb CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Zx
4
4
  class Steps
5
- include Maybe
5
+ include Zx::Maybeable
6
6
 
7
7
  class << self
8
8
  def step(step)
@@ -19,4 +19,4 @@ module Zx
19
19
  list.reduce(Some()) { |result, step| result >> send(step) }
20
20
  end
21
21
  end
22
- end
22
+ end
data/lib/zx/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Zx
4
- VERSION = '0.0.5'
4
+ VERSION = '0.0.6'
5
5
  end
data/lib/zx.rb CHANGED
@@ -1,10 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Zx
4
- def self.included(klass)
5
- klass.include(Maybe::Maybeable)
6
- klass.extend(Maybe::Maybeable)
7
- end
8
4
  end
9
5
 
10
6
  require 'zeitwerk'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zx-monads
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thadeu Esteves
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-09-28 00:00:00.000000000 Z
11
+ date: 2023-10-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -85,9 +85,9 @@ files:
85
85
  - Rakefile
86
86
  - bin/console
87
87
  - bin/setup
88
- - lib/maybe.rb
89
88
  - lib/zx.rb
90
89
  - lib/zx/maybe.rb
90
+ - lib/zx/maybeable.rb
91
91
  - lib/zx/steps.rb
92
92
  - lib/zx/version.rb
93
93
  - zx-monads.gemspec
data/lib/maybe.rb DELETED
@@ -1,173 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Maybe
4
- attr_reader :value
5
-
6
- IsBlank = ->(value) { value.nil? || value.to_s.strip&.empty? || !value }
7
-
8
- def self.of(...)
9
- new.of(...)
10
- end
11
-
12
- def self.[](...)
13
- of(...)
14
- end
15
-
16
- def of(value)
17
- return None.new if IsBlank[value]
18
-
19
- Some.new(value)
20
- rescue StandardError
21
- None.new
22
- end
23
-
24
- def type
25
- to_s.downcase.to_sym
26
- end
27
-
28
- def some?
29
- type == :some
30
- end
31
-
32
- def none?
33
- type == :none
34
- end
35
-
36
- def unwrap
37
- @value
38
- end
39
-
40
- def or(value)
41
- IsBlank[@value] ? value : @value
42
- end
43
-
44
- def >>(other)
45
- self > other
46
- end
47
- alias | >>
48
-
49
- def fmap(_)
50
- self
51
- end
52
-
53
- def >(_other)
54
- self
55
- end
56
-
57
- def map(arg = nil, &block)
58
- return Maybe[block.call(@value)] if block_given?
59
- return Maybe[arg.arity > 1 ? arg.curry.call(@value) : arg.call(@value)] if arg.respond_to?(:call)
60
-
61
- case arg
62
- in None then self
63
- in Symbol | String then dig(arg)
64
- end
65
- rescue StandardError => e
66
- None.new(e.message)
67
- end
68
- alias apply map
69
-
70
- def map!(&block)
71
- @value = block.call(@value)
72
-
73
- Maybe[@value]
74
- end
75
-
76
- def apply!(...)
77
- apply(...).unwrap
78
- end
79
-
80
- def dig(...)
81
- Maybe[@value&.dig(...)]
82
- end
83
-
84
- def dig!(...)
85
- dig(...).unwrap
86
- end
87
-
88
- def match(some:, none:)
89
- case self
90
- in Some then some.call(@value)
91
- else none.call
92
- end
93
- end
94
-
95
- def on_success(&block)
96
- return self if none?
97
-
98
- block.call(Some[@value])
99
-
100
- self
101
- end
102
-
103
- def on_failure(&block)
104
- return self if some?
105
-
106
- block.call(None[@value])
107
-
108
- self
109
- end
110
-
111
- def on(ontype, &block)
112
- case ontype.to_sym
113
- when :success then on_success(&block)
114
- when :failure then on_failure(&block)
115
- end
116
- end
117
-
118
- class Some < Maybe
119
- def self.[](...)
120
- new(...)
121
- end
122
-
123
- def initialize(value = nil)
124
- @value = value
125
- end
126
-
127
- def deconstruct
128
- [@value]
129
- end
130
-
131
- def inspect
132
- format("#<Zx::Maybe::#{self}:0x%x value=%s>", object_id, @value.inspect)
133
- end
134
-
135
- def to_s
136
- 'Some'
137
- end
138
-
139
- def >(other)
140
- other.respond_to?(:call) ? other.call(@value) : other
141
- end
142
-
143
- def fmap(&block)
144
- Maybe[block.call(@value)]
145
- end
146
- end
147
-
148
- class None < Maybe
149
- def self.[](...)
150
- new(...)
151
- end
152
-
153
- def initialize(value = nil)
154
- @value = value
155
- end
156
-
157
- def deconstruct
158
- [nil]
159
- end
160
-
161
- def inspect
162
- format("#<Zx::Maybe::#{self}:0x%x value=%s>", object_id, @value.inspect)
163
- end
164
-
165
- def to_s
166
- 'None'
167
- end
168
-
169
- def map
170
- self
171
- end
172
- end
173
- end