mona-result 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +4 -0
- data/CHANGELOG.md +3 -1
- data/Gemfile.lock +4 -4
- data/README.md +64 -4
- data/Rakefile +1 -0
- data/lib/mona/result/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 32cb8667c98ff681514f079fed657c8daa835b7c480f2e28c0524892408c81e7
|
4
|
+
data.tar.gz: 741a2b69c33f10b9ec8fad3c25aeb49d6b7dd6f9178824819d933f34b2b5db38
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 96e77b201ec66ee70f9aa84ff8b7aba988233c210e5cebb1157832c53f3c116f7ecf12f069dc29c4ae3514ed9f9007a0458e1824c2d3b45263ec7487f9bd0057
|
7
|
+
data.tar.gz: c359b0d6b6782297c3ebebea9e0d241d7646ccb3c44873c8d087567681ed3fb6ff58d827f6d0af9a97516d57e74410d754b47222c6b895440f9b15133b139faa
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
mona-result (0.1.
|
4
|
+
mona-result (0.1.1)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -33,14 +33,14 @@ GEM
|
|
33
33
|
rbs (2.6.0)
|
34
34
|
regexp_parser (2.5.0)
|
35
35
|
rexml (3.2.5)
|
36
|
-
rubocop (1.
|
36
|
+
rubocop (1.34.0)
|
37
37
|
json (~> 2.3)
|
38
38
|
parallel (~> 1.10)
|
39
|
-
parser (>= 3.1.
|
39
|
+
parser (>= 3.1.2.1)
|
40
40
|
rainbow (>= 2.2.2, < 4.0)
|
41
41
|
regexp_parser (>= 1.8, < 3.0)
|
42
42
|
rexml (>= 3.2.5, < 4.0)
|
43
|
-
rubocop-ast (>= 1.
|
43
|
+
rubocop-ast (>= 1.20.0, < 2.0)
|
44
44
|
ruby-progressbar (~> 1.7)
|
45
45
|
unicode-display_width (>= 1.4.0, < 3.0)
|
46
46
|
rubocop-ast (1.21.0)
|
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# Mona::Result
|
2
2
|
|
3
|
-
|
3
|
+
Provides a result monad, with OK holding a value, and Err holding a failure, with optional reason, and metadata.
|
4
4
|
|
5
|
-
|
5
|
+
Also provides a dict result monad, a hash-like result, which provides `#sequence` whose semantics mirror _do notation_
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
@@ -16,7 +16,67 @@ If bundler is not being used to manage dependencies, install the gem by executin
|
|
16
16
|
|
17
17
|
## Usage
|
18
18
|
|
19
|
-
|
19
|
+
```ruby
|
20
|
+
Result = Mona::Result
|
21
|
+
|
22
|
+
# basic OK
|
23
|
+
success = Result.ok(1) # => #<OK 1>
|
24
|
+
success.ok? # => true
|
25
|
+
success.err? # => false
|
26
|
+
success.value # => 1
|
27
|
+
success.and_tap { puts "it is #{_1}" }.and_then { _1 * 5 }.value_or { :nope }
|
28
|
+
# OUT: it is 1
|
29
|
+
# => 5
|
30
|
+
|
31
|
+
# basic Err
|
32
|
+
failure = Result.err(4, :not_prime) # => #<Err 4 reason: :not_prime>
|
33
|
+
failure.ok? # => false
|
34
|
+
failure.err? # => true
|
35
|
+
failure.failure # => 4
|
36
|
+
failure.reason # => :not_prime
|
37
|
+
failure.and_tap { puts "it is #{_1}" }.and_then { _1 * 5 }.value_or { :nope } # => :nope
|
38
|
+
|
39
|
+
# Dict with sequence
|
40
|
+
result = Result.dict do |d|
|
41
|
+
d.set :numerator, 10
|
42
|
+
d.set :divisor, 2
|
43
|
+
d.set :answer, d[:numerator] / d[:divisor]
|
44
|
+
end # => #<OK {:numerator=>10, :divisor=>2, :answer=>5}>
|
45
|
+
|
46
|
+
# Action - a callable sequence
|
47
|
+
divide = Result.action do |x, y|
|
48
|
+
set :numerator, x
|
49
|
+
set :divisor, y.zero? ? Result.err(y, :is_zero) : y
|
50
|
+
puts "ready to calculate answer"
|
51
|
+
set :answer, numerator / divisor
|
52
|
+
puts "answer was: #{answer}"
|
53
|
+
end
|
54
|
+
|
55
|
+
divide.call(12, 4)
|
56
|
+
# OUT: ready to calculate answer
|
57
|
+
# OUT: answer was: 3
|
58
|
+
# => #<OK {:numerator=>12, :divisor=>4, :answer=>3}>
|
59
|
+
|
60
|
+
result = divide.call(12, 0) # => #<Err {:numerator=>12, :divisor=>0} reason: :is_zero, key: :divisor>
|
61
|
+
result.err? # => true
|
62
|
+
result.failure # => {:numerator=>12, :divisor=>0}
|
63
|
+
result.reason # => :is_zero
|
64
|
+
result.meta # => {:key=>:divisor}
|
65
|
+
```
|
66
|
+
|
67
|
+
Pattern matching is supported in the following way:
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
case result
|
71
|
+
in :ok, value
|
72
|
+
in :err, failure, reason, { **meta }
|
73
|
+
end
|
74
|
+
|
75
|
+
case result
|
76
|
+
in ok:
|
77
|
+
in err:, reason:, **meta
|
78
|
+
end
|
79
|
+
```
|
20
80
|
|
21
81
|
## Development
|
22
82
|
|
@@ -26,7 +86,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
26
86
|
|
27
87
|
## Contributing
|
28
88
|
|
29
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
89
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/mona-rb/mona-result.
|
30
90
|
|
31
91
|
## License
|
32
92
|
|
data/Rakefile
CHANGED
data/lib/mona/result/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mona-result
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ian White
|
@@ -10,7 +10,7 @@ bindir: exe
|
|
10
10
|
cert_chain: []
|
11
11
|
date: 2022-08-09 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description: Mona::Result provides a result monad, and
|
13
|
+
description: Mona::Result provides a result monad, and dict result monad with do-notation
|
14
14
|
email:
|
15
15
|
- ian.w.white@gmail.com
|
16
16
|
executables: []
|
@@ -61,5 +61,5 @@ requirements: []
|
|
61
61
|
rubygems_version: 3.3.7
|
62
62
|
signing_key:
|
63
63
|
specification_version: 4
|
64
|
-
summary: Mona::Result is a result
|
64
|
+
summary: Mona::Result is a result monad for ruby
|
65
65
|
test_files: []
|