zx-monads 0.0.5 → 0.0.7

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: 6fad11ef006f65fc1217e5bbf480844d4367b3d7d8f72f486af7c8b3601de4cb
4
+ data.tar.gz: fca3ee13b064db8da1a706ad749e8af4a3f9ceddfd19876be49eaec28dcadc93
5
5
  SHA512:
6
- metadata.gz: 9969b07e28b5a4ca988e416bddcde3360e51c6f572b0712c247aedb7f78717e23e9821c2c83a4fafa4bea660e7c20ada2a43c75d07ec6159c09984f27941d471
7
- data.tar.gz: 3fffca4170917b53e657cb010727cbdc3fd94f4f752893f01f61022714b5e9fda604b9dac1de3ec11dd642d451ccc6a41427e410894e8105dab63cc05e4c6cb9
6
+ metadata.gz: 2ccf1a54176be479cb0e09ad8c2bab177668e8d9219bbc4dbe113ee855231d11cb79c3dbc5ece68b9a2d7ab2208b17cb005eb3eb4beff1ef8ee9b21faae9ff6f
7
+ data.tar.gz: 27430e4a6472dabc923357276cd3c39ca55814ddec4c7f0b6c012f60fedd3bc61080ce9d82428ed095992c96b8087d987a403d30a2fe3abc8cad7c25484c7a29
data/README.md CHANGED
@@ -68,11 +68,11 @@ class Order
68
68
  end
69
69
 
70
70
  class Order
71
- include Zx::Maybe
71
+ include Zx::Maybeable
72
72
  end
73
73
 
74
74
  class ProcessOrder < Zx::Steps
75
- # Maybe included now!
75
+ # include Zx::Maybeable included now!
76
76
  end
77
77
  ```
78
78
 
@@ -102,35 +102,35 @@ end
102
102
  ### ZX::Maybe
103
103
 
104
104
  ```ruby
105
- result = Maybe[1] # or Maybe.of(1)
105
+ result = Zx::Maybe[1] # or Maybe.of(1)
106
106
  ```
107
107
 
108
108
  ```ruby
109
- result = Maybe[nil] # or Maybe.of(nil)
109
+ result = Zx::Maybe[nil] # or Maybe.of(nil)
110
110
  ```
111
111
 
112
112
  ```ruby
113
- result = Maybe[1].map{ _1 + 2}
113
+ result = Zx::Maybe[1].map{ _1 + 2}
114
114
  # -> Some(3)
115
115
  ```
116
116
 
117
117
  ```ruby
118
- result = Maybe[nil].map{ _1 + 2}
118
+ result = Zx::Maybe[nil].map{ _1 + 2}
119
119
  # -> None
120
120
  ```
121
121
 
122
122
  ```ruby
123
- result = Maybe.of(1).or(2)
123
+ result = Zx::Maybe.of(1).or(2)
124
124
  result.or(2) # 1
125
125
  ```
126
126
 
127
127
  ```ruby
128
- result = Maybe.of(' ').or(2)
128
+ result = Zx::Maybe.of(' ').or(2)
129
129
  result.or(2) # 2
130
130
  ```
131
131
 
132
132
  ```ruby
133
- result = Maybe.of(' ').or(2)
133
+ result = Zx::Maybe.of(' ').or(2)
134
134
  result.or(2) # 2
135
135
  ```
136
136
 
@@ -143,7 +143,7 @@ order = {
143
143
  }
144
144
  }
145
145
 
146
- price = Maybe[order]
146
+ price = Zx::Maybe[order]
147
147
  .map { _1[:shopping] }
148
148
  .map { _1[:banana] }
149
149
  .map { _1[:price] }
@@ -152,13 +152,13 @@ price = Maybe[order]
152
152
 
153
153
  # or using #dig
154
154
 
155
- price = Maybe[order].dig(:shopping, :banana, :price)
155
+ price = Zx::Maybe[order].dig(:shopping, :banana, :price)
156
156
  # -> Some(10.0)
157
157
 
158
- price_none = Maybe[order].dig(:shopping, :banana, :price_non_exists)
158
+ price_none = Zx::Maybe[order].dig(:shopping, :banana, :price_non_exists)
159
159
  # -> None
160
160
 
161
- price_or = Maybe[order].dig(:shopping, :banana, :price_non_exists).or(10.0)
161
+ price_or = Zx::Maybe[order].dig(:shopping, :banana, :price_non_exists).or(10.0)
162
162
  # -> Some(10.0)
163
163
  ```
164
164
 
@@ -167,11 +167,11 @@ class Response
167
167
  attr_reader :body
168
168
 
169
169
  def initialize(new_body)
170
- @body = Maybe[new_body]
170
+ @body = Zx::Maybe[new_body]
171
171
  end
172
172
 
173
173
  def change(new_body)
174
- @body = Maybe[new_body]
174
+ @body = Zx::Maybe[new_body]
175
175
  end
176
176
  end
177
177
 
@@ -182,7 +182,7 @@ response.change({ status: 200 })
182
182
  expect(response.body).to be_some
183
183
 
184
184
  response_status = response.body.match(
185
- some: ->(body) { Maybe[body].map { _1.fetch(:status) }.unwrap },
185
+ some: ->(body) { Zx::Maybe[body].map { _1.fetch(:status) }.unwrap },
186
186
  none: -> {}
187
187
  )
188
188
  ```
@@ -196,7 +196,7 @@ response = Response.new(dump) # It's receive an JSON stringified
196
196
 
197
197
  module StatusCodeUnwrapModule
198
198
  def self.call(body)
199
- Maybe[body]
199
+ Zx::Maybe[body]
200
200
  .map{ JSON(_1, symbolize_names: true) }
201
201
  .dig(:status, :code)
202
202
  .apply(&:to_i)
@@ -215,11 +215,11 @@ expect(response_status).to eq(300)
215
215
  You can use `>>` to compose many callables, like this.
216
216
 
217
217
  ```rb
218
- sum = ->(x) { Maybe::Some[x + 1] }
218
+ sum = ->(x) { Zx::Maybe::Some[x + 1] }
219
219
 
220
- subtract = ->(x) { Maybe::Some[x - 1] }
220
+ subtract = ->(x) { Zx::Maybe::Some[x - 1] }
221
221
 
222
- result = Maybe[1] >> \
222
+ result = Zx::Maybe[1] >> \
223
223
  sum >> \
224
224
  subtract
225
225
 
@@ -229,11 +229,11 @@ expect(result.unwrap).to eq(1)
229
229
  If handle None, no worries.
230
230
 
231
231
  ```rb
232
- sum = ->(x) { Maybe::Some[x + 1] }
232
+ sum = ->(x) { Zx::Maybe::Some[x + 1] }
233
233
 
234
- subtract = ->(_) { Maybe::None.new }
234
+ subtract = ->(_) { Zx::Maybe::None.new }
235
235
 
236
- result = Maybe[1] \
236
+ result = Zx::Maybe[1] \
237
237
  >> sum \
238
238
  >> subtract
239
239
 
@@ -243,7 +243,7 @@ expect(result.unwrap).to be_nil
243
243
  ```rb
244
244
  class Order
245
245
  def self.sum(x)
246
- Maybe[{ number: x + 1 }]
246
+ Zx::Maybe[{ number: x + 1 }]
247
247
  end
248
248
  end
249
249
 
@@ -258,7 +258,7 @@ expect(result.unwrap).to be(2)
258
258
 
259
259
  ```rb
260
260
  class Order
261
- include Zx::Maybe
261
+ include Zx::Maybeable
262
262
 
263
263
  def self.sum(x)
264
264
  new.sum(x)
@@ -280,7 +280,7 @@ expect(result.unwrap).to be(2)
280
280
 
281
281
  ```rb
282
282
  class Order
283
- include Zx::Maybe
283
+ include Zx::Maybeable
284
284
 
285
285
  def self.sum(x)
286
286
  new.sum(x)
@@ -305,7 +305,7 @@ expect(number.unwrap).to be(0) # nil.to_i == 0
305
305
 
306
306
  ```rb
307
307
  class Order
308
- include Zx::Maybe
308
+ include Zx::Maybeable
309
309
 
310
310
  def self.sum(x)
311
311
  new.sum(x)
@@ -327,7 +327,7 @@ With default value, in None case.
327
327
 
328
328
  ```rb
329
329
  class Order
330
- include Zx::Maybe
330
+ include Zx::Maybeable
331
331
 
332
332
  def self.sum(x)
333
333
  new.sum(x)
@@ -347,7 +347,7 @@ expect(result.unwrap).to be(2)
347
347
 
348
348
  ```rb
349
349
  class Order
350
- include Zx::Maybe
350
+ include Zx::Maybeable
351
351
 
352
352
  def self.sum(x)
353
353
  new.sum(x)
data/lib/zx/maybe.rb CHANGED
@@ -1,34 +1,202 @@
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 = ->(value = nil) { Zx::Maybe::None.new(value) }
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
+ Maybe[*kwargs]
12
182
  end
13
183
 
14
184
  def Some(*kwargs)
15
- ::Maybe::Some.new(*kwargs)
185
+ Some[*kwargs]
16
186
  end
17
187
 
18
- def None(*kwargs)
19
- ::Maybe::None.new(*kwargs)
188
+ def None(value = nil)
189
+ Zx::Maybe.of(value)
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
199
+
200
+ include Maybe::ClassMethods
201
+ extend Maybe::ClassMethods
34
202
  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.7'
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.7
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