data.either 0.0.10 → 0.0.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/control/monad.rb +0 -1
- data/lib/data.either.rb +9 -33
- data/lib/union_type.rb +23 -0
- metadata +6 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 716a0f0e519869682c5554f64500a3fbe5cd5bec
|
4
|
+
data.tar.gz: 1d73e02964b01840553f7272909c269fe95cf82a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4136c9b93f2c5d840d3c158f6a51045f3c3eb50ac0f7542ff828b8dc6a595808a9b970ff7ae1628ec7e29fcab3ea3867d89a68cb6f9a73c5b10e38ddb191b488
|
7
|
+
data.tar.gz: d159723ccbdd525385d37a7e11e1303f4ac2d76b6f06e47b9a56cc20495b0e16e6831d31e579c2d68fd0cec7773922f4957019d8ae05758b1c858ea44cfaaffa
|
data/lib/control/monad.rb
CHANGED
data/lib/data.either.rb
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
require 'control/monad'
|
2
|
-
|
2
|
+
require 'union_type'
|
3
3
|
# The `Either` union type represents values with two possibilities:
|
4
4
|
#
|
5
5
|
# `Either a b` is either `Left a` or `Right b`
|
6
6
|
module Either
|
7
7
|
include Comparable
|
8
8
|
include Control::Monad
|
9
|
-
|
9
|
+
include UnionType
|
10
10
|
# Either only contain one value @v
|
11
11
|
# @return [Either]
|
12
|
-
def initialize v
|
13
|
-
@v
|
12
|
+
def initialize v=nil
|
13
|
+
@v=v
|
14
14
|
end
|
15
15
|
|
16
16
|
# default `false`, should override in {Left} or {Right}
|
@@ -115,27 +115,6 @@ module Either
|
|
115
115
|
end
|
116
116
|
end
|
117
117
|
|
118
|
-
# similar to Scala's `match` for case class
|
119
|
-
#
|
120
|
-
# will pattern match the value out and pass to matched lambda
|
121
|
-
# ```ruby
|
122
|
-
# Right.new(1).when({Right: ->x{x+1} }) # => 2
|
123
|
-
# Right.new(1).when({Left: ->x{x+1}) # => nil
|
124
|
-
# Right.new(1) =~ ({Left: ->x{x+1}, _: ->x{x-1} }) # => 0
|
125
|
-
# ```
|
126
|
-
# @return [Either]
|
127
|
-
def when what
|
128
|
-
current_class = self.class.to_s.to_sym
|
129
|
-
if what.include? current_class
|
130
|
-
what[current_class].(@v)
|
131
|
-
elsif what.include? :_
|
132
|
-
what[:_].(@v)
|
133
|
-
end
|
134
|
-
end
|
135
|
-
|
136
|
-
alias_method :match, :when
|
137
|
-
alias_method :=~, :when
|
138
|
-
|
139
118
|
# swap type of [Either]
|
140
119
|
#
|
141
120
|
# ```ruby
|
@@ -154,7 +133,7 @@ module Either
|
|
154
133
|
|
155
134
|
alias_method :swap, :~@
|
156
135
|
def each &block
|
157
|
-
bimap(->_{}, &
|
136
|
+
bimap(->_{}, &block)
|
158
137
|
end
|
159
138
|
|
160
139
|
def to_a
|
@@ -180,15 +159,15 @@ module Either
|
|
180
159
|
end
|
181
160
|
|
182
161
|
# @return [String]
|
183
|
-
def
|
162
|
+
def to_s
|
184
163
|
case self
|
185
164
|
when Left
|
186
|
-
"#<Left
|
165
|
+
"#<Left #{@v}>"
|
187
166
|
else
|
188
|
-
"#<Right
|
167
|
+
"#<Right #{@v}>"
|
189
168
|
end
|
190
169
|
end
|
191
|
-
|
170
|
+
alias_method :inspect, :to_s
|
192
171
|
class << self
|
193
172
|
# filter only {Right} value from List of {Either}
|
194
173
|
#
|
@@ -234,9 +213,6 @@ end
|
|
234
213
|
|
235
214
|
class Left
|
236
215
|
include Either
|
237
|
-
def initialize v=nil
|
238
|
-
@v=v
|
239
|
-
end
|
240
216
|
|
241
217
|
# always true
|
242
218
|
# @return [Boolean]
|
data/lib/union_type.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'helper'
|
2
|
+
module UnionType
|
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 what
|
14
|
+
current_class = self.class.to_s.to_sym
|
15
|
+
if what.include? current_class
|
16
|
+
what[current_class].(@v)
|
17
|
+
elsif what.include? :_
|
18
|
+
what[:_].(@v)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
alias_names [:match, :=~], :when
|
23
|
+
end
|
metadata
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: data.either
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jichao Ouyang
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-12-13 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description:
|
13
|
+
description: lightweight data.either Monad
|
14
14
|
email: oyanglulu@gmail.com
|
15
15
|
executables: []
|
16
16
|
extensions: []
|
@@ -19,6 +19,7 @@ files:
|
|
19
19
|
- lib/control/monad.rb
|
20
20
|
- lib/data.either.rb
|
21
21
|
- lib/helper.rb
|
22
|
+
- lib/union_type.rb
|
22
23
|
homepage: https://github.com/jcouyang/cats.rb
|
23
24
|
licenses:
|
24
25
|
- MIT
|
@@ -31,7 +32,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
31
32
|
requirements:
|
32
33
|
- - ">="
|
33
34
|
- !ruby/object:Gem::Version
|
34
|
-
version: 1.9.
|
35
|
+
version: 1.9.3
|
35
36
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
37
|
requirements:
|
37
38
|
- - ">="
|
@@ -42,5 +43,5 @@ rubyforge_project:
|
|
42
43
|
rubygems_version: 2.4.5.1
|
43
44
|
signing_key:
|
44
45
|
specification_version: 4
|
45
|
-
summary:
|
46
|
+
summary: data.either Data Type for Ruby
|
46
47
|
test_files: []
|