toy_adt 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1f45563ee2b678fd2d1ad8dd632d05fee56b16dbc46b5b4952082be20f48dea4
4
+ data.tar.gz: 56ea2b517926e966389b211ccc30d2d9c2113d916996b514146519c2646eef52
5
+ SHA512:
6
+ metadata.gz: 90beb64a03734f4c5405a15165b939298b33cac5bf0acb1b3422083df919aa9d4ff10325c36d983f4254ce770dfbeae8792a7ea247ad6d92e8a779ed449f3f64
7
+ data.tar.gz: c0a93e38be8bdfa02b24599ed4a49cd6aafda170e1488a82426497d2a7d07e0ee1540a1167e7a34bd2bdd3c15a986d47d4ce38d034c143dc129e778a6cf2ab1d
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-05-25
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at keystonelemur@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in toy_adt.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,75 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ toy_adt (0.1.0)
5
+ sorbet
6
+ sorbet-runtime
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ coderay (1.1.3)
12
+ diff-lcs (1.5.0)
13
+ ffi (1.15.5)
14
+ formatador (1.1.0)
15
+ guard (2.18.0)
16
+ formatador (>= 0.2.4)
17
+ listen (>= 2.7, < 4.0)
18
+ lumberjack (>= 1.0.12, < 2.0)
19
+ nenv (~> 0.1)
20
+ notiffany (~> 0.0)
21
+ pry (>= 0.13.0)
22
+ shellany (~> 0.0)
23
+ thor (>= 0.18.1)
24
+ guard-compat (1.2.1)
25
+ guard-rspec (4.7.3)
26
+ guard (~> 2.1)
27
+ guard-compat (~> 1.1)
28
+ rspec (>= 2.99.0, < 4.0)
29
+ listen (3.7.1)
30
+ rb-fsevent (~> 0.10, >= 0.10.3)
31
+ rb-inotify (~> 0.9, >= 0.9.10)
32
+ lumberjack (1.2.8)
33
+ method_source (1.0.0)
34
+ nenv (0.3.0)
35
+ notiffany (0.1.3)
36
+ nenv (~> 0.1)
37
+ shellany (~> 0.0)
38
+ pry (0.14.1)
39
+ coderay (~> 1.1)
40
+ method_source (~> 1.0)
41
+ rake (13.0.6)
42
+ rb-fsevent (0.11.1)
43
+ rb-inotify (0.10.1)
44
+ ffi (~> 1.0)
45
+ rspec (3.11.0)
46
+ rspec-core (~> 3.11.0)
47
+ rspec-expectations (~> 3.11.0)
48
+ rspec-mocks (~> 3.11.0)
49
+ rspec-core (3.11.0)
50
+ rspec-support (~> 3.11.0)
51
+ rspec-expectations (3.11.0)
52
+ diff-lcs (>= 1.2.0, < 2.0)
53
+ rspec-support (~> 3.11.0)
54
+ rspec-mocks (3.11.1)
55
+ diff-lcs (>= 1.2.0, < 2.0)
56
+ rspec-support (~> 3.11.0)
57
+ rspec-support (3.11.0)
58
+ shellany (0.0.1)
59
+ sorbet (0.5.10028)
60
+ sorbet-static (= 0.5.10028)
61
+ sorbet-runtime (0.5.10028)
62
+ sorbet-static (0.5.10028-universal-darwin-20)
63
+ thor (1.2.1)
64
+
65
+ PLATFORMS
66
+ x86_64-darwin-20
67
+
68
+ DEPENDENCIES
69
+ guard-rspec
70
+ rake (~> 13.0)
71
+ rspec (~> 3.0)
72
+ toy_adt!
73
+
74
+ BUNDLED WITH
75
+ 2.3.5
data/Guardfile ADDED
@@ -0,0 +1,14 @@
1
+ guard :rspec, cmd: "bundle exec rspec" do
2
+ require "guard/rspec/dsl"
3
+ dsl = Guard::RSpec::Dsl.new(self)
4
+
5
+ # RSpec files
6
+ rspec = dsl.rspec
7
+ watch(rspec.spec_helper) { rspec.spec_dir }
8
+ watch(rspec.spec_support) { rspec.spec_dir }
9
+ watch(rspec.spec_files)
10
+
11
+ # Ruby files
12
+ ruby = dsl.ruby
13
+ dsl.watch_spec_files_for(ruby.lib_files)
14
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Brandon Weaver
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,106 @@
1
+ # ToyAdt
2
+
3
+ A toy implementation of Algebraic Data Types for use in "Ruby in FantasyLand". As much as possible I'll try and hold to the public API, and may switch into DryRB later depending on a few factors, but this works for an initial release.
4
+
5
+ Now, as to what this gem does...
6
+
7
+ ## Type: Sorbet-aware structs with pattern matching
8
+
9
+ Types are based on Sorbet's `T::Struct` with constant properties and Ruby 2.7+ pattern matching awareness built in. It should be noted that all `sum` types use this structure behind the scenes as well.
10
+
11
+ Let's say we had a coordinate:
12
+
13
+ ```ruby
14
+ Coord = ToyAdt.type(x: Integer, y: Integer, z: Integer)
15
+
16
+ center = Coord.new(x: 0, y: 0, z: 0) # valid
17
+ invalid = Coord.new(x: 0, y: 0, z: "INVALID") # invalid: TypeError
18
+ ```
19
+
20
+ We can mix Ruby types and Sorbet-aware types (`T::Array[]` and such), and may expand this later to have awareness of DryRB-style types.
21
+
22
+ We can also add methods to these types:
23
+
24
+ ```ruby
25
+ Coord = ToyAdt.type(x: Integer, y: Integer, z: Integer) do
26
+ def translate(x: 0, y: 0, z: 0)
27
+ self.class.new(x: @x + x, y: @y + y, z: @z + z)
28
+ end
29
+ end
30
+ ```
31
+
32
+ Remember: These types are immutable constants, hence returning a new object.
33
+
34
+ ## Sum: Sum-types with exhaustive matching required
35
+
36
+ Sum represents sum types, which are composite structures representing similar data, like say a shape:
37
+
38
+ ```ruby
39
+ Shape = ToyAdt.sum(
40
+ circle: { radius: Integer, center: Coord },
41
+ square: { top_left: Coord, bottom_right: Coord }
42
+ )
43
+ ```
44
+
45
+ ...and like the type above, we can also add methods here:
46
+
47
+ ```ruby
48
+ Shape = ToyAdt.sum(
49
+ circle: { radius: Integer, center: Coord },
50
+ square: { top_left: Coord, bottom_right: Coord }
51
+ ) do
52
+ def translate(x: 0, y: 0, z: 0)
53
+ case self
54
+ when Shape::Circle
55
+ Shape::Circle.new(radius:, center: center.translate(x:, y:, z:))
56
+ when Shape::Square
57
+ Shape::Square.new(
58
+ top_left: top_left.translate(x:, y:, z:),
59
+ bottom_right: bottom_right.translate(x:, y:, z:)
60
+ )
61
+ else
62
+ T.absurd(self)
63
+ end
64
+ end
65
+ end
66
+ ```
67
+
68
+ For the moment I'm using `T.absurd` for pattern matching, but this is aware of Ruby 2.7+ pattern matching syntax as well. There's a case to be made for a more restrictive pattern matching syntax which I may evaluate later, but will leave be for now.
69
+
70
+ ## Installation
71
+
72
+ Add this line to your application's Gemfile:
73
+
74
+ ```ruby
75
+ gem 'toy_adt'
76
+ ```
77
+
78
+ And then execute:
79
+
80
+ $ bundle install
81
+
82
+ Or install it yourself as:
83
+
84
+ $ gem install toy_adt
85
+
86
+ ## Usage
87
+
88
+ TODO: Write usage instructions here
89
+
90
+ ## Development
91
+
92
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
93
+
94
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
95
+
96
+ ## Contributing
97
+
98
+ Bug reports and pull requests are welcome on GitHub at https://github.com/baweaver/toy_adt. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/baweaver/toy_adt/blob/main/CODE_OF_CONDUCT.md).
99
+
100
+ ## License
101
+
102
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
103
+
104
+ ## Code of Conduct
105
+
106
+ Everyone interacting in the ToyAdt project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/baweaver/toy_adt/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,29 @@
1
+ # typed: false
2
+ require "sorbet-runtime"
3
+
4
+ module ToyAdt
5
+ module DeconstructableSorbetStruct
6
+ extend T::Sig
7
+
8
+ sig { returns(T::Array[T.untyped]) }
9
+ def deconstruct
10
+ properties.map { send(_1) }
11
+ end
12
+
13
+ sig {
14
+ params(
15
+ keys: T.nilable(T::Array[Symbol])
16
+ ).returns(T::Hash[Symbol, T.untyped])
17
+ }
18
+ def deconstruct_keys(keys)
19
+ return properties.to_h { |k| [k, send(k)] } if keys.empty? || keys.nil?
20
+
21
+ (properties & keys).to_h { |k| [k, send(k)] }
22
+ end
23
+
24
+ sig { returns(T::Array[Symbol]) }
25
+ def properties
26
+ self.class.props.keys
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,56 @@
1
+ # typed: false
2
+ require "sorbet-runtime"
3
+
4
+ module ToyAdt
5
+ module PublicApi
6
+ extend T::Sig
7
+
8
+ sig {
9
+ params(
10
+ config: T.any(Class, T::Types::Base),
11
+ fn: T.nilable(T.proc.void)
12
+ ).returns(Class)
13
+ }
14
+ def type(**config, &fn)
15
+ Class.new(T::Struct) do
16
+ include DeconstructableSorbetStruct
17
+
18
+ config.each do |field, type|
19
+ const field, type
20
+ end
21
+
22
+ module_eval(&fn) if block_given?
23
+ end
24
+ end
25
+
26
+ sig {
27
+ params(
28
+ types: T::Hash[Symbol, T.any(Class, T::Types::Base)],
29
+ fn: T.proc.void
30
+ ).returns(Module)
31
+ }
32
+ def sum(**types, &fn)
33
+ container_module = Module.new do
34
+ extend T::Helpers
35
+ sealed!
36
+
37
+ module_eval(&fn) if block_given?
38
+ end
39
+
40
+ types.each do |type, config|
41
+ klass = Class.new(T::Struct) do
42
+ include container_module
43
+ include DeconstructableSorbetStruct
44
+
45
+ config.each do |field, type|
46
+ const field, type
47
+ end
48
+ end
49
+
50
+ container_module.const_set(type.capitalize, klass)
51
+ end
52
+
53
+ container_module
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,6 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module ToyAdt
5
+ VERSION = "0.1.0"
6
+ end
data/lib/toy_adt.rb ADDED
@@ -0,0 +1,10 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "toy_adt/version"
5
+ require_relative "toy_adt/deconstructable_sorbet_struct"
6
+ require_relative "toy_adt/public_api"
7
+
8
+ module ToyAdt
9
+ extend PublicApi
10
+ end
data/sorbet/config ADDED
@@ -0,0 +1,3 @@
1
+ --dir
2
+ .
3
+ --ignore=/vendor/bundle