data.maybe 0.0.12 → 0.0.13
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/control/monad.rb +4 -4
- data/lib/data.maybe.rb +16 -17
- data/lib/helper.rb +5 -5
- data/lib/union_type.rb +12 -12
- 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: cf5826f8ce957c248e621d76ca965962825c43a6
|
4
|
+
data.tar.gz: 861712e3517ade55bc00de8a541b3a16c2b28ed9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fbddafc1ca82703d5c6590c9f1aaa8085bbaec368614028c9676e0de8089a7c4c8f8244cbb2cdccb147f78f096991dfe738b26a9b94876a18e80b3c2f69d7a4d
|
7
|
+
data.tar.gz: af3d0b57a63c5ee41f20131f97935754a25e9f08d7d177bd130991cd7d36a6a569e828c38cf0ff185cc51aa95a53bb5fbb9d95d5f7a9ae40af0674155f77c839
|
data/lib/control/monad.rb
CHANGED
@@ -13,7 +13,7 @@ module Control
|
|
13
13
|
raise 'apply No defined'
|
14
14
|
end
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
module Monad
|
18
18
|
extend Helper
|
19
19
|
include Functor
|
@@ -23,9 +23,9 @@ module Control
|
|
23
23
|
end
|
24
24
|
|
25
25
|
alias_names [:bind, :chain], :flat_map
|
26
|
-
|
27
|
-
def >>
|
28
|
-
|
26
|
+
|
27
|
+
def >>(k)
|
28
|
+
flat_map { |_| k }
|
29
29
|
end
|
30
30
|
end
|
31
31
|
end
|
data/lib/data.maybe.rb
CHANGED
@@ -9,17 +9,17 @@ module Maybe
|
|
9
9
|
include UnionType
|
10
10
|
# Either only contain one value @v
|
11
11
|
# @return [Either]
|
12
|
-
def initialize
|
12
|
+
def initialize(v = nil)
|
13
13
|
@v = v
|
14
14
|
end
|
15
15
|
|
16
16
|
# get value `a` out from `Right a`, otherwise return `e`
|
17
|
-
def get_or_else
|
17
|
+
def get_or_else(e)
|
18
18
|
case self
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
19
|
+
when Just
|
20
|
+
@v
|
21
|
+
else
|
22
|
+
e
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
@@ -38,7 +38,7 @@ module Maybe
|
|
38
38
|
self
|
39
39
|
end
|
40
40
|
end
|
41
|
-
|
41
|
+
|
42
42
|
# it override {Monad#flat_map}, as Haskell's `>flat_map` method
|
43
43
|
# if it's {Right}, pass the value to #flat_map's block, and flat the result
|
44
44
|
# of the block.
|
@@ -68,12 +68,12 @@ module Maybe
|
|
68
68
|
# Right.new(1).when({Left: ->x{x+1}, _: ->x{x-1} }) # => 0
|
69
69
|
# ```
|
70
70
|
# @return [Either]
|
71
|
-
def when
|
71
|
+
def when(what)
|
72
72
|
current_class = self.class.to_s.to_sym
|
73
73
|
if what.include? current_class
|
74
|
-
what[current_class].(@v)
|
74
|
+
what[current_class].call(@v)
|
75
75
|
elsif what.include? :_
|
76
|
-
what[:_].(@v)
|
76
|
+
what[:_].call(@v)
|
77
77
|
end
|
78
78
|
end
|
79
79
|
|
@@ -83,18 +83,17 @@ module Maybe
|
|
83
83
|
when Just
|
84
84
|
"#<Just #{@v}>"
|
85
85
|
else
|
86
|
-
|
86
|
+
'#<Nothing>'
|
87
87
|
end
|
88
88
|
end
|
89
|
-
|
89
|
+
alias inspect to_s
|
90
90
|
end
|
91
91
|
|
92
|
-
|
93
92
|
class Nothing
|
94
93
|
include Singleton
|
95
94
|
include Maybe
|
96
|
-
|
97
|
-
def ==
|
95
|
+
|
96
|
+
def ==(other)
|
98
97
|
case other
|
99
98
|
when Nothing
|
100
99
|
true
|
@@ -108,8 +107,8 @@ Nothing = Nothing.instance
|
|
108
107
|
|
109
108
|
class Just
|
110
109
|
include Maybe
|
111
|
-
|
112
|
-
def ==
|
110
|
+
|
111
|
+
def ==(other)
|
113
112
|
case other
|
114
113
|
when Just
|
115
114
|
other.map { |v| return v == @v }
|
data/lib/helper.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
module Helper
|
2
|
-
def alias_names
|
3
|
-
names.each do |name|
|
4
|
-
define_method(name) do |*args, &block|
|
5
|
-
|
2
|
+
def alias_names(names, m)
|
3
|
+
names.each do |name|
|
4
|
+
define_method(name) do |*args, &block|
|
5
|
+
send(m, *args, &block)
|
6
6
|
end
|
7
7
|
end
|
8
|
-
end
|
8
|
+
end
|
9
9
|
end
|
data/lib/union_type.rb
CHANGED
@@ -1,21 +1,21 @@
|
|
1
1
|
require 'helper'
|
2
2
|
module UnionType
|
3
3
|
extend Helper
|
4
|
-
# similar to Scala's `match` for case class
|
5
|
-
#
|
6
|
-
# will pattern match the value out and pass to matched lambda
|
7
|
-
# ```ruby
|
8
|
-
# Right.new(1).when({Right: ->x{x+1} }) # => 2
|
9
|
-
# Right.new(1).when({Left: ->x{x+1}) # => nil
|
10
|
-
# Right.new(1) =~ ({Left: ->x{x+1}, _: ->x{x-1} }) # => 0
|
11
|
-
# ```
|
12
|
-
# @return [Either]
|
13
|
-
def when
|
4
|
+
# similar to Scala's `match` for case class
|
5
|
+
#
|
6
|
+
# will pattern match the value out and pass to matched lambda
|
7
|
+
# ```ruby
|
8
|
+
# Right.new(1).when({Right: ->x{x+1} }) # => 2
|
9
|
+
# Right.new(1).when({Left: ->x{x+1}) # => nil
|
10
|
+
# Right.new(1) =~ ({Left: ->x{x+1}, _: ->x{x-1} }) # => 0
|
11
|
+
# ```
|
12
|
+
# @return [Either]
|
13
|
+
def when(what)
|
14
14
|
current_class = self.class.to_s.to_sym
|
15
15
|
if what.include? current_class
|
16
|
-
what[current_class].(@v)
|
16
|
+
what[current_class].call(@v)
|
17
17
|
elsif what.include? :_
|
18
|
-
what[:_].(@v)
|
18
|
+
what[:_].call(@v)
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|