data.either 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/either.rb +95 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: dab81e54473d3fc3faab4f7aeffa4bb51f2c70b3
|
4
|
+
data.tar.gz: 975af75a0be0c918e982c902d41cf24ef468668b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4cbdc22b3a748a6668fafaaf10746185ba9ca1e9ea0df7021d258379eb4ce43c8b0741e5a727103be589c8a298805327b8cc022c4c32e74b1c3e7ba79ca198c6
|
7
|
+
data.tar.gz: 943ee4bdb6277cc9382af4cef7c4affc5545e99c4c417f7d24a2b7a49f4e1e4de72d0d94a1bef313181286bd71178395c50231b88e2afbe0273bc514cbb9836a
|
data/lib/either.rb
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
# a very simple Either
|
2
|
+
module Either
|
3
|
+
def initialize v
|
4
|
+
@v = v
|
5
|
+
end
|
6
|
+
|
7
|
+
def get_or_else e
|
8
|
+
case self
|
9
|
+
when Right
|
10
|
+
@v
|
11
|
+
else
|
12
|
+
e
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# Functor
|
17
|
+
def map
|
18
|
+
case self
|
19
|
+
when Right
|
20
|
+
Right.new(yield @v)
|
21
|
+
else
|
22
|
+
self
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
alias :fmap :map
|
27
|
+
|
28
|
+
def left_map
|
29
|
+
case self
|
30
|
+
when Left
|
31
|
+
Left.new(yield @v)
|
32
|
+
else
|
33
|
+
self
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def bimap lfn, rfn
|
38
|
+
case self
|
39
|
+
when Right
|
40
|
+
Right.new(rfn.(@v))
|
41
|
+
else
|
42
|
+
Left.new(lfn.(@v))
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# Monad
|
47
|
+
def bind
|
48
|
+
case self
|
49
|
+
when Right
|
50
|
+
yield @v
|
51
|
+
else
|
52
|
+
self
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
alias :chain :bind
|
57
|
+
alias :flat_map :bind
|
58
|
+
|
59
|
+
def inspect
|
60
|
+
case self
|
61
|
+
when Left
|
62
|
+
"#<Left value=#{@v}>"
|
63
|
+
else
|
64
|
+
"#<Right value=#{@v}>"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
class Left
|
70
|
+
include Either
|
71
|
+
def initialize v=nil
|
72
|
+
@v=v
|
73
|
+
end
|
74
|
+
|
75
|
+
def == other
|
76
|
+
case other
|
77
|
+
when Left
|
78
|
+
other.left_map { |v| return v == @v }
|
79
|
+
else
|
80
|
+
false
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
class Right
|
86
|
+
include Either
|
87
|
+
def == other
|
88
|
+
case other
|
89
|
+
when Right
|
90
|
+
other.map { |v| return v == @v }
|
91
|
+
else
|
92
|
+
false
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: data.either
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jichao Ouyang
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-08-18 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: The Either Monad
|
14
|
+
email: oyanglulu@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/either.rb
|
20
|
+
homepage: https://github.com/jcouyang/cats.rb
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.4.5
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: Either Data Type for Ruby
|
44
|
+
test_files: []
|