unsound 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.rubocop.yml +43 -0
- data/.rubocop_todo.yml +6 -0
- data/.travis.yml +11 -0
- data/Gemfile +27 -0
- data/LICENSE.txt +22 -0
- data/README.md +35 -0
- data/Rakefile +9 -0
- data/lib/unsound/composition.rb +16 -0
- data/lib/unsound/control.rb +20 -0
- data/lib/unsound/data.rb +101 -0
- data/lib/unsound/version.rb +3 -0
- data/lib/unsound.rb +4 -0
- data/spec/integration/try_spec.rb +52 -0
- data/spec/shared/functor_examples.rb +28 -0
- data/spec/shared/monad_examples.rb +31 -0
- data/spec/spec_helper.rb +91 -0
- data/spec/unit/unsound/control_spec.rb +27 -0
- data/spec/unit/unsound/data/either_spec.rb +122 -0
- data/unsound.gemspec +25 -0
- metadata +127 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7d3d6412d4eda965925cd704816c57bf93db662b
|
4
|
+
data.tar.gz: 58e1df47db4ad389b2ce8663123d874ed7e9445c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ee1992f01cf105fb33b2ab67e9264ce1922d612060b375cda2925c45e9266a6d0b2ee5c35aaeb38ff7f343010625012434a685608f8283bd412ea49b4e7f4552
|
7
|
+
data.tar.gz: bb75c7554b7a173b1bb5588588ee534f46d0baeb05d2643e6f321455de19ff0b6a1c0c9ec20185576db87274b59e7607c488f7a1e82082b16e7e05e97caa39e1
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
inherit_from: .rubocop_todo.yml
|
2
|
+
|
3
|
+
Style/Alias:
|
4
|
+
Enabled: false
|
5
|
+
|
6
|
+
Style/DotPosition:
|
7
|
+
Enabled: false
|
8
|
+
|
9
|
+
Style/Documentation:
|
10
|
+
Enabled: false
|
11
|
+
|
12
|
+
Style/Blocks:
|
13
|
+
Exclude:
|
14
|
+
- 'spec/**/*'
|
15
|
+
|
16
|
+
Style/MultilineBlockChain:
|
17
|
+
Exclude:
|
18
|
+
- 'spec/**/*'
|
19
|
+
|
20
|
+
Style/RegexpLiteral:
|
21
|
+
MaxSlashes: 0
|
22
|
+
Exclude:
|
23
|
+
- '*.gemspec'
|
24
|
+
|
25
|
+
Metrics/LineLength:
|
26
|
+
Max: 96
|
27
|
+
Exclude:
|
28
|
+
- '*.gemspec'
|
29
|
+
|
30
|
+
Style/OpMethod:
|
31
|
+
Enabled: false
|
32
|
+
|
33
|
+
Style/StringLiterals:
|
34
|
+
Enabled: false
|
35
|
+
|
36
|
+
Style/UnneededPercentQ:
|
37
|
+
Enabled: false
|
38
|
+
|
39
|
+
AllCops:
|
40
|
+
Exclude:
|
41
|
+
- 'bin/**/*'
|
42
|
+
- 'vendor/**/*'
|
43
|
+
|
data/.rubocop_todo.yml
ADDED
@@ -0,0 +1,6 @@
|
|
1
|
+
# This configuration was generated by `rubocop --auto-gen-config`
|
2
|
+
# on 2015-02-25 23:42:36 -0500 using RuboCop version 0.29.1.
|
3
|
+
# The point is for the user to remove these configuration records
|
4
|
+
# one by one as the offenses are removed from the code base.
|
5
|
+
# Note that changes in the inspected code, or installation of new
|
6
|
+
# versions of RuboCop, may require this file to be generated again.
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in unsound.gemspec
|
4
|
+
gemspec
|
5
|
+
|
6
|
+
group :test do
|
7
|
+
gem "rspec"
|
8
|
+
end
|
9
|
+
|
10
|
+
group :development do
|
11
|
+
gem "yard"
|
12
|
+
end
|
13
|
+
|
14
|
+
group :debug do
|
15
|
+
gem "pry"
|
16
|
+
gem "pry-byebug"
|
17
|
+
gem "pry-doc"
|
18
|
+
end
|
19
|
+
|
20
|
+
group :metrics do
|
21
|
+
gem "rubocop", require: false
|
22
|
+
|
23
|
+
platform :mri do
|
24
|
+
gem "mutant"
|
25
|
+
gem "mutant-rspec"
|
26
|
+
end
|
27
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Peter Swan
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# Unsound [![Build Status](https://travis-ci.org/pdswan/unsound.svg?branch=master)](https://travis-ci.org/pdswan/unsound) [![Inline docs](http://inch-ci.org/github/pdswan/unsound.svg?branch=master)](http://inch-ci.org/github/pdswan/unsound) [![Code Climate](https://codeclimate.com/github/pdswan/unsound/badges/gpa.svg)](https://codeclimate.com/github/pdswan/unsound)
|
2
|
+
|
3
|
+
Functional constructs inspired by [Haskell](https://www.haskell.org/), written in Ruby.
|
4
|
+
|
5
|
+
Heavily influenced by [Kliesli](https://github.com/txus/kleisli), primariliy undertaken as an experiment motivated by:
|
6
|
+
|
7
|
+
* fun
|
8
|
+
* removing default globals
|
9
|
+
* integrating `try` semantics with `Either`
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
gem 'unsound'
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install unsound
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
Check the specs.
|
28
|
+
|
29
|
+
## Contributing
|
30
|
+
|
31
|
+
1. Fork it ( http://github.com/<my-github-username>/unsound/fork )
|
32
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
33
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
34
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
35
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
module Unsound
|
2
|
+
module Composition
|
3
|
+
module_function
|
4
|
+
|
5
|
+
# Compose two callables together
|
6
|
+
#
|
7
|
+
# g(f(x)) == (g * f)(x)
|
8
|
+
#
|
9
|
+
# @param g [#call] a lambda, proc, method, etc.
|
10
|
+
# @param f [#call] a lambda, proc, method, etc.
|
11
|
+
# @return [Proc]
|
12
|
+
def compose(g, f)
|
13
|
+
->(*args) { g.call(f.call(*args)) }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "English"
|
2
|
+
|
3
|
+
module Unsound
|
4
|
+
module Control
|
5
|
+
module_function
|
6
|
+
|
7
|
+
# Try executing the block. If the block
|
8
|
+
# raises an exception wrap it in a {Data::Left},
|
9
|
+
# otherwise wrap it in a {Data::Right}.
|
10
|
+
#
|
11
|
+
# @param block [Block] the block to execute
|
12
|
+
# @return [Data::Right, Data::Left] an instance of
|
13
|
+
# a {Data::Right} or {Data::Left} to indicate success or failure.
|
14
|
+
def try(&block)
|
15
|
+
Data::Right.new(block.call)
|
16
|
+
rescue
|
17
|
+
Data::Left.new($ERROR_INFO)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/unsound/data.rb
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
require "abstract_type"
|
2
|
+
require "concord"
|
3
|
+
|
4
|
+
module Unsound
|
5
|
+
module Data
|
6
|
+
# @abstract
|
7
|
+
class Either
|
8
|
+
include AbstractType
|
9
|
+
include Concord::Public.new(:value)
|
10
|
+
|
11
|
+
# Wraps a raw value in a {Data::Right}
|
12
|
+
#
|
13
|
+
# @param value [Any] the value to wrap
|
14
|
+
# @return [Data::Right]
|
15
|
+
def self.of(value)
|
16
|
+
Right.new(value)
|
17
|
+
end
|
18
|
+
|
19
|
+
abstract_method :fmap
|
20
|
+
abstract_method :>>
|
21
|
+
|
22
|
+
abstract_method :either
|
23
|
+
abstract_method :and_then
|
24
|
+
abstract_method :or_else
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def of(value)
|
29
|
+
self.class.of(value)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class Left < Either
|
34
|
+
# A Noop
|
35
|
+
#
|
36
|
+
# @return [Data::Left]
|
37
|
+
def fmap(*)
|
38
|
+
self
|
39
|
+
end
|
40
|
+
|
41
|
+
# A Noop
|
42
|
+
#
|
43
|
+
# @return [Data::Left]
|
44
|
+
def >>(*)
|
45
|
+
self
|
46
|
+
end
|
47
|
+
alias :and_then :>>
|
48
|
+
|
49
|
+
# Chain another operation which can result in a {Data::Either}
|
50
|
+
#
|
51
|
+
# @param f[#call] the next operation
|
52
|
+
# @return [Data::Left, Data::Right]
|
53
|
+
def or_else(f = nil, &blk)
|
54
|
+
(f || blk)[value]
|
55
|
+
end
|
56
|
+
|
57
|
+
# Call a function on the value in the {Data::Left}
|
58
|
+
#
|
59
|
+
# @param f [#call] a function capable of processing the value
|
60
|
+
# @param _ [#call] a function that will never be called
|
61
|
+
def either(f, _)
|
62
|
+
f[value]
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
class Right < Either
|
67
|
+
# Apply a function to a value contained in a {Data::Right}
|
68
|
+
#
|
69
|
+
# @param f [#call] the function to apply
|
70
|
+
# @return [Data::Right] the result of applying the function
|
71
|
+
# wrapped in a {Data::Right}
|
72
|
+
def fmap(f = nil, &blk)
|
73
|
+
self >> Composition.compose(method(:of), (f || blk))
|
74
|
+
end
|
75
|
+
|
76
|
+
# Chain another operation which can result in a {Data::Either}
|
77
|
+
#
|
78
|
+
# @param f[#call] the next operation
|
79
|
+
# @return [Data::Left, Data::Right]
|
80
|
+
def >>(f = nil, &blk)
|
81
|
+
(f || blk)[value]
|
82
|
+
end
|
83
|
+
alias :and_then :>>
|
84
|
+
|
85
|
+
# A Noop
|
86
|
+
#
|
87
|
+
# @return [Data::Right]
|
88
|
+
def or_else(*)
|
89
|
+
self
|
90
|
+
end
|
91
|
+
|
92
|
+
# Call a function on the value in the {Data::Right}
|
93
|
+
#
|
94
|
+
# @param _ [#call] a function that will never be called
|
95
|
+
# @param f [#call] a function capable of processing the value
|
96
|
+
def either(_, f)
|
97
|
+
f[value]
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
data/lib/unsound.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require "unsound"
|
2
|
+
|
3
|
+
RSpec.describe "Use cases" do
|
4
|
+
let(:repo) do
|
5
|
+
users.inject({}) do |users, user|
|
6
|
+
users.merge(user.id => user)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
let(:alice) { double(:alice, id: :alice, name: "Alice") }
|
11
|
+
let(:voldemort) { double(:voldemort, id: :voldemort, name: nil) }
|
12
|
+
let(:users) { [alice, voldemort] }
|
13
|
+
|
14
|
+
let(:id) { ->(x) { x } }
|
15
|
+
|
16
|
+
it "allows mapping over the value of a computation" do
|
17
|
+
expect(
|
18
|
+
Unsound::Control.try do
|
19
|
+
repo.fetch(:alice)
|
20
|
+
end.fmap(&:name).fmap(&:upcase)
|
21
|
+
).to eq(Unsound::Data::Right.new("ALICE"))
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "error handling" do
|
25
|
+
describe "a user is not found" do
|
26
|
+
it "allows graceful error handling" do
|
27
|
+
Unsound::Control.try do
|
28
|
+
repo.fetch(:does_not_exist)
|
29
|
+
end.fmap(&:name).fmap(&:upcase).either(
|
30
|
+
->(error) { expect(error).to be_kind_of(KeyError) },
|
31
|
+
->(_) { fail "Can't get here" }
|
32
|
+
)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "failures further down the chain" do
|
37
|
+
it "allows graceful error handling" do
|
38
|
+
Unsound::Control.try do
|
39
|
+
repo.fetch(:voldemort)
|
40
|
+
end.
|
41
|
+
fmap(&:name).
|
42
|
+
# TODO: this is verbose because try eagerly evaluates
|
43
|
+
# is there a good way to make this more terse?
|
44
|
+
and_then { |value| Unsound::Control.try { value.upcase } }.
|
45
|
+
either(
|
46
|
+
->(error) { expect(error).to be_kind_of(NoMethodError) },
|
47
|
+
->(_) { fail "Can't get here" }
|
48
|
+
)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
RSpec.shared_examples_for "a Functor" do
|
2
|
+
let(:id) { ->(x) { x } }
|
3
|
+
let(:dbl) { ->(x) { [x, x] } }
|
4
|
+
let(:fst) { ->(xs) { xs.first } }
|
5
|
+
let(:compose) { Unsound::Composition.public_method(:compose) }
|
6
|
+
|
7
|
+
describe "fmap" do
|
8
|
+
describe "identity" do
|
9
|
+
specify do
|
10
|
+
instances.each do |instance|
|
11
|
+
expect(instance.fmap(id)).to eq(id[instance])
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "composition" do
|
17
|
+
specify do
|
18
|
+
instances.each do |instance|
|
19
|
+
expect(
|
20
|
+
instance.fmap(dbl).fmap(fst)
|
21
|
+
).to eq(
|
22
|
+
instance.fmap(compose[fst, dbl])
|
23
|
+
)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
RSpec.shared_examples_for "a Monad" do
|
2
|
+
let(:f) { ->(x) { type.of([x, x]) } }
|
3
|
+
|
4
|
+
describe "having left identity" do
|
5
|
+
specify do
|
6
|
+
expect(type.of(value) >> f).to eq(f[value])
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "having right identity" do
|
11
|
+
specify do
|
12
|
+
instances.each do |instance|
|
13
|
+
expect(instance >> type.public_method(:of)).to eq(instance)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "having associativity" do
|
19
|
+
specify do
|
20
|
+
instances.each do |instance|
|
21
|
+
expect(
|
22
|
+
instance >> lambda do |a|
|
23
|
+
f[a] >> lambda do |b|
|
24
|
+
f[b]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
).to eq(instance >> f >> f)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,91 @@
|
|
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
|
4
|
+
# this file to always be loaded, without a need to explicitly require it in any
|
5
|
+
# files.
|
6
|
+
#
|
7
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
8
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
9
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
10
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
11
|
+
# a separate helper file that requires the additional dependencies and performs
|
12
|
+
# the additional setup, and require it from the spec files that actually need
|
13
|
+
# it.
|
14
|
+
#
|
15
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
16
|
+
# users commonly want.
|
17
|
+
#
|
18
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
19
|
+
RSpec.configure do |config|
|
20
|
+
# rspec-expectations config goes here. You can use an alternate
|
21
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
22
|
+
# assertions if you prefer.
|
23
|
+
config.expect_with :rspec do |expectations|
|
24
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
25
|
+
# and `failure_message` of custom matchers include text for helper methods
|
26
|
+
# defined using `chain`, e.g.:
|
27
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
28
|
+
# # => "be bigger than 2 and smaller than 4"
|
29
|
+
# ...rather than:
|
30
|
+
# # => "be bigger than 2"
|
31
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
32
|
+
end
|
33
|
+
|
34
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
35
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
36
|
+
config.mock_with :rspec do |mocks|
|
37
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
38
|
+
# a real object. This is generally recommended, and will default to
|
39
|
+
# `true` in RSpec 4.
|
40
|
+
mocks.verify_partial_doubles = true
|
41
|
+
end
|
42
|
+
|
43
|
+
# The settings below are suggested to provide a good initial experience
|
44
|
+
# with RSpec, but feel free to customize to your heart's content.
|
45
|
+
begin
|
46
|
+
# These two settings work together to allow you to limit a spec run
|
47
|
+
# to individual examples or groups you care about by tagging them with
|
48
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
49
|
+
# get run.
|
50
|
+
config.filter_run :focus
|
51
|
+
config.run_all_when_everything_filtered = true
|
52
|
+
|
53
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
54
|
+
# recommended. For more details, see:
|
55
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
56
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
57
|
+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
58
|
+
config.disable_monkey_patching!
|
59
|
+
|
60
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
61
|
+
# be too noisy due to issues in dependencies.
|
62
|
+
config.warnings = true
|
63
|
+
|
64
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
65
|
+
# file, and it's useful to allow more verbose output when running an
|
66
|
+
# individual spec file.
|
67
|
+
if config.files_to_run.one?
|
68
|
+
# Use the documentation formatter for detailed output,
|
69
|
+
# unless a formatter has already been configured
|
70
|
+
# (e.g. via a command-line flag).
|
71
|
+
config.default_formatter = 'doc'
|
72
|
+
end
|
73
|
+
|
74
|
+
# Print the 10 slowest examples and example groups at the
|
75
|
+
# end of the spec run, to help surface which specs are running
|
76
|
+
# particularly slow.
|
77
|
+
config.profile_examples = 10
|
78
|
+
|
79
|
+
# Run specs in random order to surface order dependencies. If you find an
|
80
|
+
# order dependency and want to debug it, you can fix the order by providing
|
81
|
+
# the seed, which is printed after each run.
|
82
|
+
# --seed 1234
|
83
|
+
config.order = :random
|
84
|
+
|
85
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
86
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
87
|
+
# test failures related to randomization by passing the same `--seed` value
|
88
|
+
# as the one that triggered the failure.
|
89
|
+
Kernel.srand config.seed
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require "unsound"
|
2
|
+
|
3
|
+
RSpec.describe Unsound::Control do
|
4
|
+
describe Unsound::Control, ".try" do
|
5
|
+
subject(:run_try) { Unsound::Control.try(&blk) }
|
6
|
+
|
7
|
+
context "the block executes successfully" do
|
8
|
+
let(:blk) { -> { result } }
|
9
|
+
let(:result) { double(:result) }
|
10
|
+
|
11
|
+
it "returns the result of the block wrapped in a Right" do
|
12
|
+
expect(run_try).to be_a(Unsound::Data::Right)
|
13
|
+
expect(run_try.value).to eq(result)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
context "the block raises an exception" do
|
18
|
+
let(:blk) { -> { fail error } }
|
19
|
+
let(:error) { StandardError.new("Something went wrong") }
|
20
|
+
|
21
|
+
it "returns the exception wrapped in a Left" do
|
22
|
+
expect(run_try).to be_a(Unsound::Data::Left)
|
23
|
+
expect(run_try.value).to eq(error)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
require "shared/functor_examples"
|
2
|
+
require "shared/monad_examples"
|
3
|
+
|
4
|
+
require "unsound"
|
5
|
+
|
6
|
+
RSpec.describe Unsound::Data::Either do
|
7
|
+
let(:value) { double(:value) }
|
8
|
+
|
9
|
+
let(:instances) do
|
10
|
+
[
|
11
|
+
Unsound::Data::Right.new(value),
|
12
|
+
Unsound::Data::Left.new(value)
|
13
|
+
]
|
14
|
+
end
|
15
|
+
|
16
|
+
it "has an abstract base class" do
|
17
|
+
expect {
|
18
|
+
Unsound::Data::Either.new("anything")
|
19
|
+
}.to raise_error(NotImplementedError)
|
20
|
+
end
|
21
|
+
|
22
|
+
it_behaves_like "a Functor"
|
23
|
+
|
24
|
+
it_behaves_like "a Monad" do
|
25
|
+
let(:type) { Unsound::Data::Either }
|
26
|
+
|
27
|
+
specify { expect(type.of(value)).to eq(Unsound::Data::Right.new(value)) }
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "#and_then" do
|
31
|
+
let(:and_then) do
|
32
|
+
->(value) { Unsound::Data::Right.new([value, value]) }
|
33
|
+
end
|
34
|
+
|
35
|
+
context "a right" do
|
36
|
+
let(:right) { Unsound::Data::Right.new(value) }
|
37
|
+
let(:value) { double(:value) }
|
38
|
+
|
39
|
+
context "a function" do
|
40
|
+
it "applies the function over the value" do
|
41
|
+
expect(right.and_then(and_then)).to eq(and_then.call(value))
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context "a block" do
|
46
|
+
it "applies the block over the value" do
|
47
|
+
expect(right.and_then(&and_then)).to eq(and_then.call(value))
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context "a left" do
|
53
|
+
let(:left) { Unsound::Data::Left.new(error) }
|
54
|
+
let(:error) { double(:error) }
|
55
|
+
|
56
|
+
it "is a noop returning self" do
|
57
|
+
expect(left.and_then(and_then)).to eq(left)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe "#or_else" do
|
63
|
+
let(:or_else) do
|
64
|
+
->(error) { Unsound::Data::Left.new(error.message) }
|
65
|
+
end
|
66
|
+
|
67
|
+
context "a right" do
|
68
|
+
let(:right) { Unsound::Data::Right.new(value) }
|
69
|
+
let(:value) { double(:value) }
|
70
|
+
|
71
|
+
it "is a noop returning self" do
|
72
|
+
expect(right.or_else(or_else)).to eq(right)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
context "a left" do
|
77
|
+
let(:left) { Unsound::Data::Left.new(error) }
|
78
|
+
let(:error) { double(:error, message: error_message) }
|
79
|
+
let(:error_message) { double(:error_message) }
|
80
|
+
|
81
|
+
context "a function" do
|
82
|
+
it "applies the function over the error" do
|
83
|
+
expect(left.or_else(or_else)).to eq(or_else.call(error))
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
context "a block" do
|
88
|
+
it "applies the block over the error" do
|
89
|
+
expect(left.or_else(&or_else)).to eq(or_else.call(error))
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
describe "#either" do
|
96
|
+
let(:left_fn) { double(:left_fn) }
|
97
|
+
let(:right_fn) { double(:right_fn) }
|
98
|
+
|
99
|
+
let(:left_result) { double(:left_result) }
|
100
|
+
let(:right_result) { double(:right_result) }
|
101
|
+
|
102
|
+
context "a right" do
|
103
|
+
let(:right) { Unsound::Data::Right.new(value) }
|
104
|
+
|
105
|
+
it "calls the right function with the value" do
|
106
|
+
allow(right_fn).to receive(:[]).
|
107
|
+
with(value).and_return(right_result)
|
108
|
+
expect(right.either(left_fn, right_fn)).to eq(right_result)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
context "a left" do
|
113
|
+
let(:left) { Unsound::Data::Left.new(value) }
|
114
|
+
|
115
|
+
it "calls the left function with the value" do
|
116
|
+
allow(left_fn).to receive(:[]).
|
117
|
+
with(value).and_return(left_result)
|
118
|
+
expect(left.either(left_fn, right_fn)).to eq(left_result)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
data/unsound.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'unsound/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "unsound"
|
8
|
+
spec.version = Unsound::VERSION
|
9
|
+
spec.authors = ["Peter Swan"]
|
10
|
+
spec.email = ["pdswan@gmail.com"]
|
11
|
+
spec.summary = %q(General functional concepts inspired by Haskell, implemented in Ruby.)
|
12
|
+
spec.homepage = "https://github.com/pdswan/unsound"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_dependency "concord", "~> 0"
|
21
|
+
spec.add_dependency "abstract_type", "~> 0"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
24
|
+
spec.add_development_dependency "rake", "~> 0"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: unsound
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Peter Swan
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: concord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
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: abstract_type
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.5'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.5'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- pdswan@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- .rspec
|
78
|
+
- .rubocop.yml
|
79
|
+
- .rubocop_todo.yml
|
80
|
+
- .travis.yml
|
81
|
+
- Gemfile
|
82
|
+
- LICENSE.txt
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- lib/unsound.rb
|
86
|
+
- lib/unsound/composition.rb
|
87
|
+
- lib/unsound/control.rb
|
88
|
+
- lib/unsound/data.rb
|
89
|
+
- lib/unsound/version.rb
|
90
|
+
- spec/integration/try_spec.rb
|
91
|
+
- spec/shared/functor_examples.rb
|
92
|
+
- spec/shared/monad_examples.rb
|
93
|
+
- spec/spec_helper.rb
|
94
|
+
- spec/unit/unsound/control_spec.rb
|
95
|
+
- spec/unit/unsound/data/either_spec.rb
|
96
|
+
- unsound.gemspec
|
97
|
+
homepage: https://github.com/pdswan/unsound
|
98
|
+
licenses:
|
99
|
+
- MIT
|
100
|
+
metadata: {}
|
101
|
+
post_install_message:
|
102
|
+
rdoc_options: []
|
103
|
+
require_paths:
|
104
|
+
- lib
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
requirements: []
|
116
|
+
rubyforge_project:
|
117
|
+
rubygems_version: 2.4.5
|
118
|
+
signing_key:
|
119
|
+
specification_version: 4
|
120
|
+
summary: General functional concepts inspired by Haskell, implemented in Ruby.
|
121
|
+
test_files:
|
122
|
+
- spec/integration/try_spec.rb
|
123
|
+
- spec/shared/functor_examples.rb
|
124
|
+
- spec/shared/monad_examples.rb
|
125
|
+
- spec/spec_helper.rb
|
126
|
+
- spec/unit/unsound/control_spec.rb
|
127
|
+
- spec/unit/unsound/data/either_spec.rb
|