sums_up 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SumsUp
4
+ # To paraphrase Haskell's Data.Maybe docs[0]:
5
+ #
6
+ # Maybe represents an optional value. A Maybe value contains either a value
7
+ # (Maybe.just(1)), or is empty (Maybe.nothing).
8
+ #
9
+ # [0] https://hackage.haskell.org/package/base-4.14.0.0/docs/Data-Maybe.html
10
+ Maybe = SumsUp.define(:nothing, just: :value) do
11
+ # Build a new Maybe from a value which may or may not be nil.
12
+ def self.of(value)
13
+ if value.nil?
14
+ nothing
15
+ else
16
+ just(value)
17
+ end
18
+ end
19
+
20
+ # Map a function across the Maybe. If present, the value is yielded and that
21
+ # result is wrapped in a new Maybe.just. Returns Maybe.nothing otherwise.
22
+ def map
23
+ match do |m|
24
+ m.just { |value| Maybe.just(yield(value)) }
25
+ m.nothing Maybe.nothing
26
+ end
27
+ end
28
+
29
+ # On nothing, return the provided default value (or yield). On just, return
30
+ # the value.
31
+ def or_else(default = nil)
32
+ match do |m|
33
+ m.just { |value| value }
34
+ m.nothing do
35
+ block_given? ? yield : default
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SumsUp
4
+ # To parahprase Rust's std::result docs[0]:
5
+ #
6
+ # Result is a type used for returning and propagating errors.
7
+ # Result.success(value) represents a success and contains a value;
8
+ # Result.failure(error) represents an error with a propagated error.
9
+ #
10
+ # [0] https://doc.rust-lang.org/std/result/
11
+ Result = SumsUp.define(failure: :error, success: :value) do
12
+ # Yield, wrapping the result in Result.success, or wrap the raised error
13
+ # in Result.failure.
14
+ def self.from_block
15
+ success(yield)
16
+ rescue StandardError => e
17
+ failure(e)
18
+ end
19
+
20
+ # Map a function across the successful value (if present).
21
+ def map
22
+ match do |m|
23
+ m.success { |value| Result.success(yield(value)) }
24
+ m.failure self
25
+ end
26
+ end
27
+
28
+ # Map a function across the error (if present).
29
+ def map_failure
30
+ match do |m|
31
+ m.success self
32
+ m.failure { |error| Result.failure(yield(error)) }
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SumsUp
4
+ VERSION = '1.0.0'
5
+ end
data/sums_up.gemspec ADDED
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/sums_up/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'sums_up'
7
+ spec.version = SumsUp::VERSION
8
+ spec.authors = ['Tom Hulihan']
9
+ spec.email = ['hulihan.tom159@gmail.com']
10
+
11
+ spec.summary = 'Sum types for Ruby'
12
+ spec.homepage = 'https://github.com/nahiluhmot/sums_up'
13
+ spec.license = 'MIT'
14
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.4.0')
15
+
16
+ spec.metadata['homepage_uri'] = spec.homepage
17
+ spec.metadata['source_code_uri'] = spec.homepage
18
+ spec.metadata['changelog_uri'] = "#{spec.homepage}/CHANGELOG.md"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added
22
+ # into git.
23
+ spec.files = Dir.chdir(__dir__) do
24
+ `git ls-files -z`
25
+ .split("\x0")
26
+ .reject { |f| f.match(%r{^(test|spec|features)/}) }
27
+ end
28
+ spec.require_paths = ['lib']
29
+
30
+ spec.add_development_dependency 'pry', '~> 0.13'
31
+ spec.add_development_dependency 'rake', '~> 13.0'
32
+ spec.add_development_dependency 'rspec', '~> 3.10'
33
+ spec.add_development_dependency 'rubocop', '~> 1.1'
34
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sums_up
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Tom Hulihan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-04-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pry
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '13.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '13.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.1'
69
+ description:
70
+ email:
71
+ - hulihan.tom159@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".rubocop.yml"
79
+ - ".ruby-version"
80
+ - ".travis.yml"
81
+ - CODE_OF_CONDUCT.md
82
+ - Gemfile
83
+ - LICENSE.txt
84
+ - README.md
85
+ - Rakefile
86
+ - bin/console
87
+ - bin/setup
88
+ - lib/sums_up.rb
89
+ - lib/sums_up/CHANGELOG.md
90
+ - lib/sums_up/core.rb
91
+ - lib/sums_up/core/functions.rb
92
+ - lib/sums_up/core/matcher.rb
93
+ - lib/sums_up/core/parser.rb
94
+ - lib/sums_up/core/strings.rb
95
+ - lib/sums_up/core/sum_type.rb
96
+ - lib/sums_up/core/variant.rb
97
+ - lib/sums_up/maybe.rb
98
+ - lib/sums_up/result.rb
99
+ - lib/sums_up/version.rb
100
+ - sums_up.gemspec
101
+ homepage: https://github.com/nahiluhmot/sums_up
102
+ licenses:
103
+ - MIT
104
+ metadata:
105
+ homepage_uri: https://github.com/nahiluhmot/sums_up
106
+ source_code_uri: https://github.com/nahiluhmot/sums_up
107
+ changelog_uri: https://github.com/nahiluhmot/sums_up/CHANGELOG.md
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: 2.4.0
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.6.14.4
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Sum types for Ruby
128
+ test_files: []