zx-monads 0.0.4 → 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: 50a07904dbbe8ee15651293ad4e71e863fe3b32de630cdf0743ef34575351ca1
4
- data.tar.gz: 11e32540d53a86ce32300fd38d95c10244c35f7601b44e8cce2728a1d83c2185
3
+ metadata.gz: '0391e47fa921d99cc0295cd52c3dd364a6f2bbe69af850b8cce890f7d74510ab'
4
+ data.tar.gz: b662943acb8c80009cc12d2322495169aca0bcda8bd52d7d1aa8ae5808f93998
5
5
  SHA512:
6
- metadata.gz: 81841c32f54a4bf2c22dd33ad1f72a1561c7d29c5b53977bb9641225fbe27116886d3391765643d35b43b3c77755cf58f74360213287e3ddbe443fe9706ecd65
7
- data.tar.gz: fe4d63145ecb587180a29e9880036d673d712019854098ff7842afe56a1a27c625f654f9f8dc356f6afce2983ea69e9ad87edc640dd3176e8ccef8304e861256
6
+ metadata.gz: 4eac7c508d2eeb24fb0cdcda5ef86fe55536773533acb22ff3e4865e0190379ddc723a00f6ed6ee2bae3183040e258cce8740e1f90931fd2064cf9a30ddb36d4
7
+ data.tar.gz: 47b496f06b771727f6b5d542dda7c303bb178b64253ef6dbbe9d978937671c28a78e433ca312ac488c67cbdf2385781bb5db5fb8016b48ce990d1a81bb9c64b7
@@ -10,7 +10,7 @@ jobs:
10
10
  runs-on: ubuntu-latest
11
11
  strategy:
12
12
  matrix:
13
- ruby-version: ['2.7', '3.0', '3.1', '3.2']
13
+ ruby-version: ['2.7', '3.0', '3.1', 'head']
14
14
 
15
15
  steps:
16
16
  - uses: actions/checkout@v3
data/.rubocop.yml CHANGED
@@ -7,7 +7,7 @@ Style/Documentation:
7
7
  Enabled: false
8
8
 
9
9
  Layout:
10
- Enabled: false
10
+ Enabled: true
11
11
 
12
12
  Style/FrozenStringLiteralComment:
13
13
  Enabled: true
data/Gemfile CHANGED
@@ -1,14 +1,12 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'base64'
4
- gem 'json'
5
3
  gem 'zeitwerk'
6
4
 
7
5
  group :development, :test do
6
+ gem 'byebug'
8
7
  gem 'rubocop', github: 'rubocop/rubocop', require: false
9
8
  end
10
9
 
11
10
  group :test do
12
- gem 'byebug'
13
11
  gem 'rspec'
14
12
  end
data/lib/zx/maybe.rb CHANGED
@@ -1,33 +1,199 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Zx
3
- module Maybe
4
- module Maybeable
5
- None = ->(*kwargs) { ::Maybe::None.new(*kwargs) }
6
- Some = ->(*kwargs) { ::Maybe::Some.new(*kwargs) }
7
- Maybe = ->(*kwargs) { ::Maybe.of(*kwargs) }
8
-
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) }
179
+
9
180
  def Maybe(*kwargs)
10
- ::Maybe.of(*kwargs)
181
+ Zx::Maybe.of(*kwargs)
11
182
  end
12
-
183
+
13
184
  def Some(*kwargs)
14
- ::Maybe::Some.new(*kwargs)
185
+ Zx::Maybe::Some.new(*kwargs)
15
186
  end
16
-
187
+
17
188
  def None(*kwargs)
18
- ::Maybe::None.new(*kwargs)
189
+ Zx::Maybe::None.new(*kwargs)
19
190
  end
20
191
 
21
192
  def Try(default = nil, options = {})
22
- Some yield
193
+ Some[yield]
23
194
  rescue StandardError => e
24
195
  None[default || options.fetch(:or, nil)]
25
196
  end
26
197
  end
27
-
28
- def self.included(klass)
29
- klass.include(Maybeable)
30
- klass.extend(Maybeable)
31
- end
32
198
  end
33
- end
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.4'
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.4
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-25 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,170 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Maybe
4
- IsBlank = ->(value) { value.nil? || value.to_s.strip&.empty? || !value }
5
-
6
- def self.of(...)
7
- new.of(...)
8
- end
9
-
10
- def self.[](...)
11
- of(...)
12
- end
13
-
14
- def of(value)
15
- return None.new if IsBlank[value]
16
-
17
- Some.new(value)
18
- rescue StandardError
19
- None.new
20
- end
21
-
22
- def type
23
- to_s.downcase.to_sym
24
- end
25
-
26
- def some?
27
- type == :some
28
- end
29
-
30
- def none?
31
- type == :none
32
- end
33
-
34
- def unwrap
35
- @value
36
- end
37
-
38
- def or(value)
39
- IsBlank[@value] ? value : @value
40
- end
41
-
42
- def >>(other)
43
- self > other
44
- end
45
-
46
- def fmap(_)
47
- self
48
- end
49
-
50
- def >(_other)
51
- self
52
- end
53
-
54
- def map(arg = nil, &block)
55
- return Maybe[block.call(@value)] if block_given?
56
- return Maybe[arg.arity > 1 ? arg.curry.call(@value) : arg.call(@value)] if arg.respond_to?(:call)
57
-
58
- case arg
59
- in None then self
60
- in Symbol | String then dig(arg)
61
- end
62
- rescue StandardError => e
63
- None.new(e.message)
64
- end
65
- alias apply map
66
-
67
- def map!(&block)
68
- @value = block.call(@value)
69
-
70
- Maybe[@value]
71
- end
72
-
73
- def apply!(...)
74
- apply(...).unwrap
75
- end
76
-
77
- def dig(...)
78
- Maybe[@value&.dig(...)]
79
- end
80
-
81
- def dig!(...)
82
- dig(...).unwrap
83
- end
84
-
85
- def match(some:, none:)
86
- case self
87
- in Some then some.call(@value)
88
- else none.call
89
- end
90
- end
91
-
92
- def on_success(&block)
93
- return self if none?
94
-
95
- block.call(Some[@value])
96
-
97
- self
98
- end
99
-
100
- def on_failure(&block)
101
- return self if some?
102
-
103
- block.call(None[@value])
104
-
105
- self
106
- end
107
-
108
- def on(ontype, &block)
109
- case ontype.to_sym
110
- when :success then on_success(&block)
111
- when :failure then on_failure(&block)
112
- end
113
- end
114
-
115
- class Some < Maybe
116
- def self.[](...)
117
- new(...)
118
- end
119
-
120
- def initialize(value = nil)
121
- @value = value
122
- end
123
-
124
- def deconstruct
125
- [@value]
126
- end
127
-
128
- def inspect
129
- format("#<Zx::Maybe::#{self}:0x%x value=%s>", object_id, @value.inspect)
130
- end
131
-
132
- def to_s
133
- 'Some'
134
- end
135
-
136
- def >(other)
137
- other.respond_to?(:call) ? other.call(@value) : other
138
- end
139
-
140
- def fmap(&block)
141
- Maybe[block.call(@value)]
142
- end
143
- end
144
-
145
- class None < Maybe
146
- def self.[](...)
147
- new(...)
148
- end
149
-
150
- def initialize(value = nil)
151
- @value = value
152
- end
153
-
154
- def deconstruct
155
- [nil]
156
- end
157
-
158
- def inspect
159
- format("#<Zx::Maybe::#{self}:0x%x value=%s>", object_id, @value.inspect)
160
- end
161
-
162
- def to_s
163
- 'None'
164
- end
165
-
166
- def map
167
- self
168
- end
169
- end
170
- end