Class: Fear::Either::LeftProjection

Inherits:
Object
  • Object
show all
Defined in:
lib/fear/either/left_projection.rb

Overview

Projects an ‘Either` into a `Left`.

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(either) ⇒ LeftProjection

Returns a new instance of LeftProjection.

Parameters:



17
18
19
# File 'lib/fear/either/left_projection.rb', line 17

def initialize(either)
  @either = either
end

Instance Attribute Details

#either=(value) ⇒ Fear::Either

Returns:



13
14
15
# File 'lib/fear/either/left_projection.rb', line 13

def either
  @either
end

Instance Method Details

#==(other) ⇒ Boolean

Parameters:

  • other (Object)

Returns:

  • (Boolean)


224
225
226
# File 'lib/fear/either/left_projection.rb', line 224

def ==(other)
  other.is_a?(self.class) && other.either == either
end

#any? {|value| ... } ⇒ Boolean

Returns false if Fear::Right or returns the result of the application of the given predicate to the Fear::Light value.

Examples:

Fear.left(12).left.any? { |v| v > 10 }  #=> true
Fear.left(7).left.any? { |v| v > 10 }   #=> false
Fear.right(12).left.any? { |v| v > 10 } #=> false

Yield Parameters:

  • value (Object)

Yield Returns:

  • (Boolean)

Returns:

  • (Boolean)


169
170
171
172
173
174
175
176
# File 'lib/fear/either/left_projection.rb', line 169

def any?(&predicate)
  case either
  in Fear::Left(value)
    predicate.(value)
  in Fear::Right
    false
  end
end

#each {|value| ... } ⇒ Fear::Either Also known as: apply

Performs the given block if this is a Fear::Left.

Examples:

Fear.right(17).left.each do |value|
  puts value
end #=> does nothing

Fear.left('undefined').left.each do |value|
  puts value
end #=> prints "nothing"

Yield Parameters:

  • value (Object)

Yield Returns:

  • (void)

Returns:



76
77
78
79
80
81
82
83
84
# File 'lib/fear/either/left_projection.rb', line 76

def each
  case either
  in Fear::Left(value)
    yield(value)
    either
  in Fear::Right
    either
  end
end

#find {|value| ... } ⇒ Fear::Option<Fear::Either> Also known as: detect

Returns Fear::None if this is a Fear::Right or if the given predicate does not hold for the left value, otherwise, returns a Fear::Left.

Examples:

Fear.left(12).left.find(&:even?) #=> #<Fear::Some value=#<Fear::Left value=12>>
Fear.left(7).left.find(&:even?)  #=> #<Fear::None>
Fear.right(12).left.find(&:even) #=> #<Fear::None>

Yield Parameters:

  • value (Object)

Yield Returns:

  • (Boolean)

Returns:



212
213
214
215
216
217
218
219
# File 'lib/fear/either/left_projection.rb', line 212

def find(&predicate)
  case either
  in Fear::Left(value) if predicate.(value)
    Fear.some(either)
  in Fear::Either
    Fear.none
  end
end

#flat_map {|value| ... } ⇒ Fear::Either

Returns the given block applied to the value from this Fear::Left or returns this if this is a Fear::Right.

Examples:

Fear.left(12).left.flat_map { Fear.left(_1 * 2) }  #=> Fear.left(24)
Fear.left(12).left.flat_map { Fear.right(_1 * 2) } #=> Fear.right(24)
Fear.right(12).left.flat_map { Fear.left(_1 * 2) } #=> Fear.right(12)

Yield Parameters:

  • value (Object)

Yield Returns:

Returns:



116
117
118
119
120
121
122
123
# File 'lib/fear/either/left_projection.rb', line 116

def flat_map
  case either
  in Fear::Left(value)
    yield(value)
  in Fear::Right
    either
  end
end

#get_or_else(&default) ⇒ Object #get_or_else(default) ⇒ Object

Returns the value from this Fear::Left or evaluates the given default argument if this is a Fear::Right.

Overloads:

  • #get_or_else(&default) ⇒ Object

    Examples:

    Fear.right(42).left.get_or_else { 24/2 }         #=> 12
    Fear.left('undefined').left.get_or_else { 24/2 } #=> 'undefined'

    Yield Returns:

    • (Object)

    Returns:

    • (Object)
  • #get_or_else(default) ⇒ Object

    Examples:

    Fear.right(42).left.get_or_else(12)         #=> 12
    Fear.left('undefined').left.get_or_else(12) #=> 'undefined'

    Returns:

    • (Object)


54
55
56
57
58
59
60
61
# File 'lib/fear/either/left_projection.rb', line 54

def get_or_else(*args)
  case either
  in Fear::Left(value)
    value
  in Fear::Right
    args.fetch(0) { yield }
  end
end

#include?(other_value) ⇒ Boolean

Returns true if Fear::Left has an element that is equal (as determined by ==) to other_value, false otherwise.

Examples:

Fear.left(17).left.include?(17)           #=> true
Fear.left(17).left.include?(7)            #=> false
Fear.right('undefined').left.include?(17) #=> false

Parameters:

  • (Object)

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
# File 'lib/fear/either/left_projection.rb', line 30

def include?(other_value)
  case either
  in Fear::Left(x)
    x == other_value
  in Fear::Right
    false
  end
end

#map {|value| ... } ⇒ Object

Maps the block argument through Fear::Left.

Examples:

Fear.left(42).left.map { _1/2 }   #=> Fear.left(24)
Fear.right(42).left.map { _1/2 }  #=> Fear.right(42)

Yield Parameters:

  • value (Object)

Yield Returns:



95
96
97
98
99
100
101
102
# File 'lib/fear/either/left_projection.rb', line 95

def map
  case either
  in Fear::Left(value)
    Fear.left(yield(value))
  in Fear::Right
    either
  end
end

#select {|value| ... } ⇒ Fear::Either

Returns Fear::Right of value if the given predicate does not hold for the left value, otherwise, returns Fear::Left.

Examples:

Fear.left(12).left.select(&:even?)   #=> Fear.left(12)
Fear.left(7).left.select(&:even?)    #=> Fear.right(7)
Fear.right(12).left.select(&:even?)  #=> Fear.right(12)
Fear.right(7).left.select(&:even?)   #=> Fear.right(7)

Yield Parameters:

  • value (Object)

Yield Returns:

  • (Boolean)

Returns:



190
191
192
193
194
195
196
197
198
199
# File 'lib/fear/either/left_projection.rb', line 190

def select(&predicate)
  case either
  in Fear::Right
    either
  in Fear::Left(value) if predicate.(value)
    either
  in Fear::Left
    either.swap
  end
end

#to_aArray

Returns an array containing the Fear::Left value or an empty array if this is a Fear::Right.

Examples:

Fear.left(42).left.to_a   #=> [42]
Fear.right(42).left.to_a  #=> []

Returns:

  • (Array)


149
150
151
152
153
154
155
156
# File 'lib/fear/either/left_projection.rb', line 149

def to_a
  case either
  in Fear::Left(value)
    [value]
  in Fear::Right
    []
  end
end

#to_optionFear::Option

Returns an Fear::Some containing the Fear::Left value or a Fear::None if this is a Fear::Right.

Examples:

Fear.left(42).left.to_option   #=> Fear.some(42)
Fear.right(42).left.to_option  #=> Fear.none

Returns:



132
133
134
135
136
137
138
139
# File 'lib/fear/either/left_projection.rb', line 132

def to_option
  case either
  in Fear::Left(value)
    Fear.some(value)
  in Fear::Right
    Fear.none
  end
end