monadt 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/monadt.rb +10 -0
- data/lib/monadt/async.rb +24 -0
- data/lib/monadt/async_either.rb +28 -0
- data/lib/monadt/monad.rb +45 -6
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7746a87e5355708be40ab3440bb65ea902e9add0
|
4
|
+
data.tar.gz: 6baed7771d0bb368794f0314d26bc18f8f51d292
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 61d461c44b602499a075876e5883731553f173db40ef3a267dcc099c1061ab6a3bc399a169ba8763df39ba7d1e266b7f6fc9c1f5df74f47d436709084fbfba67
|
7
|
+
data.tar.gz: fd8712a4f9092c683b906ab02908afae83ef4497f9bfbb5f3e5c82a8ad9a65c25155a3e76c2dd300fa3b5c3a9c0994bae501bb67a310c3346fdfea06e5f90d02
|
data/lib/monadt.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
require 'monadt/maybe'
|
2
2
|
require 'monadt/either'
|
3
|
+
require 'monadt/async'
|
3
4
|
require 'monadt/state'
|
5
|
+
require 'monadt/async_either'
|
4
6
|
require 'monadt/reader_state_either'
|
5
7
|
require 'monadt/monad'
|
6
8
|
|
@@ -19,6 +21,14 @@ module Monadt
|
|
19
21
|
do_m(Either, &blk)
|
20
22
|
end
|
21
23
|
|
24
|
+
def async(&blk)
|
25
|
+
do_m(Async, &blk)
|
26
|
+
end
|
27
|
+
|
28
|
+
def async_either(&blk)
|
29
|
+
do_m(AsyncEither, &blk)
|
30
|
+
end
|
31
|
+
|
22
32
|
def state(&blk)
|
23
33
|
do_m(State, &blk)
|
24
34
|
end
|
data/lib/monadt/async.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'monadt/adt'
|
2
|
+
|
3
|
+
module Monadt
|
4
|
+
class Async
|
5
|
+
class << self
|
6
|
+
|
7
|
+
def bind(m, &blk)
|
8
|
+
a = m.resume
|
9
|
+
m2 = blk.call(a)
|
10
|
+
if m2.alive?
|
11
|
+
m2
|
12
|
+
else
|
13
|
+
self.return a
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def return(a)
|
18
|
+
Fiber.new { a }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'monadt/adt'
|
2
|
+
require 'monadt/either'
|
3
|
+
|
4
|
+
module Monadt
|
5
|
+
class AsyncEither
|
6
|
+
class << self
|
7
|
+
include Adt
|
8
|
+
|
9
|
+
def bind(m, &blk)
|
10
|
+
a = m.resume
|
11
|
+
match(a,
|
12
|
+
with(Either::Left) { |v| Async.return a },
|
13
|
+
with(Either::Right) { |v|
|
14
|
+
m2 = blk.call(v)
|
15
|
+
if m2.alive?
|
16
|
+
m2
|
17
|
+
else
|
18
|
+
Async.return a
|
19
|
+
end
|
20
|
+
})
|
21
|
+
end
|
22
|
+
|
23
|
+
def return(a)
|
24
|
+
Async.return(Either.return(a))
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/monadt/monad.rb
CHANGED
@@ -1,9 +1,25 @@
|
|
1
|
+
require 'fiber'
|
2
|
+
|
1
3
|
module Monadt
|
2
4
|
module Internal
|
3
|
-
class
|
5
|
+
class MonadObjFiber
|
6
|
+
def initialize(klass)
|
7
|
+
@klass = klass
|
8
|
+
end
|
9
|
+
|
10
|
+
def return(val)
|
11
|
+
@klass.return val
|
12
|
+
end
|
13
|
+
|
14
|
+
def bind(val)
|
15
|
+
Fiber.yield val
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class MonadObjEnum
|
4
20
|
def initialize(klass, yielder)
|
5
|
-
@yielder = yielder
|
6
21
|
@klass = klass
|
22
|
+
@yielder = yielder
|
7
23
|
end
|
8
24
|
|
9
25
|
def return(val)
|
@@ -19,14 +35,37 @@ module Monadt
|
|
19
35
|
class Monad
|
20
36
|
class << self
|
21
37
|
def do_m(klass, &blk)
|
38
|
+
do_m_fiber(klass, &blk)
|
39
|
+
end
|
40
|
+
|
41
|
+
def do_m_fiber(klass, &blk)
|
42
|
+
f = Fiber.new do |y|
|
43
|
+
m_obj = Internal::MonadObjFiber.new klass
|
44
|
+
blk.call(m_obj)
|
45
|
+
end
|
46
|
+
do_m_recur_fiber(klass, f, nil)
|
47
|
+
end
|
48
|
+
|
49
|
+
def do_m_recur_fiber(klass, f, ma, *args)
|
50
|
+
if f.alive?
|
51
|
+
ma = f.resume(*args)
|
52
|
+
klass.bind(ma) do |a|
|
53
|
+
do_m_recur_fiber(klass, f, ma, a)
|
54
|
+
end
|
55
|
+
else
|
56
|
+
ma
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def do_m_enum(klass, &blk)
|
22
61
|
e = Enumerator.new do |y|
|
23
|
-
m_obj = Internal::
|
62
|
+
m_obj = Internal::MonadObjEnum.new klass, y
|
24
63
|
blk.call(m_obj)
|
25
64
|
end
|
26
|
-
|
65
|
+
do_m_recur_enum(klass, e)
|
27
66
|
end
|
28
67
|
|
29
|
-
def
|
68
|
+
def do_m_recur_enum(klass, e)
|
30
69
|
begin
|
31
70
|
ma = e.next
|
32
71
|
rescue StopIteration => ex
|
@@ -34,7 +73,7 @@ module Monadt
|
|
34
73
|
end
|
35
74
|
klass.bind(ma) do |a|
|
36
75
|
e.feed a
|
37
|
-
|
76
|
+
do_m_recur_enum(klass, e)
|
38
77
|
end
|
39
78
|
end
|
40
79
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: monadt
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Will Pleasant-Ryan
|
@@ -19,6 +19,8 @@ extra_rdoc_files: []
|
|
19
19
|
files:
|
20
20
|
- lib/monadt.rb
|
21
21
|
- lib/monadt/adt.rb
|
22
|
+
- lib/monadt/async.rb
|
23
|
+
- lib/monadt/async_either.rb
|
22
24
|
- lib/monadt/either.rb
|
23
25
|
- lib/monadt/list.rb
|
24
26
|
- lib/monadt/maybe.rb
|