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:
# @!method join_right
Joins an +Either+ through +Left+. This method requires
that the left side of this +Either+ is itself an
+Either+ type.
@note This method, and +join_right+, are analogous to +Option#flatten+
@return [Either]
@raise [TypeError] if it does not contain +Either+.
@example
Left(Right("flower")).join_left #=> Right("flower")
Left(Left(12)).join_left #=> Left(12)
Right("daisy").join_left #=> Right("daisy")
Right(Left("daisy")).join_left #=> Right(Left("daisy"))
Defined Under Namespace
Modules: Mixin
Instance Method Summary collapse
-
#any? {|value| ... } ⇒ Boolean
Returns
falseifLeftor returns the result of the application of the given predicate to theRightvalue. -
#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
-
#join_right ⇒ Either
Joins an
EitherthroughRight. -
#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. -
#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_a ⇒ Array
Returns an
Arraycontaining theRightvalue or an emptyArrayif this is aLeft. -
#to_option ⇒ Option
Returns an
Somecontaining theRightvalue or aNoneif this is aLeft.
Instance Method Details
#any? {|value| ... } ⇒ Boolean
Returns false if Left or returns the result of the application of the given predicate to the Right value.
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 260 261 262 263 264 |
# File 'lib/fear/either.rb', line 224 module Either include Dry::Equalizer(:value) # @private def left_class Left end # @private def right_class Right end def initialize(value) @value = value end attr_reader :value protected :value # 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 [any] # @return [Left] def Left(value) Left.new(value) end # @param [any] # @return [Right] def Right(value) Right.new(value) end end end |
#each {|value| ... } ⇒ Option
Performs the given block if this is a Right.
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 260 261 262 263 264 |
# File 'lib/fear/either.rb', line 224 module Either include Dry::Equalizer(:value) # @private def left_class Left end # @private def right_class Right end def initialize(value) @value = value end attr_reader :value protected :value # 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 [any] # @return [Left] def Left(value) Left.new(value) end # @param [any] # @return [Right] def Right(value) Right.new(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.
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 260 261 262 263 264 |
# File 'lib/fear/either.rb', line 224 module Either include Dry::Equalizer(:value) # @private def left_class Left end # @private def right_class Right end def initialize(value) @value = value end attr_reader :value protected :value # 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 [any] # @return [Left] def Left(value) Left.new(value) end # @param [any] # @return [Right] def Right(value) Right.new(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.
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 260 261 262 263 264 |
# File 'lib/fear/either.rb', line 224 module Either include Dry::Equalizer(:value) # @private def left_class Left end # @private def right_class Right end def initialize(value) @value = value end attr_reader :value protected :value # 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 [any] # @return [Left] def Left(value) Left.new(value) end # @param [any] # @return [Right] def Right(value) Right.new(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.
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 260 261 262 263 264 |
# File 'lib/fear/either.rb', line 224 module Either include Dry::Equalizer(:value) # @private def left_class Left end # @private def right_class Right end def initialize(value) @value = value end attr_reader :value protected :value # 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 [any] # @return [Left] def Left(value) Left.new(value) end # @param [any] # @return [Right] def Right(value) Right.new(value) end end end |
#initialize(value) ⇒ Object
237 238 239 |
# File 'lib/fear/either.rb', line 237 def initialize(value) @value = value end |
#join_right ⇒ Either
This method, and join_left, are analogous to Option#flatten
Joins an Either through Right. This method requires that the right side of this Either is itself an Either type.
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 260 261 262 263 264 |
# File 'lib/fear/either.rb', line 224 module Either include Dry::Equalizer(:value) # @private def left_class Left end # @private def right_class Right end def initialize(value) @value = value end attr_reader :value protected :value # 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 [any] # @return [Left] def Left(value) Left.new(value) end # @param [any] # @return [Right] def Right(value) Right.new(value) end end end |
#left? ⇒ Boolean
this method is also aliased as #failure?
Returns true if this is a Left, false otherwise.
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 260 261 262 263 264 |
# File 'lib/fear/either.rb', line 224 module Either include Dry::Equalizer(:value) # @private def left_class Left end # @private def right_class Right end def initialize(value) @value = value end attr_reader :value protected :value # 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 [any] # @return [Left] def Left(value) Left.new(value) end # @param [any] # @return [Right] def Right(value) Right.new(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.
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 260 261 262 263 264 |
# File 'lib/fear/either.rb', line 224 module Either include Dry::Equalizer(:value) # @private def left_class Left end # @private def right_class Right end def initialize(value) @value = value end attr_reader :value protected :value # 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 [any] # @return [Left] def Left(value) Left.new(value) end # @param [any] # @return [Right] def Right(value) Right.new(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.
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 260 261 262 263 264 |
# File 'lib/fear/either.rb', line 224 module Either include Dry::Equalizer(:value) # @private def left_class Left end # @private def right_class Right end def initialize(value) @value = value end attr_reader :value protected :value # 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 [any] # @return [Left] def Left(value) Left.new(value) end # @param [any] # @return [Right] def Right(value) Right.new(value) end end end |
#reject {|value| ... } ⇒ Either
Returns Left of value if the given predicate holds for the right value, otherwise, returns Right.
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 260 261 262 263 264 |
# File 'lib/fear/either.rb', line 224 module Either include Dry::Equalizer(:value) # @private def left_class Left end # @private def right_class Right end def initialize(value) @value = value end attr_reader :value protected :value # 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 [any] # @return [Left] def Left(value) Left.new(value) end # @param [any] # @return [Right] def Right(value) Right.new(value) end end end |
#right? ⇒ Boolean
this method is also aliased as #success?
Returns true if this is a Right, false otherwise.
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 260 261 262 263 264 |
# File 'lib/fear/either.rb', line 224 module Either include Dry::Equalizer(:value) # @private def left_class Left end # @private def right_class Right end def initialize(value) @value = value end attr_reader :value protected :value # 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 [any] # @return [Left] def Left(value) Left.new(value) end # @param [any] # @return [Right] def Right(value) Right.new(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.
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 260 261 262 263 264 |
# File 'lib/fear/either.rb', line 224 module Either include Dry::Equalizer(:value) # @private def left_class Left end # @private def right_class Right end def initialize(value) @value = value end attr_reader :value protected :value # 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 [any] # @return [Left] def Left(value) Left.new(value) end # @param [any] # @return [Right] def Right(value) Right.new(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.
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 260 261 262 263 264 |
# File 'lib/fear/either.rb', line 224 module Either include Dry::Equalizer(:value) # @private def left_class Left end # @private def right_class Right end def initialize(value) @value = value end attr_reader :value protected :value # 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 [any] # @return [Left] def Left(value) Left.new(value) end # @param [any] # @return [Right] def Right(value) Right.new(value) end end end |
#swap ⇒ Either
If this is a Left, then return the left value in Right or vice versa.
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 260 261 262 263 264 |
# File 'lib/fear/either.rb', line 224 module Either include Dry::Equalizer(:value) # @private def left_class Left end # @private def right_class Right end def initialize(value) @value = value end attr_reader :value protected :value # 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 [any] # @return [Left] def Left(value) Left.new(value) end # @param [any] # @return [Right] def Right(value) Right.new(value) end end end |
#to_a ⇒ Array
Returns an Array containing the Right value or an empty Array if this is a Left.
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 260 261 262 263 264 |
# File 'lib/fear/either.rb', line 224 module Either include Dry::Equalizer(:value) # @private def left_class Left end # @private def right_class Right end def initialize(value) @value = value end attr_reader :value protected :value # 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 [any] # @return [Left] def Left(value) Left.new(value) end # @param [any] # @return [Right] def Right(value) Right.new(value) end end end |
#to_option ⇒ Option
Returns an Some containing the Right value or a None if this is a Left.
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 260 261 262 263 264 |
# File 'lib/fear/either.rb', line 224 module Either include Dry::Equalizer(:value) # @private def left_class Left end # @private def right_class Right end def initialize(value) @value = value end attr_reader :value protected :value # 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 [any] # @return [Left] def Left(value) Left.new(value) end # @param [any] # @return [Right] def Right(value) Right.new(value) end end end |