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 +4 -4
- data/lib/rstructural.rb +4 -0
- data/lib/rstructural/adt.rb +7 -1
- data/lib/rstructural/either.rb +110 -0
- data/lib/rstructural/enum.rb +2 -0
- data/lib/rstructural/option.rb +61 -0
- data/lib/rstructural/struct.rb +2 -0
- data/rstructural.gemspec +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4a7909862f6c8e1218224beba82ea52bb2ca8ccd2d489d8f988c3c2e4c760537
|
4
|
+
data.tar.gz: 407a43807cbdd57c068b533398b54737f1160c95fc0c02d64237b8def7f3221d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e66a4919105969c110c63701cf6dcc866e4b52317c96c13dcd5d2878677a5377b428e73d81c553288c7287eb3aca04713df345523a5de3935457837e3c7248c9
|
7
|
+
data.tar.gz: 91e03c79a6d525cab6e5ced41c08078b813aeaaccf8eb8173aeb92a363f47895623d7ffdb8aadc6fe9a7b8896b0e4afa280a5986d8ec711c2223442fa60a3034
|
data/lib/rstructural.rb
CHANGED
@@ -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
|
data/lib/rstructural/adt.rb
CHANGED
@@ -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
|
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
|
data/lib/rstructural/enum.rb
CHANGED
@@ -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
|
data/lib/rstructural/struct.rb
CHANGED
data/rstructural.gemspec
CHANGED
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.
|
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-
|
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
|