rb-kgy-fp 1.0.1 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rb-kgy-fp/fun.rb +1 -1
- data/lib/rb-kgy-fp/typeclass/either.rb +21 -2
- data/lib/rb-kgy-fp/typeclass/maybe.rb +23 -6
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3697ca15bcf22fea89e6e2621f8165abf24edb989a0fb5269755834c9809dab0
|
4
|
+
data.tar.gz: 14ed08760f9c239697eb33a4461b561753bb207da4ad94d6a4dec6376a92c989
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9097a06a37b571fc51befabb6adb786fb5daf27cdde571690d01400955435e1e2e4214194235148da51370d218d484f5ee35015ec854133056288c777ea96c0c
|
7
|
+
data.tar.gz: 33e72b36d9f8a247386aea220afafdc11cc72c12c42f08b70c766e72b401ee480c77fe2e3a6c93ba3b5de6fceb9979a08e97ae26c5db6477a21b68acf3fe0670
|
data/lib/rb-kgy-fp/fun.rb
CHANGED
@@ -16,6 +16,8 @@ class Either
|
|
16
16
|
|
17
17
|
public
|
18
18
|
|
19
|
+
def inspect = to_s
|
20
|
+
|
19
21
|
def initialize value
|
20
22
|
@value = value
|
21
23
|
end
|
@@ -43,9 +45,18 @@ class Either
|
|
43
45
|
other.right? ? 1 : @value <=> other.value
|
44
46
|
end
|
45
47
|
end
|
48
|
+
|
49
|
+
def get
|
50
|
+
throw RuntimeError
|
51
|
+
end
|
52
|
+
|
53
|
+
def get_left
|
54
|
+
throw RuntimeError
|
55
|
+
end
|
46
56
|
end
|
47
57
|
|
48
58
|
class Left < Either
|
59
|
+
|
49
60
|
def assoc other
|
50
61
|
other
|
51
62
|
end
|
@@ -67,7 +78,7 @@ class Left < Either
|
|
67
78
|
end
|
68
79
|
|
69
80
|
def to_s
|
70
|
-
"Left(#{@value})"
|
81
|
+
"Left(#{@value.to_s})"
|
71
82
|
end
|
72
83
|
|
73
84
|
def either(map_left, _)
|
@@ -77,6 +88,10 @@ class Left < Either
|
|
77
88
|
def bimap(map_fst, map_snd)
|
78
89
|
Left.new map_fst.(@value)
|
79
90
|
end
|
91
|
+
|
92
|
+
def get_left
|
93
|
+
@value
|
94
|
+
end
|
80
95
|
end
|
81
96
|
|
82
97
|
class Right < Either
|
@@ -101,7 +116,7 @@ class Right < Either
|
|
101
116
|
end
|
102
117
|
|
103
118
|
def to_s
|
104
|
-
"Right(#{@value})"
|
119
|
+
"Right(#{@value.to_s})"
|
105
120
|
end
|
106
121
|
|
107
122
|
def either(_, map_right)
|
@@ -111,4 +126,8 @@ class Right < Either
|
|
111
126
|
def bimap(map_fst, map_snd)
|
112
127
|
of map_snd.(@value)
|
113
128
|
end
|
129
|
+
|
130
|
+
def get
|
131
|
+
@value
|
132
|
+
end
|
114
133
|
end
|
@@ -5,13 +5,12 @@ require_relative '../fun'
|
|
5
5
|
require_relative '../curry_fun'
|
6
6
|
require_relative '../trait/monad'
|
7
7
|
require_relative '../trait/alternative'
|
8
|
+
require 'singleton'
|
8
9
|
|
9
10
|
class Maybe
|
10
11
|
include Monad, Alternative, Comparable
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
public
|
13
|
+
def inspect = to_s
|
15
14
|
|
16
15
|
def just?
|
17
16
|
raise Special::UNIMPLEMENTED
|
@@ -34,16 +33,23 @@ class Maybe
|
|
34
33
|
end
|
35
34
|
|
36
35
|
def self.empty
|
37
|
-
Nothing.
|
36
|
+
Nothing.instance
|
38
37
|
end
|
39
|
-
|
38
|
+
|
40
39
|
def consume(other)
|
41
40
|
replace other
|
42
41
|
end
|
42
|
+
|
43
|
+
def get
|
44
|
+
throw RuntimeError
|
45
|
+
end
|
43
46
|
end
|
44
47
|
|
45
48
|
class Just < Maybe
|
46
49
|
attr_reader :value
|
50
|
+
|
51
|
+
def to_s = "Just(#{@value.to_s})"
|
52
|
+
|
47
53
|
def initialize value
|
48
54
|
@value = value
|
49
55
|
end
|
@@ -64,7 +70,7 @@ class Just < Maybe
|
|
64
70
|
if other.just?
|
65
71
|
of fn.(@value, other.value)
|
66
72
|
else
|
67
|
-
Nothing.
|
73
|
+
Nothing.instance
|
68
74
|
end
|
69
75
|
end
|
70
76
|
|
@@ -80,9 +86,20 @@ class Just < Maybe
|
|
80
86
|
fn.(@value)
|
81
87
|
end
|
82
88
|
|
89
|
+
def get
|
90
|
+
@value
|
91
|
+
end
|
83
92
|
end
|
84
93
|
|
85
94
|
class Nothing < Maybe
|
95
|
+
include Singleton
|
96
|
+
|
97
|
+
def to_s = 'Nothing'
|
98
|
+
|
99
|
+
def self.of _ = nil
|
100
|
+
instance
|
101
|
+
end
|
102
|
+
|
86
103
|
def just?
|
87
104
|
false
|
88
105
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rb-kgy-fp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Phil Lui
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-09-
|
11
|
+
date: 2023-09-05 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Ruby FP lib for self usage. This lib included simple FP functions like
|
14
14
|
Rmada of JS, and different typeclass support as Haskell
|