rstructural 0.2.0 → 0.3.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2127165f6a372306fb6f9e518457b57705636560c187ff59aa85418d41c4c725
4
- data.tar.gz: cfc374a5b5a5694719ffb3a9142d2d5990bb8c21df3c48a47168e253e0a3bd2a
3
+ metadata.gz: 4a7909862f6c8e1218224beba82ea52bb2ca8ccd2d489d8f988c3c2e4c760537
4
+ data.tar.gz: 407a43807cbdd57c068b533398b54737f1160c95fc0c02d64237b8def7f3221d
5
5
  SHA512:
6
- metadata.gz: 85a86e425fc24176d3019c2694cf9ea942b806292b8a61a86e106c5adcb4200722199d95e6ba2bf0ba0b79f29c723176953e7de8e3a44e2d9e8b1a5914826e77
7
- data.tar.gz: 27efa42db6472c158a3c28df59ae3ab80863bbcb20b4e8f824aa6a539524ad5711aec5b495ba659273de25488adeb7b04aee9574940f341678be88ff3e860986
6
+ metadata.gz: e66a4919105969c110c63701cf6dcc866e4b52317c96c13dcd5d2878677a5377b428e73d81c553288c7287eb3aca04713df345523a5de3935457837e3c7248c9
7
+ data.tar.gz: 91e03c79a6d525cab6e5ced41c08078b813aeaaccf8eb8173aeb92a363f47895623d7ffdb8aadc6fe9a7b8896b0e4afa280a5986d8ec711c2223442fa60a3034
@@ -1,6 +1,8 @@
1
1
  require_relative './rstructural/struct'
2
2
  require_relative './rstructural/enum'
3
3
  require_relative './rstructural/adt'
4
+ require_relative './rstructural/option'
5
+ require_relative './rstructural/either'
4
6
 
5
7
  module Rstructural
6
8
  end
@@ -9,3 +11,5 @@ end
9
11
  Rstruct = Rstructural::Struct
10
12
  Enum = Rstructural::Enum
11
13
  ADT = Rstructural::ADT
14
+ Option = Rstructural::Option
15
+ Either = Rstructural::Either
@@ -5,6 +5,7 @@ require_relative './struct'
5
5
  module Rstructural::ADT
6
6
  def self.extended(klass)
7
7
  klass.class_variable_set(:@@adt_types, [])
8
+ klass.class_variable_set(:@@adt_super_class, klass)
8
9
  end
9
10
 
10
11
  def const(value = nil, &block)
@@ -14,6 +15,8 @@ module Rstructural::ADT
14
15
  Rstructural::Struct.new(__caller: caller, &block).new
15
16
  end.tap do |k|
16
17
  self.class_variable_get(:@@adt_types) << k
18
+ k.class.include(self.class_variable_get(:@@adt_super_class))
19
+
17
20
  def k.name
18
21
  self.class.name
19
22
  end
@@ -21,7 +24,10 @@ module Rstructural::ADT
21
24
  end
22
25
 
23
26
  def data(*fields, &block)
24
- Rstructural::Struct.new(*fields, __caller: caller, &block).tap { |k| self.class_variable_get(:@@adt_types) << k }
27
+ Rstructural::Struct.new(*fields, __caller: caller, &block).tap do |k|
28
+ k.include(self.class_variable_get(:@@adt_super_class))
29
+ self.class_variable_get(:@@adt_types) << k
30
+ end
25
31
  end
26
32
 
27
33
  def interface(&block)
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+ require_relative './adt'
3
+
4
+ module Rstructural::Either
5
+ extend Rstructural::ADT
6
+
7
+ Left = data :value
8
+ Right = data :value
9
+
10
+ def self.left(obj)
11
+ Left.new(obj)
12
+ end
13
+
14
+ def self.right(obj)
15
+ Right.new(obj)
16
+ end
17
+
18
+ def self.try(&block)
19
+ result = block.call
20
+ Right.new(result)
21
+ rescue => e
22
+ Left.new(e)
23
+ end
24
+
25
+ interface do
26
+ def right?
27
+ case self
28
+ in Left
29
+ false
30
+ in Right
31
+ true
32
+ end
33
+ end
34
+
35
+ def left?
36
+ !right?
37
+ end
38
+
39
+ def map(&f)
40
+ case self
41
+ in Left
42
+ self
43
+ in Right[value]
44
+ Right.new(f.call(value))
45
+ end
46
+ end
47
+
48
+ def map_left(&f)
49
+ case self
50
+ in Left[value]
51
+ Left.new(f.call(value))
52
+ in Right
53
+ self
54
+ end
55
+ end
56
+
57
+ def flat_map(&f)
58
+ case self
59
+ in Left
60
+ self
61
+ in Right[value]
62
+ f.call(value)
63
+ end
64
+ end
65
+
66
+ def flat_map_left(&f)
67
+ case self
68
+ in Left[value]
69
+ f.call(value)
70
+ in Right
71
+ self
72
+ end
73
+ end
74
+
75
+ def swap
76
+ case self
77
+ in Left[value]
78
+ Right.new(value)
79
+ in Right[value]
80
+ Left.new(value)
81
+ end
82
+ end
83
+
84
+ def right_or_else(default = nil)
85
+ case self
86
+ in Right[value]
87
+ value
88
+ in Left
89
+ if block_given?
90
+ yield
91
+ else
92
+ default
93
+ end
94
+ end
95
+ end
96
+
97
+ def left_or_else(default = nil)
98
+ case self
99
+ in Left[value]
100
+ value
101
+ in Right
102
+ if block_given?
103
+ yield
104
+ else
105
+ default
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
@@ -5,6 +5,7 @@ require_relative './struct'
5
5
  module Rstructural::Enum
6
6
  def self.extended(klass)
7
7
  klass.class_variable_set(:@@enum_values, [])
8
+ klass.class_variable_set(:@@enum_super_class, klass)
8
9
  end
9
10
 
10
11
  def enum(value, &block)
@@ -13,6 +14,7 @@ module Rstructural::Enum
13
14
  end
14
15
  Rstructural::Struct.new(:value, __caller: caller, &block).new(value).tap do |k|
15
16
  self.class_variable_get(:@@enum_values) << k
17
+ k.class.include(self.class_variable_get(:@@enum_super_class))
16
18
  def k.name
17
19
  self.class.name
18
20
  end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+ require_relative './adt'
3
+
4
+ module Rstructural::Option
5
+ extend Rstructural::ADT
6
+
7
+ Some = data :value
8
+ None = const do
9
+ def value
10
+ nil
11
+ end
12
+ end
13
+
14
+ def self.of(obj)
15
+ obj.nil? ? None : Some.new(obj)
16
+ end
17
+
18
+ interface do
19
+ def map(&f)
20
+ case self
21
+ in Some[value]
22
+ Option.of(f.call(value))
23
+ in None
24
+ None
25
+ end
26
+ end
27
+
28
+ def flat_map(&f)
29
+ case self
30
+ in Some[value]
31
+ f.call(value)
32
+ in None
33
+ None
34
+ end
35
+ end
36
+
37
+ def get_or_else(default = nil)
38
+ case self
39
+ in Some[value]
40
+ value
41
+ in None
42
+ if block_given?
43
+ yield
44
+ else
45
+ default
46
+ end
47
+ end
48
+ end
49
+
50
+ def flatten
51
+ case self
52
+ in Some[value] if value.is_a?(Option)
53
+ value.flatten
54
+ in Some[value]
55
+ Option.of(value)
56
+ in None
57
+ None
58
+ end
59
+ end
60
+ end
61
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+ #
1
3
  module Rstructural
2
4
  module Struct
3
5
  def self.new(*attributes, __caller: nil, &block)
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "rstructural"
3
- spec.version = "0.2.0"
3
+ spec.version = "0.3.0"
4
4
  spec.authors = ["petitviolet"]
5
5
  spec.email = ["violethero0820@gmail.com"]
6
6
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rstructural
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - petitviolet
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-01-14 00:00:00.000000000 Z
11
+ date: 2020-01-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: byebug
@@ -43,7 +43,9 @@ files:
43
43
  - bin/setup
44
44
  - lib/rstructural.rb
45
45
  - lib/rstructural/adt.rb
46
+ - lib/rstructural/either.rb
46
47
  - lib/rstructural/enum.rb
48
+ - lib/rstructural/option.rb
47
49
  - lib/rstructural/struct.rb
48
50
  - rstructural.gemspec
49
51
  homepage: https://github.com/petitviolet/rstructural