indubitably 0.3.0 → 0.4.0

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.
Files changed (2) hide show
  1. data/lib/indubitably.rb +45 -7
  2. metadata +3 -4
data/lib/indubitably.rb CHANGED
@@ -1,4 +1,8 @@
1
+ # encoding: utf-8
2
+ # base class for Some and None
1
3
  class Maybe
4
+ @@none = nil
5
+
2
6
  ([:each] + Enumerable.instance_methods).each do |enumerable_method|
3
7
  define_method(enumerable_method) do |*args, &block|
4
8
  res = __enumerable_value.send(enumerable_method, *args, &block)
@@ -16,9 +20,30 @@ class Maybe
16
20
  end
17
21
  alias_method :eql?, :==
18
22
 
23
+ def self.concat(list, default = none)
24
+ if default == none
25
+ list.select(&:is_some?).map(&:get)
26
+ else
27
+ list.map { |x| x.or_else(default) }
28
+ end
29
+ end
30
+
19
31
  def self.empty_value?(value)
20
32
  value.nil? || (value.respond_to?(:length) && value.length == 0)
21
33
  end
34
+
35
+ def self.join?(value)
36
+ return value if value.is_a?(Maybe)
37
+ Maybe(value)
38
+ end
39
+
40
+ def self.none
41
+ @@none ||= None.new
42
+ end
43
+
44
+ def self.seq(list, default = none)
45
+ Maybe(concat(list, default))
46
+ end
22
47
  end
23
48
 
24
49
  # Represents a non-empty value
@@ -31,6 +56,10 @@ class Some < Maybe
31
56
  @value
32
57
  end
33
58
 
59
+ def if(&blk)
60
+ blk.call(@value) ? self : Maybe.none
61
+ end
62
+
34
63
  def or_else(*)
35
64
  @value
36
65
  end
@@ -68,16 +97,21 @@ class Some < Maybe
68
97
  other && other.class == self.class && @value === other.get
69
98
  end
70
99
 
100
+ # This strips the leading underscore if the method is sent with that prefix;
101
+ # this allows us to force dispatch to the contained object. It is inline in
102
+ # two places because this avoids a function call performance hit.
71
103
  def method_missing(method_sym, *args, &block)
72
- if method_sym[0] == '_'
73
- method_sym = method_sym.slice(1, method_sym.length)
74
- end
75
-
104
+ method_sym = method_sym.slice(1, method_sym.length) if method_sym[0] == "_"
76
105
  map { |value| value.send(method_sym, *args, &block) }
77
106
  end
78
107
 
79
108
  private
80
109
 
110
+ def respond_to_missing?(method_sym, include_private = false)
111
+ method_sym = method_sym.slice(1, method_sym.length) if method_sym[0] == "_"
112
+ @value.respond_to?(method_sym, include_private) || super
113
+ end
114
+
81
115
  def __enumerable_value
82
116
  [@value]
83
117
  end
@@ -86,7 +120,7 @@ end
86
120
  # Represents an empty value
87
121
  class None < Maybe
88
122
  def get
89
- fail 'No such element'
123
+ fail "No such element"
90
124
  end
91
125
 
92
126
  def or_else(els = nil)
@@ -107,6 +141,10 @@ class None < Maybe
107
141
  self
108
142
  end
109
143
 
144
+ def respond_to_missing?(*)
145
+ true
146
+ end
147
+
110
148
  private
111
149
 
112
150
  def __enumerable_value
@@ -116,7 +154,7 @@ end
116
154
 
117
155
  # rubocop:disable MethodName
118
156
  def Maybe(value)
119
- Maybe.empty_value?(value) ? None() : Some.new(value)
157
+ Maybe.empty_value?(value) ? Maybe.none : Some.new(value)
120
158
  end
121
159
 
122
160
  def Some(value)
@@ -124,6 +162,6 @@ def Some(value)
124
162
  end
125
163
 
126
164
  def None
127
- None.new
165
+ Maybe.none
128
166
  end
129
167
  # rubocop:enable MethodName
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: indubitably
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,10 +10,9 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2015-01-30 00:00:00.000000000 Z
13
+ date: 2015-02-13 00:00:00.000000000 Z
14
14
  dependencies: []
15
- description: Maybe monad implementation for Ruby (some might call it Option pattern
16
- or Null pattern)
15
+ description: An implementation of Maybe/Option for Ruby.
17
16
  email: syrion@gmail.com
18
17
  executables: []
19
18
  extensions: []