data.maybe 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8806e85d0ca8b9733e9bf574476aabf3eb61bba5
4
+ data.tar.gz: a0df6bfe5d96121adb1112b7db2ec0181e4f58ef
5
+ SHA512:
6
+ metadata.gz: 03c0be25899d188fc7ac9e18332094e07fe40c77970572d536ba7ddb113d01c560afb5f6661dae47ecea83afde65a571414e727bbd89c9f12b718af391f6a221
7
+ data.tar.gz: 536c7efbbf71487e5b189fc851df60d26bb78108100ca950335c616e027815f8ff48170c143ce5e8a044be4be5636e5ee79b3cab1f273eb9397578435c0242be
@@ -0,0 +1,25 @@
1
+ require 'helper'
2
+ module Control
3
+ module Functor
4
+ extend Helper
5
+ def map
6
+ raise 'map No defined'
7
+ end
8
+ alias_names [:fmap], :map
9
+ end
10
+
11
+ module Monad
12
+ extend Helper
13
+ include Functor
14
+ def flat_map
15
+ raise 'flat_map No defined'
16
+ end
17
+ alias :fmap :map
18
+ alias_names [:bind, :chain], :flat_map
19
+
20
+ def >> k
21
+ self.flat_map { |_| k }
22
+ end
23
+ end
24
+
25
+ end
data/lib/data.maybe.rb ADDED
@@ -0,0 +1,118 @@
1
+ require 'control/monad'
2
+ require 'singleton'
3
+ # The `Either` union type represents values with two possibilities:
4
+ #
5
+ # `Either a b` is either `Left a` or `Right b`
6
+ module Maybe
7
+ include Control::Monad
8
+
9
+ # Either only contain one value @v
10
+ # @return [Either]
11
+ def initialize v=nil
12
+ @v = v
13
+ end
14
+
15
+ # get value `a` out from `Right a`, otherwise return `e`
16
+ def get_or_else e
17
+ case self
18
+ when Just
19
+ @v
20
+ else
21
+ e
22
+ end
23
+ end
24
+
25
+ # overide of Functor's `fmap`, apply function on `Right a`'s value `a`, do nothing if it's `Left e`
26
+ #
27
+ # ``` ruby
28
+ # Right.new(1).map {|x| x + 1} # => #<Right value=2>
29
+ # Left.new(1).map {|x| x + 1} # => #<Left value=1>
30
+ # ```
31
+ # @return [Either]
32
+ def map
33
+ case self
34
+ when Just
35
+ Just.new(yield @v)
36
+ else
37
+ self
38
+ end
39
+ end
40
+
41
+ # it override {Monad#flat_map}, as Haskell's `>flat_map` method
42
+ # if it's {Right}, pass the value to #flat_map's block, and flat the result
43
+ # of the block.
44
+ #
45
+ # when it's {Left}, do nothing
46
+ # ``` ruby
47
+ # expect(Right.new(1).flat_map { |x| Left.new } ).to eq(Left.new)
48
+ # expect(Left.new(1).flat_map { |x| Left.new } ).to eq(Left.new(1))
49
+ # ```
50
+ # @return [Either]
51
+ def flat_map
52
+ case self
53
+ when Just
54
+ yield @v
55
+ else
56
+ self
57
+ end
58
+ end
59
+
60
+ # similar to Scala's `match` for case class
61
+ #
62
+ # will pattern match the value out and pass to matched lambda
63
+ #
64
+ # ``` ruby
65
+ # Right.new(1).when({Right: ->x{x+1} }) # => 2
66
+ # Right.new(1).when({Left: ->x{x+1}) # => nil
67
+ # Right.new(1).when({Left: ->x{x+1}, _: ->x{x-1} }) # => 0
68
+ # ```
69
+ # @return [Either]
70
+ def when what
71
+ current_class = self.class.to_s.to_sym
72
+ if what.include? current_class
73
+ what[current_class].(@v)
74
+ elsif what.include? :_
75
+ what[:_].(@v)
76
+ end
77
+ end
78
+
79
+ # @return [String]
80
+ def inspect
81
+ case self
82
+ when Just
83
+ "#<Just #{@v}>"
84
+ else
85
+ "#<Nothing>"
86
+ end
87
+ end
88
+ end
89
+
90
+
91
+ class Nothing
92
+ include Singleton
93
+ include Maybe
94
+
95
+ def == other
96
+ case other
97
+ when Nothing
98
+ true
99
+ else
100
+ false
101
+ end
102
+ end
103
+ end
104
+
105
+ Nothing = Nothing.instance
106
+
107
+ class Just
108
+ include Maybe
109
+
110
+ def == other
111
+ case other
112
+ when Just
113
+ other.map { |v| return v == @v }
114
+ else
115
+ false
116
+ end
117
+ end
118
+ end
data/lib/helper.rb ADDED
@@ -0,0 +1,9 @@
1
+ module Helper
2
+ def alias_names names, m
3
+ names.each do |name|
4
+ define_method(name) do |*args, &block|
5
+ self.send(m, *args, &block)
6
+ end
7
+ end
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: data.maybe
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-26 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: lightweight data.maybe Monad
14
+ email: oyanglulu@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/control/monad.rb
20
+ - lib/data.maybe.rb
21
+ - lib/helper.rb
22
+ homepage: https://github.com/jcouyang/cats.rb
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: 1.9.3
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.4.5
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: data.maybe Data Type for Ruby
46
+ test_files: []