Module: Fear::Either
Overview
Represents a value of one of two possible types (a disjoint union.) An instance of Either is either an instance of Left or Right.
A common use of Either is as an alternative to Option for dealing with possible missing values. In this usage, None is replaced with a Left which can contain useful information. Right takes the place of Some. Convention dictates that Left is used for failure and Right is used for Right.
For example, you could use Either<String, Fixnum> to select_or_else whether a received input is a String or an Fixnum.
Either is right-biased, which means that Right is assumed to be the default case to operate on. If it is Left, operations like #map, #flat_map, … return the Left value unchanged:
Defined Under Namespace
Modules: Mixin
Class Method Summary collapse
-
.matcher {|| ... } ⇒ Fear::PartialFunction
Build pattern matcher to be used later, despite off Either#match method, id doesn’t apply matcher immanently, but build it instead.
Instance Method Summary collapse
- #==(other) ⇒ Boolean
-
#any? {|value| ... } ⇒ Boolean
Returns
falseifLeftor returns the result of the application of the given predicate to theRightvalue. - #deconstruct ⇒ <any>
-
#each {|value| ... } ⇒ Option
Performs the given block if this is a
Right. -
#flat_map {|value| ... } ⇒ Option
Returns the given block applied to the value from this
Rightor returns this if this is aLeft. -
#get_or_else(*args) ⇒ Object
Returns the value from this
Rightor evaluates the given default argument if this is aLeft. -
#include?(other_value) ⇒ Boolean
Returns
trueifRighthas an element that is equal (as determined by ==) toother_value,falseotherwise. - #initialize(value) ⇒ Object
- #inspect ⇒ String (also: #to_s)
-
#join_right ⇒ Either
Joins an
EitherthroughLeft. -
#left? ⇒ Boolean
Returns
trueif this is aLeft,falseotherwise. -
#map {|value| ... } ⇒ Object
Maps the given block to the value from this
Rightor returns this if this is aLeft. -
#match(&matcher) ⇒ Object
Pattern match against this
Either. -
#or_else(&alternative) ⇒ Either
Returns this
Rightor the given alternative if this is aLeft. -
#reduce(reduce_left, reduce_right) ⇒ any
Applies
reduce_leftif this is aLeftorreduce_rightif this is aRight. -
#reject {|value| ... } ⇒ Either
Returns
Leftof value if the given predicate holds for the right value, otherwise, returnsRight. -
#right? ⇒ Boolean
Returns
trueif this is aRight,falseotherwise. -
#select {|value| ... } ⇒ Either
Returns
Leftof value if the given predicate does not hold for the right value, otherwise, returnsRight. -
#select_or_else(default) {|value| ... } ⇒ Either
Returns
Leftof the default if the given predicate does not hold for the right value, otherwise, returnsRight. -
#swap ⇒ Either
If this is a
Left, then return the left value inRightor vice versa. -
#to_option ⇒ Option
Returns an
Somecontaining theRightvalue or aNoneif this is aLeft.
Class Method Details
.matcher {|| ... } ⇒ Fear::PartialFunction
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.
301 302 303 |
# File 'lib/fear/either.rb', line 301 def matcher(&matcher) EitherPatternMatch.new(&matcher) end |
Instance Method Details
#==(other) ⇒ Boolean
266 267 268 |
# File 'lib/fear/either.rb', line 266 def ==(other) other.is_a?(self.class) && value == other.value end |
#any? {|value| ... } ⇒ Boolean
Returns false if Left or returns the result of the application of the given predicate to the Right value.
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 |
# 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 to_s inspect # @return [<any>] def deconstruct [value] 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::EitherPatternMatch] # @return [Fear::PartialFunction] def matcher(&matcher) EitherPatternMatch.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 |
#deconstruct ⇒ <any>
279 280 281 |
# File 'lib/fear/either.rb', line 279 def deconstruct [value] end |
#each {|value| ... } ⇒ Option
Performs the given block if this is a Right.
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 |
# 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 to_s inspect # @return [<any>] def deconstruct [value] 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::EitherPatternMatch] # @return [Fear::PartialFunction] def matcher(&matcher) EitherPatternMatch.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 |
#flat_map {|value| ... } ⇒ Option
Returns the given block applied to the value from this Right or returns this 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 |
# 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 to_s inspect # @return [<any>] def deconstruct [value] 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::EitherPatternMatch] # @return [Fear::PartialFunction] def matcher(&matcher) EitherPatternMatch.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 |
#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 |
# 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 to_s inspect # @return [<any>] def deconstruct [value] 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::EitherPatternMatch] # @return [Fear::PartialFunction] def matcher(&matcher) EitherPatternMatch.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 |
#include?(other_value) ⇒ Boolean
Returns true if Right has an element that is equal (as determined by ==) to other_value, false otherwise.
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 |
# 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 to_s inspect # @return [<any>] def deconstruct [value] 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::EitherPatternMatch] # @return [Fear::PartialFunction] def matcher(&matcher) EitherPatternMatch.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 |
#initialize(value) ⇒ Object
257 258 259 |
# File 'lib/fear/either.rb', line 257 def initialize(value) @value = value end |
#inspect ⇒ String Also known as: to_s
271 272 273 |
# File 'lib/fear/either.rb', line 271 def inspect "#<#{self.class} value=#{value.inspect}>" end |
#join_right ⇒ Either
This method, and join_right, are analogous to Option#flatten
Joins an Either through Left. This method requires that the left side of this Either is itself an Either type.
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 |
# 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 to_s inspect # @return [<any>] def deconstruct [value] 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::EitherPatternMatch] # @return [Fear::PartialFunction] def matcher(&matcher) EitherPatternMatch.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 |
#left? ⇒ Boolean
this method is also aliased as #failure?
Returns true if this is a Left, false otherwise.
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 |
# 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 to_s inspect # @return [<any>] def deconstruct [value] 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::EitherPatternMatch] # @return [Fear::PartialFunction] def matcher(&matcher) EitherPatternMatch.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 |
#map {|value| ... } ⇒ Object
Maps the given block to the value from this Right or returns this 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 |
# 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 to_s inspect # @return [<any>] def deconstruct [value] 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::EitherPatternMatch] # @return [Fear::PartialFunction] def matcher(&matcher) EitherPatternMatch.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 |
#match(&matcher) ⇒ Object
Pattern match against this Either
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 |
# 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 to_s inspect # @return [<any>] def deconstruct [value] 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::EitherPatternMatch] # @return [Fear::PartialFunction] def matcher(&matcher) EitherPatternMatch.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 |
#or_else(&alternative) ⇒ Either
Returns this Right or the given alternative 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 |
# 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 to_s inspect # @return [<any>] def deconstruct [value] 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::EitherPatternMatch] # @return [Fear::PartialFunction] def matcher(&matcher) EitherPatternMatch.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 |
#reduce(reduce_left, reduce_right) ⇒ any
Applies reduce_left if this is a Left or reduce_right if this is a Right.
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 |
# 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 to_s inspect # @return [<any>] def deconstruct [value] 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::EitherPatternMatch] # @return [Fear::PartialFunction] def matcher(&matcher) EitherPatternMatch.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 |
#reject {|value| ... } ⇒ Either
Returns Left of value if the given predicate holds for the right value, otherwise, returns Right.
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 |
# 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 to_s inspect # @return [<any>] def deconstruct [value] 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::EitherPatternMatch] # @return [Fear::PartialFunction] def matcher(&matcher) EitherPatternMatch.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 |
#right? ⇒ Boolean
this method is also aliased as #success?
Returns true if this is a Right, false otherwise.
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 |
# 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 to_s inspect # @return [<any>] def deconstruct [value] 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::EitherPatternMatch] # @return [Fear::PartialFunction] def matcher(&matcher) EitherPatternMatch.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 |
#select {|value| ... } ⇒ Either
Returns Left of value if the given predicate does not hold for the right value, otherwise, returns Right.
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 |
# 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 to_s inspect # @return [<any>] def deconstruct [value] 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::EitherPatternMatch] # @return [Fear::PartialFunction] def matcher(&matcher) EitherPatternMatch.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 |
#select_or_else(default) {|value| ... } ⇒ Either
Returns Left of the default if the given predicate does not hold for the right value, otherwise, returns Right.
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 |
# 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 to_s inspect # @return [<any>] def deconstruct [value] 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::EitherPatternMatch] # @return [Fear::PartialFunction] def matcher(&matcher) EitherPatternMatch.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 |
#swap ⇒ Either
If this is a Left, then return the left value in Right or vice versa.
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 |
# 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 to_s inspect # @return [<any>] def deconstruct [value] 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::EitherPatternMatch] # @return [Fear::PartialFunction] def matcher(&matcher) EitherPatternMatch.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 |
#to_option ⇒ Option
Returns an Some containing the Right value or a None 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 |
# 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 to_s inspect # @return [<any>] def deconstruct [value] 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::EitherPatternMatch] # @return [Fear::PartialFunction] def matcher(&matcher) EitherPatternMatch.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 |