monadt 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/monadt/adt.rb +28 -0
- data/lib/monadt/either.rb +7 -16
- data/lib/monadt/maybe.rb +6 -16
- data/lib/monadt/reader.rb +1 -1
- data/lib/monadt/reader_state_either.rb +2 -2
- data/lib/monadt/state.rb +1 -1
- data/lib/monadt.rb +6 -6
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d8691f7fc7008c12954eb7391133089ae6241b0e
|
4
|
+
data.tar.gz: 6410e6eb457068ab362a423bf11bd1f725d79337
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0b1a02941b1b59cdb845a44ae0c5e92473999a5722662d8328dc5098bf2219ae30cf445d16a6377e1657fe2346f5156761227a494d039d6b26fbbdba79d6e470
|
7
|
+
data.tar.gz: 8fe3f176625326ceb70f6855541bfb41555d62d5723b767266e88c6e523a3cf7082ab3662c910810b4497404c7c0a7b5cab5eae03f057fc37b716559a5d89af9
|
data/lib/monadt/adt.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'pry'
|
2
|
+
|
1
3
|
AdtPattern = Struct.new :klass, :lambda
|
2
4
|
|
3
5
|
def data(*fields)
|
@@ -9,6 +11,32 @@ def data(*fields)
|
|
9
11
|
Class.new(base)
|
10
12
|
end
|
11
13
|
|
14
|
+
def decorate_adt(klass)
|
15
|
+
klass.constants.each do |v|
|
16
|
+
name = v.to_s.downcase
|
17
|
+
const = klass.const_get v
|
18
|
+
klass.constants.each do |is_v|
|
19
|
+
is_name = is_v.to_s.downcase
|
20
|
+
ret = is_v == v
|
21
|
+
const.class_eval do
|
22
|
+
define_method "is_#{is_name}?" do ret end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
const.class_eval do
|
26
|
+
define_method "to_s" do
|
27
|
+
if respond_to? :values
|
28
|
+
"#{v.to_s}(#{values.join(", ")})"
|
29
|
+
else
|
30
|
+
v.to_s
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
klass.class_eval do
|
35
|
+
define_singleton_method name do |*values| const.new(*values) end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
12
40
|
Default = data
|
13
41
|
|
14
42
|
module Adt
|
data/lib/monadt/either.rb
CHANGED
@@ -1,35 +1,26 @@
|
|
1
1
|
require 'monadt/adt'
|
2
2
|
|
3
3
|
module Monadt
|
4
|
-
|
4
|
+
class Either
|
5
5
|
Left = data :left
|
6
6
|
Right = data :right
|
7
|
-
end
|
8
|
-
class Either::Left
|
9
|
-
def is_left?() true end
|
10
|
-
def is_right?() false end
|
11
|
-
def to_s() "Left(#{left})" end
|
12
|
-
end
|
13
|
-
class Either::Right
|
14
|
-
def is_left?() false end
|
15
|
-
def is_right?() true end
|
16
|
-
def to_s() "Right(#{right})" end
|
17
|
-
end
|
18
7
|
|
19
|
-
class EitherM
|
20
8
|
class << self
|
21
9
|
include Adt
|
22
10
|
|
23
11
|
def bind(m, &blk)
|
24
12
|
match(m,
|
25
|
-
with(
|
26
|
-
with(
|
13
|
+
with(Left) { |v| m },
|
14
|
+
with(Right) { |v| blk.call(v) })
|
27
15
|
end
|
28
16
|
|
29
17
|
def return(a)
|
30
|
-
|
18
|
+
right a
|
31
19
|
end
|
32
20
|
end
|
33
21
|
|
34
22
|
end
|
23
|
+
|
24
|
+
decorate_adt Either
|
35
25
|
end
|
26
|
+
|
data/lib/monadt/maybe.rb
CHANGED
@@ -1,38 +1,28 @@
|
|
1
1
|
require 'monadt/adt'
|
2
2
|
|
3
3
|
module Monadt
|
4
|
-
|
4
|
+
class Maybe
|
5
5
|
Just = data :value
|
6
6
|
Nothing = data
|
7
|
-
end
|
8
|
-
|
9
|
-
class Maybe::Just
|
10
|
-
def is_nothing?() false end
|
11
|
-
def to_s() "Just #{value}" end
|
12
|
-
end
|
13
|
-
class Maybe::Nothing
|
14
|
-
def is_nothing?() true end
|
15
|
-
def to_s() "Nothing" end
|
16
|
-
end
|
17
7
|
|
18
|
-
class MaybeM
|
19
8
|
class << self
|
20
9
|
include Adt
|
21
10
|
|
22
11
|
def bind(m, &blk)
|
23
12
|
match(m,
|
24
|
-
with(
|
25
|
-
with(
|
13
|
+
with(Just) { |v| blk.call(v) },
|
14
|
+
with(Nothing) { m })
|
26
15
|
end
|
27
16
|
|
28
17
|
def return(a)
|
29
|
-
Maybe
|
18
|
+
Maybe.just a
|
30
19
|
end
|
31
20
|
end
|
32
21
|
end
|
22
|
+
decorate_adt Maybe
|
33
23
|
|
34
24
|
# like Maybe but nil / not-nill
|
35
|
-
class
|
25
|
+
class Present
|
36
26
|
class << self
|
37
27
|
def bind(m, &blk)
|
38
28
|
if m.nil?
|
data/lib/monadt/reader.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'monadt/adt'
|
2
2
|
|
3
3
|
module Monadt
|
4
|
-
class
|
4
|
+
class ReaderStateEither
|
5
5
|
class << self
|
6
6
|
include Adt
|
7
7
|
|
@@ -17,7 +17,7 @@ module Monadt
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def return(val)
|
20
|
-
->(e, s) { [
|
20
|
+
->(e, s) { [Either.return(val), s] }
|
21
21
|
end
|
22
22
|
end
|
23
23
|
end
|
data/lib/monadt/state.rb
CHANGED
data/lib/monadt.rb
CHANGED
@@ -8,19 +8,19 @@ module Monadt
|
|
8
8
|
class Monad
|
9
9
|
class << self
|
10
10
|
def maybe(&blk)
|
11
|
-
do_m(
|
11
|
+
do_m(Maybe, &blk)
|
12
12
|
end
|
13
13
|
|
14
14
|
def present(&blk)
|
15
|
-
do_m(
|
15
|
+
do_m(Present, &blk)
|
16
16
|
end
|
17
17
|
|
18
18
|
def either(&blk)
|
19
|
-
do_m(
|
19
|
+
do_m(Either, &blk)
|
20
20
|
end
|
21
21
|
|
22
22
|
def state(&blk)
|
23
|
-
do_m(
|
23
|
+
do_m(State, &blk)
|
24
24
|
end
|
25
25
|
|
26
26
|
def run_state(initial_state, &blk)
|
@@ -29,7 +29,7 @@ module Monadt
|
|
29
29
|
end
|
30
30
|
|
31
31
|
def reader(&blk)
|
32
|
-
do_m(
|
32
|
+
do_m(Reader, &blk)
|
33
33
|
end
|
34
34
|
|
35
35
|
def run_reader(env, &blk)
|
@@ -37,7 +37,7 @@ module Monadt
|
|
37
37
|
end
|
38
38
|
|
39
39
|
def reader_state_choice(&blk)
|
40
|
-
do_m(
|
40
|
+
do_m(ReaderStateEither, &blk)
|
41
41
|
end
|
42
42
|
|
43
43
|
def run_reader_state_choice(env, initial_state, &blk)
|