monadic 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.md +4 -1
- data/lib/monadic/maybe.rb +2 -0
- data/lib/monadic/version.rb +1 -1
- data/spec/examples/applicative_spec.rb +48 -0
- data/spec/maybe_spec.rb +5 -0
- metadata +11 -4
data/CHANGELOG.md
CHANGED
data/lib/monadic/maybe.rb
CHANGED
data/lib/monadic/version.rb
CHANGED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
# Is a transformation C[A -> B] -> C[A] -> C[B]
|
4
|
+
module Applicative
|
5
|
+
# @param [Proc, lambda] proc
|
6
|
+
# @return [Functor] functor
|
7
|
+
def apply(value)
|
8
|
+
return value.class.unit(@value.call(value.fetch))
|
9
|
+
end
|
10
|
+
alias :<< :apply
|
11
|
+
end
|
12
|
+
|
13
|
+
class Maybe
|
14
|
+
include Applicative
|
15
|
+
end
|
16
|
+
|
17
|
+
class Either
|
18
|
+
include Applicative
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
describe Applicative do
|
23
|
+
it 'allows applying monads' do
|
24
|
+
l = lambda {|x,y| x + y }.curry
|
25
|
+
res = Just(l) << Just(1) << Just(3)
|
26
|
+
res.should == Just(4)
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'applicative composition success' do
|
30
|
+
settings = -> { Success({setting: 1}) }
|
31
|
+
db = ->(pr) { [pr, :x, :y, ] }
|
32
|
+
ws = ->(pr) { "Rock'n'Roll #{pr}" }
|
33
|
+
construct = ->(preferences, database, webservice) { "#{preferences} #{database} #{webservice}" }.curry
|
34
|
+
|
35
|
+
res = Either(construct) >= settings >= db >= ws
|
36
|
+
res.should == Success("Rock'n'Roll [{:setting=>1}, :x, :y]")
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'applicative composition failure' do
|
40
|
+
settings = -> { Success({a: 1}) }
|
41
|
+
db = ->(pr) { Failure('too many connections') }
|
42
|
+
ws = ->(pr) { raise 'should have never called me' }
|
43
|
+
construct = ->(preferences, database, webservice) { "never executed" }.curry
|
44
|
+
|
45
|
+
res = Either(construct) + settings + db + ws
|
46
|
+
res.should == Failure('too many connections')
|
47
|
+
end
|
48
|
+
end
|
data/spec/maybe_spec.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: monadic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-05-
|
12
|
+
date: 2012-05-29 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -152,6 +152,7 @@ files:
|
|
152
152
|
- monadic.gemspec
|
153
153
|
- spec/core_ext/object_spec.rb
|
154
154
|
- spec/either_spec.rb
|
155
|
+
- spec/examples/applicative_spec.rb
|
155
156
|
- spec/jruby_fixes.rb
|
156
157
|
- spec/maybe_spec.rb
|
157
158
|
- spec/monad_axioms.rb
|
@@ -170,25 +171,31 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
170
171
|
- - ! '>='
|
171
172
|
- !ruby/object:Gem::Version
|
172
173
|
version: '0'
|
174
|
+
segments:
|
175
|
+
- 0
|
176
|
+
hash: 634459162394168513
|
173
177
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
174
178
|
none: false
|
175
179
|
requirements:
|
176
180
|
- - ! '>='
|
177
181
|
- !ruby/object:Gem::Version
|
178
182
|
version: '0'
|
183
|
+
segments:
|
184
|
+
- 0
|
185
|
+
hash: 634459162394168513
|
179
186
|
requirements: []
|
180
187
|
rubyforge_project:
|
181
|
-
rubygems_version: 1.8.
|
188
|
+
rubygems_version: 1.8.24
|
182
189
|
signing_key:
|
183
190
|
specification_version: 3
|
184
191
|
summary: see README
|
185
192
|
test_files:
|
186
193
|
- spec/core_ext/object_spec.rb
|
187
194
|
- spec/either_spec.rb
|
195
|
+
- spec/examples/applicative_spec.rb
|
188
196
|
- spec/jruby_fixes.rb
|
189
197
|
- spec/maybe_spec.rb
|
190
198
|
- spec/monad_axioms.rb
|
191
199
|
- spec/monad_spec.rb
|
192
200
|
- spec/spec_helper.rb
|
193
201
|
- spec/validation_spec.rb
|
194
|
-
has_rdoc:
|