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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8806e85d0ca8b9733e9bf574476aabf3eb61bba5
4
- data.tar.gz: a0df6bfe5d96121adb1112b7db2ec0181e4f58ef
3
+ metadata.gz: f07c4cb8a0e940c6fd3e3dbe4771efbc424c0290
4
+ data.tar.gz: 6ec1eb3e762f6fbf8485ff3e1648d269c208bae4
5
5
  SHA512:
6
- metadata.gz: 03c0be25899d188fc7ac9e18332094e07fe40c77970572d536ba7ddb113d01c560afb5f6661dae47ecea83afde65a571414e727bbd89c9f12b718af391f6a221
7
- data.tar.gz: 536c7efbbf71487e5b189fc851df60d26bb78108100ca950335c616e027815f8ff48170c143ce5e8a044be4be5636e5ee79b3cab1f273eb9397578435c0242be
6
+ metadata.gz: 280a236ab5079527c1ff06b5262be172be30a730d8ed862745a0458c5c1e2f4cf0814e769ee22a98061a7f92d838927d7872c67e495c6330e7ad4d7b3ff1381b
7
+ data.tar.gz: 2173f6cee5a8cf10b438a95d1dc6c64f7cc7c5c3f54f04b4144781ec30a0b60142578cc785668657ed222677c8af3cd4d0f2e18e5c4131de82589d66bed36db8
data/lib/control/monad.rb CHANGED
@@ -14,12 +14,11 @@ module Control
14
14
  def flat_map
15
15
  raise 'flat_map No defined'
16
16
  end
17
- alias :fmap :map
17
+
18
18
  alias_names [:bind, :chain], :flat_map
19
19
 
20
20
  def >> k
21
21
  self.flat_map { |_| k }
22
22
  end
23
23
  end
24
-
25
24
  end
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 inspect
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.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-08-26 00:00:00.000000000 Z
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