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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c19e86f991142006e0e23f98ec59c2a9780db485
4
- data.tar.gz: 52cc959d8eeb05fa7d617d51278ec9b2aed00467
3
+ metadata.gz: d8691f7fc7008c12954eb7391133089ae6241b0e
4
+ data.tar.gz: 6410e6eb457068ab362a423bf11bd1f725d79337
5
5
  SHA512:
6
- metadata.gz: a06ffff7277f97c3a16652bbb45ed0d0c13a9ad382772a58d372dbdf9b93e2ae45ce9eafdca7612ed29dffc9a5c1513a4be98d2cae09fb128450e2f5142f91d4
7
- data.tar.gz: a61dab72be3127c89f11d33ad49af0094838073554719fd2f10de78a6408120302c9818c5843ec71b94c379f43d0348e962dbd5a80591cd545b3e6d6bea23b93
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
- module Either
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(Either::Left) { |v| m },
26
- with(Either::Right) { |v| blk.call(v) })
13
+ with(Left) { |v| m },
14
+ with(Right) { |v| blk.call(v) })
27
15
  end
28
16
 
29
17
  def return(a)
30
- Either::Right.new a
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
- module Maybe
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(Maybe::Just) { |v| blk.call(v) },
25
- with(Maybe::Nothing) { m })
13
+ with(Just) { |v| blk.call(v) },
14
+ with(Nothing) { m })
26
15
  end
27
16
 
28
17
  def return(a)
29
- Maybe::Just.new a
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 PresentM
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,5 +1,5 @@
1
1
  module Monadt
2
- class ReaderM
2
+ class Reader
3
3
  class << self
4
4
  def bind(m, &blk)
5
5
  ->(e) {
@@ -1,7 +1,7 @@
1
1
  require 'monadt/adt'
2
2
 
3
3
  module Monadt
4
- class ReaderStateEitherM
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) { [EitherM.return(val), s] }
20
+ ->(e, s) { [Either.return(val), s] }
21
21
  end
22
22
  end
23
23
  end
data/lib/monadt/state.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Monadt
2
- class StateM
2
+ class State
3
3
  class << self
4
4
  def bind(m, &blk)
5
5
  ->(st) {
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(MaybeM, &blk)
11
+ do_m(Maybe, &blk)
12
12
  end
13
13
 
14
14
  def present(&blk)
15
- do_m(PresentM, &blk)
15
+ do_m(Present, &blk)
16
16
  end
17
17
 
18
18
  def either(&blk)
19
- do_m(EitherM, &blk)
19
+ do_m(Either, &blk)
20
20
  end
21
21
 
22
22
  def state(&blk)
23
- do_m(StateM, &blk)
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(ReaderM, &blk)
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(ReaderStateEitherM, &blk)
40
+ do_m(ReaderStateEither, &blk)
41
41
  end
42
42
 
43
43
  def run_reader_state_choice(env, initial_state, &blk)
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.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Will Pleasant-Ryan