data.maybe 0.0.0 → 0.0.11
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 +1 -2
- data/lib/data.maybe.rb +4 -2
- data/lib/union_type.rb +23 -0
- metadata +4 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f07c4cb8a0e940c6fd3e3dbe4771efbc424c0290
|
|
4
|
+
data.tar.gz: 6ec1eb3e762f6fbf8485ff3e1648d269c208bae4
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 280a236ab5079527c1ff06b5262be172be30a730d8ed862745a0458c5c1e2f4cf0814e769ee22a98061a7f92d838927d7872c67e495c6330e7ad4d7b3ff1381b
|
|
7
|
+
data.tar.gz: 2173f6cee5a8cf10b438a95d1dc6c64f7cc7c5c3f54f04b4144781ec30a0b60142578cc785668657ed222677c8af3cd4d0f2e18e5c4131de82589d66bed36db8
|
data/lib/control/monad.rb
CHANGED
data/lib/data.maybe.rb
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
require 'control/monad'
|
|
2
2
|
require 'singleton'
|
|
3
|
+
require 'union_type'
|
|
3
4
|
# The `Either` union type represents values with two possibilities:
|
|
4
5
|
#
|
|
5
6
|
# `Either a b` is either `Left a` or `Right b`
|
|
6
7
|
module Maybe
|
|
7
8
|
include Control::Monad
|
|
8
|
-
|
|
9
|
+
include UnionType
|
|
9
10
|
# Either only contain one value @v
|
|
10
11
|
# @return [Either]
|
|
11
12
|
def initialize v=nil
|
|
@@ -77,7 +78,7 @@ module Maybe
|
|
|
77
78
|
end
|
|
78
79
|
|
|
79
80
|
# @return [String]
|
|
80
|
-
def
|
|
81
|
+
def to_s
|
|
81
82
|
case self
|
|
82
83
|
when Just
|
|
83
84
|
"#<Just #{@v}>"
|
|
@@ -85,6 +86,7 @@ module Maybe
|
|
|
85
86
|
"#<Nothing>"
|
|
86
87
|
end
|
|
87
88
|
end
|
|
89
|
+
alias_method :inspect, :to_s
|
|
88
90
|
end
|
|
89
91
|
|
|
90
92
|
|
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,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: data.maybe
|
|
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
13
|
description: lightweight data.maybe Monad
|
|
14
14
|
email: oyanglulu@gmail.com
|
|
@@ -19,6 +19,7 @@ files:
|
|
|
19
19
|
- lib/control/monad.rb
|
|
20
20
|
- lib/data.maybe.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
|
|
@@ -39,7 +40,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
39
40
|
version: '0'
|
|
40
41
|
requirements: []
|
|
41
42
|
rubyforge_project:
|
|
42
|
-
rubygems_version: 2.4.5
|
|
43
|
+
rubygems_version: 2.4.5.1
|
|
43
44
|
signing_key:
|
|
44
45
|
specification_version: 4
|
|
45
46
|
summary: data.maybe Data Type for Ruby
|