adts 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 488ac93106452d551665d5e7a01015b56fed473c
4
+ data.tar.gz: d87357f5358c234a2a480e4180aefc76ad43f26b
5
+ SHA512:
6
+ metadata.gz: a266603d63209ff785eebcb2fa4315b8a4f7716a22572611185b349a6f01a850220d46ab470b13b98f3259616eb38d29d8a6a673f8b55b3ef62e83c30c9fe3aa
7
+ data.tar.gz: 2cea76a6c954c2afebf85e543b78a7aa934c1b5368902b5c289a559797fb231dcf374c7c6992c8387cfd91bf31a54cff1323821c96ce3ef0ede3e00dc9cde3a1
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.1.2
5
+ - rbx-2
6
+ - ruby-head
7
+ - jruby-head
8
+
9
+ script:
10
+ - rake
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in adt.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Josep M. Bach
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,102 @@
1
+ # ADTs [![Build Status](https://secure.travis-ci.org/txus/adts.png)](http://travis-ci.org/txus/adts)
2
+
3
+ [Algebraic Data Types][adts] for Ruby.
4
+
5
+ ## Usage
6
+
7
+ Let's define a Shape data type:
8
+
9
+ ```ruby
10
+ require 'adts'
11
+
12
+ Shape = ADT do
13
+ Void() |
14
+ Square(width: Fixnum) |
15
+ Rectangle(width: Fixnum, height: Fixnum) |
16
+ Circle(radius: Fixnum) {
17
+ def area
18
+ Math::PI * radius * radius
19
+ end
20
+ }
21
+ end
22
+ ```
23
+
24
+ Let's try an instantiate a Shape with our nullary constructor Void:
25
+
26
+ ```ruby
27
+ Shape::Void()
28
+ # => #<Shape::Void ...>
29
+ ```
30
+
31
+ What about a square?
32
+
33
+ ```ruby
34
+ Shape::Square(23)
35
+ # => #<Shape::Square @width=23>
36
+ ```
37
+
38
+ Our type constructors are even **type-checked**:
39
+
40
+ ```ruby
41
+ Shape::Square("foo")
42
+ # raises a TypeError
43
+ ```
44
+
45
+ Our ADT implements equality by type and value:
46
+
47
+ ```ruby
48
+ Shape::Square(23) == Shape::Square(23)
49
+ # => true
50
+ Shape::Circle(23) == Shape::Square(23)
51
+ # => false
52
+ Shape::Square(23) == Shape::Square(99)
53
+ # => false
54
+ ```
55
+
56
+ All its instances expose (read-only) their respective parameters:
57
+
58
+ ```ruby
59
+ Shape::Square(23).width
60
+ # => 23
61
+ ```
62
+
63
+ All instances are a kind of `Shape`:
64
+
65
+ ```ruby
66
+ Shape::Square(23).is_a?(Shape)
67
+ # => true
68
+ ```
69
+
70
+ And finally, our constructors can have their own special methods, just like we
71
+ defined `area` on `Circle`:
72
+
73
+ ```ruby
74
+ Shape::Circle(1).area
75
+ # => 3.141592653589793
76
+ ```
77
+
78
+ ## Installation
79
+
80
+ Add this line to your application's Gemfile:
81
+
82
+ ```ruby
83
+ gem 'adts'
84
+ ```
85
+
86
+ And then execute:
87
+
88
+ $ bundle
89
+
90
+ Or install it yourself as:
91
+
92
+ $ gem install adts
93
+
94
+ ## Who's this
95
+
96
+ This was made by [Josep M. Bach (Txus)](http://blog.txus.io) under the MIT
97
+ license. I am [@txustice][twitter] on twitter (where you should probably follow
98
+ me!).
99
+
100
+ [twitter]: https://twitter.com/txustice
101
+ [adts]: http://en.wikipedia.org/wiki/Algebraic_data_type
102
+
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ begin
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ rescue LoadError
7
+ end
8
+
9
+ task :default => :spec
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'adt/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "adts"
8
+ spec.version = ADT::VERSION
9
+ spec.authors = ["Josep M. Bach"]
10
+ spec.email = ["josep.m.bach@gmail.com"]
11
+ spec.summary = %q{Abstract Data Types for Ruby}
12
+ spec.description = %q{Abstract Data Types for Ruby}
13
+ spec.homepage = "http://blog.txus.io/adts"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "rspec"
22
+ spec.add_development_dependency "bundler", "~> 1.7"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ end
@@ -0,0 +1,23 @@
1
+ require "adt/version"
2
+ require "adt/constructor"
3
+
4
+ module ADT
5
+ end
6
+
7
+ def ADT(&block)
8
+ Class.new.tap do |klass|
9
+ o = Object.new
10
+
11
+ o.define_singleton_method(:method_missing) do |m, *args, &block|
12
+ ADT::Constructor.new(klass, m, args.first || {}, &block)
13
+ end
14
+
15
+ first = o.instance_eval(&block)
16
+ [first, *first.others].each do |tc|
17
+ klass.const_set(tc.name, tc.klass)
18
+ klass.singleton_class.send(:define_method, tc.name) do |*args|
19
+ const_get(tc.name).new(*args)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,40 @@
1
+ module ADT
2
+ class Constructor
3
+ attr_reader :name, :klass, :others
4
+
5
+ def initialize(parent, name, parameters, &block)
6
+ @name = name
7
+ @klass = Class.new(parent, &block)
8
+ keys = parameters.keys
9
+ types = parameters.values
10
+ @klass.class_eval """
11
+ attr_reader #{keys.map { |n| ":#{n}" }.join(', ')}
12
+
13
+ def initialize(#{keys.join(", ")})
14
+ types = [#{keys.join(", ")}].map(&:class)
15
+ raise TypeError, 'Types mismatch: given ' + types.join(', ') + ', expected #{types.join(", ")}' unless types == [#{types.join(", ")}]
16
+ #{keys.empty? ? "" : keys.map{ |n| "@#{n}" }.join(',') + " = " + keys.join(", ")}
17
+ end
18
+
19
+ def ==(other)
20
+ other.is_a?(self.class) && #{keys.empty? ? "true" : keys.map { |key| "#{key} == other.#{key}"}.join(" && ")}
21
+ end
22
+ "
23
+ @parameters = parameters
24
+ @others = []
25
+ end
26
+
27
+ def to_s
28
+ "#{@name}(#{@parameters})"
29
+ end
30
+
31
+ def inspect
32
+ to_s
33
+ end
34
+
35
+ def |(other)
36
+ @others << other
37
+ self
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ module ADT
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,42 @@
1
+ require 'rspec'
2
+ require 'adt'
3
+
4
+ Shape = ADT do
5
+ Void() |
6
+ Square(width: Fixnum) |
7
+ Rectangle(width: Fixnum, height: Fixnum) |
8
+ Circle(radius: Fixnum) {
9
+ def area
10
+ Math::PI * radius * radius
11
+ end
12
+ }
13
+ end
14
+
15
+ describe ADT do
16
+ it 'allows for a nullary constructor' do
17
+ expect(Shape::Void()).to be_kind_of(Shape::Void)
18
+ end
19
+
20
+ it 'typechecks the non-nullary constructors'do
21
+ expect { Shape::Square(23) }.to_not raise_exception
22
+ expect { Shape::Square("foo") }.to raise_exception(TypeError)
23
+ end
24
+
25
+ it 'implements equality by type and value' do
26
+ expect(Shape::Square(23)).to eq(Shape::Square(23))
27
+ expect(Shape::Circle(23)).to_not eq(Shape::Square(23))
28
+ expect(Shape::Square(23)).to_not eq(Shape::Square(55))
29
+ end
30
+
31
+ it 'exposes readers for the constructor parameters' do
32
+ expect(Shape::Square(23).width).to eq(23)
33
+ end
34
+
35
+ it 'allows for custom methods on type constructors' do
36
+ expect(Shape::Circle(1).area).to eq(Math::PI)
37
+ end
38
+
39
+ it 'uses subtyping' do
40
+ expect(Shape::Circle(1)).to be_kind_of(Shape)
41
+ end
42
+ end
@@ -0,0 +1,89 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
4
+ # file to always be loaded, without a need to explicitly require it in any files.
5
+ #
6
+ # Given that it is always loaded, you are encouraged to keep this file as
7
+ # light-weight as possible. Requiring heavyweight dependencies from this file
8
+ # will add to the boot time of your test suite on EVERY test run, even for an
9
+ # individual file that may not need all of that loaded. Instead, consider making
10
+ # a separate helper file that requires the additional dependencies and performs
11
+ # the additional setup, and require it from the spec files that actually need it.
12
+ #
13
+ # The `.rspec` file also contains a few flags that are not defaults but that
14
+ # users commonly want.
15
+ #
16
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
17
+ RSpec.configure do |config|
18
+ # rspec-expectations config goes here. You can use an alternate
19
+ # assertion/expectation library such as wrong or the stdlib/minitest
20
+ # assertions if you prefer.
21
+ config.expect_with :rspec do |expectations|
22
+ # This option will default to `true` in RSpec 4. It makes the `description`
23
+ # and `failure_message` of custom matchers include text for helper methods
24
+ # defined using `chain`, e.g.:
25
+ # be_bigger_than(2).and_smaller_than(4).description
26
+ # # => "be bigger than 2 and smaller than 4"
27
+ # ...rather than:
28
+ # # => "be bigger than 2"
29
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
30
+ end
31
+
32
+ # rspec-mocks config goes here. You can use an alternate test double
33
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
34
+ config.mock_with :rspec do |mocks|
35
+ # Prevents you from mocking or stubbing a method that does not exist on
36
+ # a real object. This is generally recommended, and will default to
37
+ # `true` in RSpec 4.
38
+ mocks.verify_partial_doubles = true
39
+ end
40
+
41
+ # The settings below are suggested to provide a good initial experience
42
+ # with RSpec, but feel free to customize to your heart's content.
43
+ =begin
44
+ # These two settings work together to allow you to limit a spec run
45
+ # to individual examples or groups you care about by tagging them with
46
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
47
+ # get run.
48
+ config.filter_run :focus
49
+ config.run_all_when_everything_filtered = true
50
+
51
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
52
+ # For more details, see:
53
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
54
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
55
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
56
+ config.disable_monkey_patching!
57
+
58
+ # This setting enables warnings. It's recommended, but in some cases may
59
+ # be too noisy due to issues in dependencies.
60
+ config.warnings = true
61
+
62
+ # Many RSpec users commonly either run the entire suite or an individual
63
+ # file, and it's useful to allow more verbose output when running an
64
+ # individual spec file.
65
+ if config.files_to_run.one?
66
+ # Use the documentation formatter for detailed output,
67
+ # unless a formatter has already been configured
68
+ # (e.g. via a command-line flag).
69
+ config.default_formatter = 'doc'
70
+ end
71
+
72
+ # Print the 10 slowest examples and example groups at the
73
+ # end of the spec run, to help surface which specs are running
74
+ # particularly slow.
75
+ config.profile_examples = 10
76
+
77
+ # Run specs in random order to surface order dependencies. If you find an
78
+ # order dependency and want to debug it, you can fix the order by providing
79
+ # the seed, which is printed after each run.
80
+ # --seed 1234
81
+ config.order = :random
82
+
83
+ # Seed global randomization in this process using the `--seed` CLI option.
84
+ # Setting this allows you to use `--seed` to deterministically reproduce
85
+ # test failures related to randomization by passing the same `--seed` value
86
+ # as the one that triggered the failure.
87
+ Kernel.srand config.seed
88
+ =end
89
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: adts
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Josep M. Bach
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: Abstract Data Types for Ruby
56
+ email:
57
+ - josep.m.bach@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - adts.gemspec
70
+ - lib/adt.rb
71
+ - lib/adt/constructor.rb
72
+ - lib/adt/version.rb
73
+ - spec/adt_spec.rb
74
+ - spec/spec_helper.rb
75
+ homepage: http://blog.txus.io/adts
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.2.2
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Abstract Data Types for Ruby
99
+ test_files:
100
+ - spec/adt_spec.rb
101
+ - spec/spec_helper.rb