Method: Fear::Either#get_or_else
- Defined in:
- lib/fear/either.rb
#get_or_else(&default) ⇒ any #get_or_else(default) ⇒ any
Returns the value from this Right or evaluates the given default argument if this is a Left.
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/fear/either.rb', line 246 module Either # @private def left_class Left end # @private def right_class Right end def initialize(value) @value = value end attr_reader :value protected :value # @param other [Any] # @return [Boolean] def ==(other) other.is_a?(self.class) && value == other.value end # @return [String] def inspect "#<#{self.class} value=#{value.inspect}>" end # @return [String] alias_method :to_s, :inspect # @return [<any>] def deconstruct [value] end # Projects this +Fear::Either+ as a +Fear::Left+. # This allows performing right-biased operation of the left # side of the +Fear::Either+. # # @example # Fear.left(42).left.map(&:succ) #=> Fear.left(43) # Fear.right(42).left.map(&:succ) #=> Fear.left(42) # # @return [Fear::LeftProjection] def left LeftProjection.new(self) end class << self # Build pattern matcher to be used later, despite off # +Either#match+ method, id doesn't apply matcher immanently, # but build it instead. Unusually in sake of efficiency it's better # to statically build matcher and reuse it later. # # @example # matcher = # Either.matcher do |m| # m.right(Integer, ->(x) { x > 2 }) { |x| x * 2 } # m.right(String) { |x| x.to_i * 2 } # m.left(String) { :err } # m.else { 'error '} # end # matcher.call(Fear.right(42)) # # @yieldparam [Fear::Either::PatternMatch] # @return [Fear::PartialFunction] def matcher(&matcher) Either::PatternMatch.new(&matcher) end end # Include this mixin to access convenient factory methods. # @example # include Fear::Either::Mixin # # Right('flower') #=> #<Fear::Right value='flower'> # Left('beaf') #=> #<Fear::Legt value='beaf'> # module Mixin # @param value [any] # @return [Fear::Left] # @example # Left(42) #=> #<Fear::Left value=42> # def Left(value) Fear.left(value) end # @param value [any] # @return [Fear::Right] # @example # Right(42) #=> #<Fear::Right value=42> # def Right(value) Fear.right(value) end end end |