Module: Fear::Option
- Included in:
- Some
- Defined in:
- lib/fear/option.rb,
lib/fear/option/pattern_match.rb
Overview
Represents optional values. Instances of Option are either an instance of Some or the object None.
This allows for sophisticated chaining of Option values without having to check for the existence of a value.
Defined Under Namespace
Modules: Mixin Classes: PatternMatch
Class Method Summary collapse
- .match(value, &block) ⇒ Object
-
.matcher {|| ... } ⇒ Fear::PartialFunction
Build pattern matcher to be used later, despite off Option#match method, it doesn’t apply matcher immanently, but build it instead.
Instance Method Summary collapse
-
#any? {|value| ... } ⇒ Boolean
Returns
falseifNoneor returns the result of the application of the given predicate to theSomevalue. -
#each {|value| ... } ⇒ Option
Performs the given block if this is a
Some. -
#empty? ⇒ Boolean
Returns
trueif theOptionisNone,falseotherwise. -
#filter_map {|value| ... } ⇒ Object
Returns a new
Someof truthy results (everything exceptfalseornil) of running the block orNoneotherwise. -
#flat_map {|value| ... } ⇒ Option
Returns the given block applied to the value from this
Someor returns this if this is aNone. -
#get ⇒ any
The
Option‘s value. -
#get_or_else(*args) ⇒ Object
Returns the value from this
Someor evaluates the given default argument if this is aNone. -
#include?(other_value) ⇒ Boolean
Returns
trueif it has an element that is equal (as determined by ==) toother_value,falseotherwise. -
#map {|value| ... } ⇒ Object
Maps the given block to the value from this
Someor returns this if this is aNone. -
#match(&matcher) ⇒ Object
Pattern match against this
Option. -
#or_else(&alternative) ⇒ Option
Returns this
Someor the given alternative if this is aNone. -
#reject {|value| ... } ⇒ Option
Returns
Someif applying the predicate to thisOption‘s value returnsfalse. -
#select {|value| ... } ⇒ Option
Returns self if it is nonempty and applying the predicate to this
Option‘s value returnstrue. -
#zip(other) ⇒ Fear::Option
A
Fear::Someformed from this option and another option by combining the corresponding elements in an array.
Class Method Details
.match(value, &block) ⇒ Object
213 214 215 |
# File 'lib/fear/option.rb', line 213 def match(value, &block) matcher(&block).call(value) end |
.matcher {|| ... } ⇒ Fear::PartialFunction
Build pattern matcher to be used later, despite off Option#match method, it doesn’t apply matcher immanently, but build it instead. Usually in sake of efficiency it’s better to statically build matcher and reuse it later.
209 210 211 |
# File 'lib/fear/option.rb', line 209 def matcher(&matcher) Option::PatternMatch.new(&matcher) end |
Instance Method Details
#any? {|value| ... } ⇒ Boolean
Returns false if None or returns the result of the application of the given predicate to the Some value.
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/fear/option.rb', line 180 module Option # @private def left_class NoneClass end # @private def right_class Some end class << self # Build pattern matcher to be used later, despite off # +Option#match+ method, it doesn't apply matcher immanently, # but build it instead. Usually in sake of efficiency it's better # to statically build matcher and reuse it later. # # @example # matcher = # Option.matcher do |m| # m.some(Integer) { |x| x * 2 } # m.some(String) { |x| x.to_i * 2 } # m.none { 'NaN' } # m.else { 'error '} # end # matcher.call(Fear.some(42)) # # @yieldparam [OptionPatternMatch] # @return [Fear::PartialFunction] def matcher(&matcher) Option::PatternMatch.new(&matcher) end def match(value, &block) matcher(&block).call(value) end end # Include this mixin to access convenient factory methods. # @example # include Fear::Option::Mixin # # Option(17) #=> #<Fear::Some get=17> # Option(nil) #=> #<Fear::None> # Some(17) #=> #<Fear::Some get=17> # None() #=> #<Fear::None> # module Mixin # An +Option+ factory which creates +Some+ if the argument is # not +nil+, and +None+ if it is +nil+. # @param value [any] # @return [Fear::Some, Fear::None] # # @example # Option(17) #=> #<Fear::Some get=17> # Option(nil) #=> #<Fear::None> # def Option(value) Fear.option(value) end # @return [None] # @example # None() #=> #<Fear::None> # def None Fear.none end # @param value [any] except nil # @return [Fear::Some] # @example # Some(17) #=> #<Fear::Some get=17> # Some(nil) #=> #<Fear::Some get=nil> # def Some(value) Fear.some(value) end end end |
#each {|value| ... } ⇒ Option
Performs the given block if this is a Some.
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/fear/option.rb', line 180 module Option # @private def left_class NoneClass end # @private def right_class Some end class << self # Build pattern matcher to be used later, despite off # +Option#match+ method, it doesn't apply matcher immanently, # but build it instead. Usually in sake of efficiency it's better # to statically build matcher and reuse it later. # # @example # matcher = # Option.matcher do |m| # m.some(Integer) { |x| x * 2 } # m.some(String) { |x| x.to_i * 2 } # m.none { 'NaN' } # m.else { 'error '} # end # matcher.call(Fear.some(42)) # # @yieldparam [OptionPatternMatch] # @return [Fear::PartialFunction] def matcher(&matcher) Option::PatternMatch.new(&matcher) end def match(value, &block) matcher(&block).call(value) end end # Include this mixin to access convenient factory methods. # @example # include Fear::Option::Mixin # # Option(17) #=> #<Fear::Some get=17> # Option(nil) #=> #<Fear::None> # Some(17) #=> #<Fear::Some get=17> # None() #=> #<Fear::None> # module Mixin # An +Option+ factory which creates +Some+ if the argument is # not +nil+, and +None+ if it is +nil+. # @param value [any] # @return [Fear::Some, Fear::None] # # @example # Option(17) #=> #<Fear::Some get=17> # Option(nil) #=> #<Fear::None> # def Option(value) Fear.option(value) end # @return [None] # @example # None() #=> #<Fear::None> # def None Fear.none end # @param value [any] except nil # @return [Fear::Some] # @example # Some(17) #=> #<Fear::Some get=17> # Some(nil) #=> #<Fear::Some get=nil> # def Some(value) Fear.some(value) end end end |
#empty? ⇒ Boolean
Returns true if the Option is None, false otherwise.
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/fear/option.rb', line 180 module Option # @private def left_class NoneClass end # @private def right_class Some end class << self # Build pattern matcher to be used later, despite off # +Option#match+ method, it doesn't apply matcher immanently, # but build it instead. Usually in sake of efficiency it's better # to statically build matcher and reuse it later. # # @example # matcher = # Option.matcher do |m| # m.some(Integer) { |x| x * 2 } # m.some(String) { |x| x.to_i * 2 } # m.none { 'NaN' } # m.else { 'error '} # end # matcher.call(Fear.some(42)) # # @yieldparam [OptionPatternMatch] # @return [Fear::PartialFunction] def matcher(&matcher) Option::PatternMatch.new(&matcher) end def match(value, &block) matcher(&block).call(value) end end # Include this mixin to access convenient factory methods. # @example # include Fear::Option::Mixin # # Option(17) #=> #<Fear::Some get=17> # Option(nil) #=> #<Fear::None> # Some(17) #=> #<Fear::Some get=17> # None() #=> #<Fear::None> # module Mixin # An +Option+ factory which creates +Some+ if the argument is # not +nil+, and +None+ if it is +nil+. # @param value [any] # @return [Fear::Some, Fear::None] # # @example # Option(17) #=> #<Fear::Some get=17> # Option(nil) #=> #<Fear::None> # def Option(value) Fear.option(value) end # @return [None] # @example # None() #=> #<Fear::None> # def None Fear.none end # @param value [any] except nil # @return [Fear::Some] # @example # Some(17) #=> #<Fear::Some get=17> # Some(nil) #=> #<Fear::Some get=nil> # def Some(value) Fear.some(value) end end end |
#filter_map {|value| ... } ⇒ Object
Returns a new Some of truthy results (everything except false or nil) of running the block or None otherwise.
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/fear/option.rb', line 180 module Option # @private def left_class NoneClass end # @private def right_class Some end class << self # Build pattern matcher to be used later, despite off # +Option#match+ method, it doesn't apply matcher immanently, # but build it instead. Usually in sake of efficiency it's better # to statically build matcher and reuse it later. # # @example # matcher = # Option.matcher do |m| # m.some(Integer) { |x| x * 2 } # m.some(String) { |x| x.to_i * 2 } # m.none { 'NaN' } # m.else { 'error '} # end # matcher.call(Fear.some(42)) # # @yieldparam [OptionPatternMatch] # @return [Fear::PartialFunction] def matcher(&matcher) Option::PatternMatch.new(&matcher) end def match(value, &block) matcher(&block).call(value) end end # Include this mixin to access convenient factory methods. # @example # include Fear::Option::Mixin # # Option(17) #=> #<Fear::Some get=17> # Option(nil) #=> #<Fear::None> # Some(17) #=> #<Fear::Some get=17> # None() #=> #<Fear::None> # module Mixin # An +Option+ factory which creates +Some+ if the argument is # not +nil+, and +None+ if it is +nil+. # @param value [any] # @return [Fear::Some, Fear::None] # # @example # Option(17) #=> #<Fear::Some get=17> # Option(nil) #=> #<Fear::None> # def Option(value) Fear.option(value) end # @return [None] # @example # None() #=> #<Fear::None> # def None Fear.none end # @param value [any] except nil # @return [Fear::Some] # @example # Some(17) #=> #<Fear::Some get=17> # Some(nil) #=> #<Fear::Some get=nil> # def Some(value) Fear.some(value) end end end |
#flat_map {|value| ... } ⇒ Option
Returns the given block applied to the value from this Some or returns this if this is a None
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/fear/option.rb', line 180 module Option # @private def left_class NoneClass end # @private def right_class Some end class << self # Build pattern matcher to be used later, despite off # +Option#match+ method, it doesn't apply matcher immanently, # but build it instead. Usually in sake of efficiency it's better # to statically build matcher and reuse it later. # # @example # matcher = # Option.matcher do |m| # m.some(Integer) { |x| x * 2 } # m.some(String) { |x| x.to_i * 2 } # m.none { 'NaN' } # m.else { 'error '} # end # matcher.call(Fear.some(42)) # # @yieldparam [OptionPatternMatch] # @return [Fear::PartialFunction] def matcher(&matcher) Option::PatternMatch.new(&matcher) end def match(value, &block) matcher(&block).call(value) end end # Include this mixin to access convenient factory methods. # @example # include Fear::Option::Mixin # # Option(17) #=> #<Fear::Some get=17> # Option(nil) #=> #<Fear::None> # Some(17) #=> #<Fear::Some get=17> # None() #=> #<Fear::None> # module Mixin # An +Option+ factory which creates +Some+ if the argument is # not +nil+, and +None+ if it is +nil+. # @param value [any] # @return [Fear::Some, Fear::None] # # @example # Option(17) #=> #<Fear::Some get=17> # Option(nil) #=> #<Fear::None> # def Option(value) Fear.option(value) end # @return [None] # @example # None() #=> #<Fear::None> # def None Fear.none end # @param value [any] except nil # @return [Fear::Some] # @example # Some(17) #=> #<Fear::Some get=17> # Some(nil) #=> #<Fear::Some get=nil> # def Some(value) Fear.some(value) end end end |
#get ⇒ any
Returns the Option‘s value.
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/fear/option.rb', line 180 module Option # @private def left_class NoneClass end # @private def right_class Some end class << self # Build pattern matcher to be used later, despite off # +Option#match+ method, it doesn't apply matcher immanently, # but build it instead. Usually in sake of efficiency it's better # to statically build matcher and reuse it later. # # @example # matcher = # Option.matcher do |m| # m.some(Integer) { |x| x * 2 } # m.some(String) { |x| x.to_i * 2 } # m.none { 'NaN' } # m.else { 'error '} # end # matcher.call(Fear.some(42)) # # @yieldparam [OptionPatternMatch] # @return [Fear::PartialFunction] def matcher(&matcher) Option::PatternMatch.new(&matcher) end def match(value, &block) matcher(&block).call(value) end end # Include this mixin to access convenient factory methods. # @example # include Fear::Option::Mixin # # Option(17) #=> #<Fear::Some get=17> # Option(nil) #=> #<Fear::None> # Some(17) #=> #<Fear::Some get=17> # None() #=> #<Fear::None> # module Mixin # An +Option+ factory which creates +Some+ if the argument is # not +nil+, and +None+ if it is +nil+. # @param value [any] # @return [Fear::Some, Fear::None] # # @example # Option(17) #=> #<Fear::Some get=17> # Option(nil) #=> #<Fear::None> # def Option(value) Fear.option(value) end # @return [None] # @example # None() #=> #<Fear::None> # def None Fear.none end # @param value [any] except nil # @return [Fear::Some] # @example # Some(17) #=> #<Fear::Some get=17> # Some(nil) #=> #<Fear::Some get=nil> # def Some(value) Fear.some(value) end end end |
#get_or_else(&default) ⇒ any #get_or_else(default) ⇒ any
Returns the value from this Some or evaluates the given default argument if this is a None.
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/fear/option.rb', line 180 module Option # @private def left_class NoneClass end # @private def right_class Some end class << self # Build pattern matcher to be used later, despite off # +Option#match+ method, it doesn't apply matcher immanently, # but build it instead. Usually in sake of efficiency it's better # to statically build matcher and reuse it later. # # @example # matcher = # Option.matcher do |m| # m.some(Integer) { |x| x * 2 } # m.some(String) { |x| x.to_i * 2 } # m.none { 'NaN' } # m.else { 'error '} # end # matcher.call(Fear.some(42)) # # @yieldparam [OptionPatternMatch] # @return [Fear::PartialFunction] def matcher(&matcher) Option::PatternMatch.new(&matcher) end def match(value, &block) matcher(&block).call(value) end end # Include this mixin to access convenient factory methods. # @example # include Fear::Option::Mixin # # Option(17) #=> #<Fear::Some get=17> # Option(nil) #=> #<Fear::None> # Some(17) #=> #<Fear::Some get=17> # None() #=> #<Fear::None> # module Mixin # An +Option+ factory which creates +Some+ if the argument is # not +nil+, and +None+ if it is +nil+. # @param value [any] # @return [Fear::Some, Fear::None] # # @example # Option(17) #=> #<Fear::Some get=17> # Option(nil) #=> #<Fear::None> # def Option(value) Fear.option(value) end # @return [None] # @example # None() #=> #<Fear::None> # def None Fear.none end # @param value [any] except nil # @return [Fear::Some] # @example # Some(17) #=> #<Fear::Some get=17> # Some(nil) #=> #<Fear::Some get=nil> # def Some(value) Fear.some(value) end end end |
#include?(other_value) ⇒ Boolean
Returns true if it has an element that is equal (as determined by ==) to other_value, false otherwise.
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/fear/option.rb', line 180 module Option # @private def left_class NoneClass end # @private def right_class Some end class << self # Build pattern matcher to be used later, despite off # +Option#match+ method, it doesn't apply matcher immanently, # but build it instead. Usually in sake of efficiency it's better # to statically build matcher and reuse it later. # # @example # matcher = # Option.matcher do |m| # m.some(Integer) { |x| x * 2 } # m.some(String) { |x| x.to_i * 2 } # m.none { 'NaN' } # m.else { 'error '} # end # matcher.call(Fear.some(42)) # # @yieldparam [OptionPatternMatch] # @return [Fear::PartialFunction] def matcher(&matcher) Option::PatternMatch.new(&matcher) end def match(value, &block) matcher(&block).call(value) end end # Include this mixin to access convenient factory methods. # @example # include Fear::Option::Mixin # # Option(17) #=> #<Fear::Some get=17> # Option(nil) #=> #<Fear::None> # Some(17) #=> #<Fear::Some get=17> # None() #=> #<Fear::None> # module Mixin # An +Option+ factory which creates +Some+ if the argument is # not +nil+, and +None+ if it is +nil+. # @param value [any] # @return [Fear::Some, Fear::None] # # @example # Option(17) #=> #<Fear::Some get=17> # Option(nil) #=> #<Fear::None> # def Option(value) Fear.option(value) end # @return [None] # @example # None() #=> #<Fear::None> # def None Fear.none end # @param value [any] except nil # @return [Fear::Some] # @example # Some(17) #=> #<Fear::Some get=17> # Some(nil) #=> #<Fear::Some get=nil> # def Some(value) Fear.some(value) end end end |
#map {|value| ... } ⇒ Object
Maps the given block to the value from this Some or returns this if this is a None
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/fear/option.rb', line 180 module Option # @private def left_class NoneClass end # @private def right_class Some end class << self # Build pattern matcher to be used later, despite off # +Option#match+ method, it doesn't apply matcher immanently, # but build it instead. Usually in sake of efficiency it's better # to statically build matcher and reuse it later. # # @example # matcher = # Option.matcher do |m| # m.some(Integer) { |x| x * 2 } # m.some(String) { |x| x.to_i * 2 } # m.none { 'NaN' } # m.else { 'error '} # end # matcher.call(Fear.some(42)) # # @yieldparam [OptionPatternMatch] # @return [Fear::PartialFunction] def matcher(&matcher) Option::PatternMatch.new(&matcher) end def match(value, &block) matcher(&block).call(value) end end # Include this mixin to access convenient factory methods. # @example # include Fear::Option::Mixin # # Option(17) #=> #<Fear::Some get=17> # Option(nil) #=> #<Fear::None> # Some(17) #=> #<Fear::Some get=17> # None() #=> #<Fear::None> # module Mixin # An +Option+ factory which creates +Some+ if the argument is # not +nil+, and +None+ if it is +nil+. # @param value [any] # @return [Fear::Some, Fear::None] # # @example # Option(17) #=> #<Fear::Some get=17> # Option(nil) #=> #<Fear::None> # def Option(value) Fear.option(value) end # @return [None] # @example # None() #=> #<Fear::None> # def None Fear.none end # @param value [any] except nil # @return [Fear::Some] # @example # Some(17) #=> #<Fear::Some get=17> # Some(nil) #=> #<Fear::Some get=nil> # def Some(value) Fear.some(value) end end end |
#match(&matcher) ⇒ Object
Pattern match against this Option
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/fear/option.rb', line 180 module Option # @private def left_class NoneClass end # @private def right_class Some end class << self # Build pattern matcher to be used later, despite off # +Option#match+ method, it doesn't apply matcher immanently, # but build it instead. Usually in sake of efficiency it's better # to statically build matcher and reuse it later. # # @example # matcher = # Option.matcher do |m| # m.some(Integer) { |x| x * 2 } # m.some(String) { |x| x.to_i * 2 } # m.none { 'NaN' } # m.else { 'error '} # end # matcher.call(Fear.some(42)) # # @yieldparam [OptionPatternMatch] # @return [Fear::PartialFunction] def matcher(&matcher) Option::PatternMatch.new(&matcher) end def match(value, &block) matcher(&block).call(value) end end # Include this mixin to access convenient factory methods. # @example # include Fear::Option::Mixin # # Option(17) #=> #<Fear::Some get=17> # Option(nil) #=> #<Fear::None> # Some(17) #=> #<Fear::Some get=17> # None() #=> #<Fear::None> # module Mixin # An +Option+ factory which creates +Some+ if the argument is # not +nil+, and +None+ if it is +nil+. # @param value [any] # @return [Fear::Some, Fear::None] # # @example # Option(17) #=> #<Fear::Some get=17> # Option(nil) #=> #<Fear::None> # def Option(value) Fear.option(value) end # @return [None] # @example # None() #=> #<Fear::None> # def None Fear.none end # @param value [any] except nil # @return [Fear::Some] # @example # Some(17) #=> #<Fear::Some get=17> # Some(nil) #=> #<Fear::Some get=nil> # def Some(value) Fear.some(value) end end end |
#or_else(&alternative) ⇒ Option
Returns this Some or the given alternative if this is a None.
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/fear/option.rb', line 180 module Option # @private def left_class NoneClass end # @private def right_class Some end class << self # Build pattern matcher to be used later, despite off # +Option#match+ method, it doesn't apply matcher immanently, # but build it instead. Usually in sake of efficiency it's better # to statically build matcher and reuse it later. # # @example # matcher = # Option.matcher do |m| # m.some(Integer) { |x| x * 2 } # m.some(String) { |x| x.to_i * 2 } # m.none { 'NaN' } # m.else { 'error '} # end # matcher.call(Fear.some(42)) # # @yieldparam [OptionPatternMatch] # @return [Fear::PartialFunction] def matcher(&matcher) Option::PatternMatch.new(&matcher) end def match(value, &block) matcher(&block).call(value) end end # Include this mixin to access convenient factory methods. # @example # include Fear::Option::Mixin # # Option(17) #=> #<Fear::Some get=17> # Option(nil) #=> #<Fear::None> # Some(17) #=> #<Fear::Some get=17> # None() #=> #<Fear::None> # module Mixin # An +Option+ factory which creates +Some+ if the argument is # not +nil+, and +None+ if it is +nil+. # @param value [any] # @return [Fear::Some, Fear::None] # # @example # Option(17) #=> #<Fear::Some get=17> # Option(nil) #=> #<Fear::None> # def Option(value) Fear.option(value) end # @return [None] # @example # None() #=> #<Fear::None> # def None Fear.none end # @param value [any] except nil # @return [Fear::Some] # @example # Some(17) #=> #<Fear::Some get=17> # Some(nil) #=> #<Fear::Some get=nil> # def Some(value) Fear.some(value) end end end |
#reject {|value| ... } ⇒ Option
Returns Some if applying the predicate to this Option‘s value returns false. Otherwise, return None.
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/fear/option.rb', line 180 module Option # @private def left_class NoneClass end # @private def right_class Some end class << self # Build pattern matcher to be used later, despite off # +Option#match+ method, it doesn't apply matcher immanently, # but build it instead. Usually in sake of efficiency it's better # to statically build matcher and reuse it later. # # @example # matcher = # Option.matcher do |m| # m.some(Integer) { |x| x * 2 } # m.some(String) { |x| x.to_i * 2 } # m.none { 'NaN' } # m.else { 'error '} # end # matcher.call(Fear.some(42)) # # @yieldparam [OptionPatternMatch] # @return [Fear::PartialFunction] def matcher(&matcher) Option::PatternMatch.new(&matcher) end def match(value, &block) matcher(&block).call(value) end end # Include this mixin to access convenient factory methods. # @example # include Fear::Option::Mixin # # Option(17) #=> #<Fear::Some get=17> # Option(nil) #=> #<Fear::None> # Some(17) #=> #<Fear::Some get=17> # None() #=> #<Fear::None> # module Mixin # An +Option+ factory which creates +Some+ if the argument is # not +nil+, and +None+ if it is +nil+. # @param value [any] # @return [Fear::Some, Fear::None] # # @example # Option(17) #=> #<Fear::Some get=17> # Option(nil) #=> #<Fear::None> # def Option(value) Fear.option(value) end # @return [None] # @example # None() #=> #<Fear::None> # def None Fear.none end # @param value [any] except nil # @return [Fear::Some] # @example # Some(17) #=> #<Fear::Some get=17> # Some(nil) #=> #<Fear::Some get=nil> # def Some(value) Fear.some(value) end end end |
#select {|value| ... } ⇒ Option
Returns self if it is nonempty and applying the predicate to this Option‘s value returns true. Otherwise, return None.
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/fear/option.rb', line 180 module Option # @private def left_class NoneClass end # @private def right_class Some end class << self # Build pattern matcher to be used later, despite off # +Option#match+ method, it doesn't apply matcher immanently, # but build it instead. Usually in sake of efficiency it's better # to statically build matcher and reuse it later. # # @example # matcher = # Option.matcher do |m| # m.some(Integer) { |x| x * 2 } # m.some(String) { |x| x.to_i * 2 } # m.none { 'NaN' } # m.else { 'error '} # end # matcher.call(Fear.some(42)) # # @yieldparam [OptionPatternMatch] # @return [Fear::PartialFunction] def matcher(&matcher) Option::PatternMatch.new(&matcher) end def match(value, &block) matcher(&block).call(value) end end # Include this mixin to access convenient factory methods. # @example # include Fear::Option::Mixin # # Option(17) #=> #<Fear::Some get=17> # Option(nil) #=> #<Fear::None> # Some(17) #=> #<Fear::Some get=17> # None() #=> #<Fear::None> # module Mixin # An +Option+ factory which creates +Some+ if the argument is # not +nil+, and +None+ if it is +nil+. # @param value [any] # @return [Fear::Some, Fear::None] # # @example # Option(17) #=> #<Fear::Some get=17> # Option(nil) #=> #<Fear::None> # def Option(value) Fear.option(value) end # @return [None] # @example # None() #=> #<Fear::None> # def None Fear.none end # @param value [any] except nil # @return [Fear::Some] # @example # Some(17) #=> #<Fear::Some get=17> # Some(nil) #=> #<Fear::Some get=nil> # def Some(value) Fear.some(value) end end end |
#zip(other) ⇒ Fear::Option
Returns a Fear::Some formed from this option and another option by combining the corresponding elements in an array.
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/fear/option.rb', line 180 module Option # @private def left_class NoneClass end # @private def right_class Some end class << self # Build pattern matcher to be used later, despite off # +Option#match+ method, it doesn't apply matcher immanently, # but build it instead. Usually in sake of efficiency it's better # to statically build matcher and reuse it later. # # @example # matcher = # Option.matcher do |m| # m.some(Integer) { |x| x * 2 } # m.some(String) { |x| x.to_i * 2 } # m.none { 'NaN' } # m.else { 'error '} # end # matcher.call(Fear.some(42)) # # @yieldparam [OptionPatternMatch] # @return [Fear::PartialFunction] def matcher(&matcher) Option::PatternMatch.new(&matcher) end def match(value, &block) matcher(&block).call(value) end end # Include this mixin to access convenient factory methods. # @example # include Fear::Option::Mixin # # Option(17) #=> #<Fear::Some get=17> # Option(nil) #=> #<Fear::None> # Some(17) #=> #<Fear::Some get=17> # None() #=> #<Fear::None> # module Mixin # An +Option+ factory which creates +Some+ if the argument is # not +nil+, and +None+ if it is +nil+. # @param value [any] # @return [Fear::Some, Fear::None] # # @example # Option(17) #=> #<Fear::Some get=17> # Option(nil) #=> #<Fear::None> # def Option(value) Fear.option(value) end # @return [None] # @example # None() #=> #<Fear::None> # def None Fear.none end # @param value [any] except nil # @return [Fear::Some] # @example # Some(17) #=> #<Fear::Some get=17> # Some(nil) #=> #<Fear::Some get=nil> # def Some(value) Fear.some(value) end end end |