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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 170acae8e3dab4e7c9dec3b88b17cb8668a678290aafa5eb363da54261a5eab9
4
- data.tar.gz: 0b327151acf8487019f87f5b859b1f940d766e67c84479c29ef1a70a4d26aeb1
3
+ metadata.gz: 32cb8667c98ff681514f079fed657c8daa835b7c480f2e28c0524892408c81e7
4
+ data.tar.gz: 741a2b69c33f10b9ec8fad3c25aeb49d6b7dd6f9178824819d933f34b2b5db38
5
5
  SHA512:
6
- metadata.gz: 45ca05476a2621a4d6e76a190b37852880b9c69d43470ec77ed5dc9dabf7ec9f6cbe9771777adc369f69ce421e8297f2e5c5ee66df8bf43b12c62d1e0ce3c53d
7
- data.tar.gz: 59684cbea28ce2392a1755edb70c9756786b1bf730900e5aaf0829a3a4a068a0742c6db907e3720d4533ff17ff57a2c7b5a72e7dd5d1364c4acfd4f6a4bafd80
6
+ metadata.gz: 96e77b201ec66ee70f9aa84ff8b7aba988233c210e5cebb1157832c53f3c116f7ecf12f069dc29c4ae3514ed9f9007a0458e1824c2d3b45263ec7487f9bd0057
7
+ data.tar.gz: c359b0d6b6782297c3ebebea9e0d241d7646ccb3c44873c8d087567681ed3fb6ff58d827f6d0af9a97516d57e74410d754b47222c6b895440f9b15133b139faa
data/.rubocop.yml CHANGED
@@ -1,3 +1,7 @@
1
+ require:
2
+ - rubocop-minitest
3
+ - rubocop-rake
4
+
1
5
  AllCops:
2
6
  TargetRubyVersion: 3.1
3
7
  NewCops: enable
data/CHANGELOG.md CHANGED
@@ -1,4 +1,6 @@
1
- ## [Unreleased]
1
+ ## [0.1.1]
2
+
3
+ - Adds basic documentation
2
4
 
3
5
  ## [0.1.0] - 2022-08-04
4
6
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mona-result (0.1.0)
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.33.0)
36
+ rubocop (1.34.0)
37
37
  json (~> 2.3)
38
38
  parallel (~> 1.10)
39
- parser (>= 3.1.0.0)
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.19.1, < 2.0)
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
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/mona/result`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ Provides a result monad, with OK holding a value, and Err holding a failure, with optional reason, and metadata.
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
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
- TODO: Write usage instructions here
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/[USERNAME]/mona-result.
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
@@ -3,6 +3,7 @@
3
3
  require "bundler/gem_tasks"
4
4
  require "rake/testtask"
5
5
 
6
+ desc "Run steep check"
6
7
  task :steep do
7
8
  system "steep check" or raise "Steep type checking failed"
8
9
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Mona
4
4
  module Result
5
- VERSION = "0.1.0"
5
+ VERSION = "0.1.1"
6
6
  end
7
7
  end
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.0
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 dict_result monad
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 Monad for ruby
64
+ summary: Mona::Result is a result monad for ruby
65
65
  test_files: []