limonad 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/LICENSE.txt +22 -0
- data/README.md +85 -0
- data/Rakefile +2 -0
- data/lib/limonad.rb +48 -0
- data/lib/limonad/version.rb +3 -0
- data/spec/examples.txt +7 -0
- data/spec/logger_monad_spec.rb +32 -0
- data/spec/maybe_monad_spec.rb +33 -0
- data/spec/spec_helper.rb +96 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 842e58b8be3b186e2bf15d8a4fd30da41c8503af
|
4
|
+
data.tar.gz: f063e0540939917baad9d597d7f193d3027f06b2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 611c5863fd154fbfb57894eeca10c00b6cac246ceddf4be869b4e9eeecfa789e5718da627328facbdb69003ed94884cb845faa68d348a6265858aabc4581dc04
|
7
|
+
data.tar.gz: 4772c55b1dadc98d6ba4e959db74a839552b70abab357168e6a173be134aecb6a3264a1a18d07f6625e434ba1cef1d50835728cfea7651146a99cce5d52bc1fb
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Chaker Nakhli - chaker.nakhli@sinbadsoft.com
|
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,85 @@
|
|
1
|
+
# LiMonad
|
2
|
+
|
3
|
+
LiMonad is a tiny library for building monads.
|
4
|
+
|
5
|
+
Here are some examples of how to build common monads with LiMonad:
|
6
|
+
|
7
|
+
## Maybe Monad
|
8
|
+
|
9
|
+
Example:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
MaybeMonad >>
|
13
|
+
-> x { 5 / x unless x == 0 } >>
|
14
|
+
-> x { x + 1 } >>
|
15
|
+
-> x { 3 * x } >>
|
16
|
+
-> x { Math.log(x) unless x < 0 } >>
|
17
|
+
-> x { x.round }
|
18
|
+
```
|
19
|
+
|
20
|
+
Monad Definition:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
class MaybeMonad < LiMonad
|
24
|
+
def bind(f, g, *args)
|
25
|
+
result = f[*args]
|
26
|
+
g[result] if result
|
27
|
+
end
|
28
|
+
end
|
29
|
+
```
|
30
|
+
|
31
|
+
## Logger Monad
|
32
|
+
|
33
|
+
Example:
|
34
|
+
```ruby
|
35
|
+
LoggerMonad >>
|
36
|
+
-> x { [x * x, "#{x} was squared"] } >>
|
37
|
+
-> x { [x / 2, "#{x} was halved"] }
|
38
|
+
```
|
39
|
+
Monad Definition:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
class LoggerMonad < LiMonad
|
43
|
+
def bind(f, g, *args)
|
44
|
+
f_result, f_log = f[*args]
|
45
|
+
g_result, g_log = g[f_result]
|
46
|
+
[g_result, "#{f_log}\n#{g_log}"]
|
47
|
+
end
|
48
|
+
|
49
|
+
def unit(g, *args)
|
50
|
+
[g[*args], args.inspect]
|
51
|
+
end
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
55
|
+
## Installation
|
56
|
+
|
57
|
+
Add this line to your application's Gemfile:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
gem 'limonad'
|
61
|
+
```
|
62
|
+
|
63
|
+
And then execute:
|
64
|
+
|
65
|
+
$ bundle
|
66
|
+
|
67
|
+
Or install it yourself as:
|
68
|
+
|
69
|
+
$ gem install limonad
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
## Contributing
|
74
|
+
|
75
|
+
1. Fork it ( https://github.com/nakhli/limonad/fork )
|
76
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
77
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
78
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
79
|
+
5. Create a new Pull Request
|
80
|
+
|
81
|
+
## License
|
82
|
+
|
83
|
+
Licensed under the [MIT License](http://opensource.org/licenses/MIT).
|
84
|
+
|
85
|
+
Copyright Chaker Nakhli.
|
data/Rakefile
ADDED
data/lib/limonad.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'limonad/version'
|
2
|
+
|
3
|
+
class LiMonad
|
4
|
+
|
5
|
+
def initialize(f=nil)
|
6
|
+
self.fn = f
|
7
|
+
end
|
8
|
+
|
9
|
+
def [](*args)
|
10
|
+
fn[*args]
|
11
|
+
end
|
12
|
+
|
13
|
+
def >>(g)
|
14
|
+
do_bind(g)
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.>>(g)
|
18
|
+
self.new >> g
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.unit(f, *args)
|
22
|
+
self.new.unit(f, *args)
|
23
|
+
end
|
24
|
+
|
25
|
+
protected
|
26
|
+
|
27
|
+
# child monads should override these methods
|
28
|
+
def unit(f, *args)
|
29
|
+
f[*args]
|
30
|
+
end
|
31
|
+
|
32
|
+
def bind(f, g, *args)
|
33
|
+
g[f[*args]]
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
attr_accessor :fn
|
39
|
+
|
40
|
+
def do_unit(f)
|
41
|
+
self.class.new -> *args { unit(f, *args) }
|
42
|
+
end
|
43
|
+
|
44
|
+
def do_bind(g)
|
45
|
+
fn ? self.class.new(-> *args { bind(fn, g, *args) }) : self.class.new(g)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
data/spec/examples.txt
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
example_id | status | run_time |
|
2
|
+
-------------------------------- | ------ | --------------- |
|
3
|
+
./spec/logger_monad_spec.rb[1:1] | passed | 0.00009 seconds |
|
4
|
+
./spec/logger_monad_spec.rb[1:2] | passed | 0.00052 seconds |
|
5
|
+
./spec/maybe_monad_spec.rb[1:1] | passed | 0.00012 seconds |
|
6
|
+
./spec/maybe_monad_spec.rb[1:2] | passed | 0.00011 seconds |
|
7
|
+
./spec/maybe_monad_spec.rb[1:3] | passed | 0.00233 seconds |
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class LoggerMonad < LiMonad
|
4
|
+
def bind(f, g, *args)
|
5
|
+
f_result, f_log = f[*args]
|
6
|
+
g_result, g_log = g[f_result]
|
7
|
+
[g_result, "#{f_log}\n#{g_log}"]
|
8
|
+
end
|
9
|
+
|
10
|
+
def unit(g, *args)
|
11
|
+
[g[*args], args.inspect]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
RSpec.describe LoggerMonad do
|
16
|
+
|
17
|
+
let(:op) do
|
18
|
+
LoggerMonad >>
|
19
|
+
-> x { [x * x, "#{x} was squared"] } >>
|
20
|
+
-> x { [x / 2, "#{x} was halved"] }
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'computes the correct result' do
|
24
|
+
result, _ = op[100]
|
25
|
+
expect(result).to eq(5000)
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'does the correct logging' do
|
29
|
+
_, log = op[100]
|
30
|
+
expect(log).to eq("100 was squared\n10000 was halved")
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class MaybeMonad < LiMonad
|
4
|
+
def bind(f, g, *args)
|
5
|
+
result = f[*args]
|
6
|
+
g[result] if result
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
RSpec.describe MaybeMonad do
|
11
|
+
|
12
|
+
let(:math_op) do
|
13
|
+
MaybeMonad >>
|
14
|
+
-> x { 5 / x unless x == 0 } >>
|
15
|
+
-> x { x + 1 } >>
|
16
|
+
-> x { 3 * x } >>
|
17
|
+
-> x { Math.log(x) unless x < 0 } >>
|
18
|
+
-> x { x.round }
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'propagates not nil values' do
|
23
|
+
expect(math_op[0.01]).to eq(7)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'does not propagate nil values' do
|
27
|
+
expect(math_op[0]).to be_nil
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'does not propagate nil values' do
|
31
|
+
expect(math_op[-0.8]).to be_nil
|
32
|
+
end
|
33
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,96 @@
|
|
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
|
+
require 'limonad'
|
19
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
20
|
+
RSpec.configure do |config|
|
21
|
+
# rspec-expectations config goes here. You can use an alternate
|
22
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
23
|
+
# assertions if you prefer.
|
24
|
+
config.expect_with :rspec do |expectations|
|
25
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
26
|
+
# and `failure_message` of custom matchers include text for helper methods
|
27
|
+
# defined using `chain`, e.g.:
|
28
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
29
|
+
# # => "be bigger than 2 and smaller than 4"
|
30
|
+
# ...rather than:
|
31
|
+
# # => "be bigger than 2"
|
32
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
33
|
+
end
|
34
|
+
|
35
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
36
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
37
|
+
config.mock_with :rspec do |mocks|
|
38
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
39
|
+
# a real object. This is generally recommended, and will default to
|
40
|
+
# `true` in RSpec 4.
|
41
|
+
mocks.verify_partial_doubles = true
|
42
|
+
end
|
43
|
+
|
44
|
+
# The settings below are suggested to provide a good initial experience
|
45
|
+
# with RSpec, but feel free to customize to your heart's content.
|
46
|
+
|
47
|
+
# These two settings work together to allow you to limit a spec run
|
48
|
+
# to individual examples or groups you care about by tagging them with
|
49
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
50
|
+
# get run.
|
51
|
+
config.filter_run :focus
|
52
|
+
config.run_all_when_everything_filtered = true
|
53
|
+
|
54
|
+
# Allows RSpec to persist some state between runs in order to support
|
55
|
+
# the `--only-failures` and `--next-failure` CLI options. We recommend
|
56
|
+
# you configure your source control system to ignore this file.
|
57
|
+
config.example_status_persistence_file_path = 'spec/examples.txt'
|
58
|
+
|
59
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
60
|
+
# recommended. For more details, see:
|
61
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
62
|
+
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
63
|
+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
64
|
+
config.disable_monkey_patching!
|
65
|
+
|
66
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
67
|
+
# be too noisy due to issues in dependencies.
|
68
|
+
config.warnings = true
|
69
|
+
|
70
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
71
|
+
# file, and it's useful to allow more verbose output when running an
|
72
|
+
# individual spec file.
|
73
|
+
if config.files_to_run.one?
|
74
|
+
# Use the documentation formatter for detailed output,
|
75
|
+
# unless a formatter has already been configured
|
76
|
+
# (e.g. via a command-line flag).
|
77
|
+
config.default_formatter = 'doc'
|
78
|
+
end
|
79
|
+
|
80
|
+
# Print the 10 slowest examples and example groups at the
|
81
|
+
# end of the spec run, to help surface which specs are running
|
82
|
+
# particularly slow.
|
83
|
+
config.profile_examples = 10
|
84
|
+
|
85
|
+
# Run specs in random order to surface order dependencies. If you find an
|
86
|
+
# order dependency and want to debug it, you can fix the order by providing
|
87
|
+
# the seed, which is printed after each run.
|
88
|
+
# --seed 1234
|
89
|
+
config.order = :random
|
90
|
+
|
91
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
92
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
93
|
+
# test failures related to randomization by passing the same `--seed` value
|
94
|
+
# as the one that triggered the failure.
|
95
|
+
Kernel.srand config.seed
|
96
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: limonad
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Chaker Nakhli
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-07-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.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.1'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.1'
|
55
|
+
description: A tiny Monad library.
|
56
|
+
email:
|
57
|
+
- chaker.nakhli@sinbadsoft.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- LICENSE.txt
|
63
|
+
- README.md
|
64
|
+
- Rakefile
|
65
|
+
- lib/limonad.rb
|
66
|
+
- lib/limonad/version.rb
|
67
|
+
- spec/examples.txt
|
68
|
+
- spec/logger_monad_spec.rb
|
69
|
+
- spec/maybe_monad_spec.rb
|
70
|
+
- spec/spec_helper.rb
|
71
|
+
homepage: https://github.com/nakhli/limonad
|
72
|
+
licenses:
|
73
|
+
- MIT
|
74
|
+
metadata: {}
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options:
|
77
|
+
- "--charset=UTF-8"
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: 2.0.0
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.4.6
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: A tiny Monad library.
|
96
|
+
test_files:
|
97
|
+
- spec/examples.txt
|
98
|
+
- spec/logger_monad_spec.rb
|
99
|
+
- spec/maybe_monad_spec.rb
|
100
|
+
- spec/spec_helper.rb
|