Class: Fear::Some

Inherits:
Object
  • Object
show all
Includes:
Option
Defined in:
lib/fear/some.rb

Constant Summary collapse

EXTRACTOR =
proc do |option|
  if Fear::Some === option
    Fear.some([option.get])
  else
    Fear.none
  end
end

Instance Method Summary collapse

Methods included from Option

#any?, #each, #flat_map, #get_or_else, #include?, #map, match, #match, matcher, #or_else

Constructor Details

#initialize(value) ⇒ Some

Returns a new instance of Some.



22
23
24
# File 'lib/fear/some.rb', line 22

def initialize(value)
  @value = value
end

Instance Method Details

#==(other) ⇒ Boolean

Parameters:

  • other (Any)

Returns:

  • (Boolean)


61
62
63
# File 'lib/fear/some.rb', line 61

def ==(other)
  other.is_a?(Some) && get == other.get
end

#deconstructArray<any>

Returns:

  • (Array<any>)


95
96
97
# File 'lib/fear/some.rb', line 95

def deconstruct
  [get]
end

#empty?false

Returns:

  • (false)


37
38
39
# File 'lib/fear/some.rb', line 37

def empty?
  false
end

#filter_map(&filter) ⇒ RightBiased::Left, RightBiased::Right

Returns:

  • (RightBiased::Left, RightBiased::Right)


90
91
92
# File 'lib/fear/some.rb', line 90

def filter_map(&filter)
  map(&filter).select(&:itself)
end

#getany

Returns:

  • (any)


27
28
29
# File 'lib/fear/some.rb', line 27

def get
  @value
end

#inspectString Also known as: to_s

Returns:

  • (String)


66
67
68
# File 'lib/fear/some.rb', line 66

def inspect
  "#<Fear::Some get=#{value.inspect}>"
end

#or_nilany

Returns:

  • (any)


32
33
34
# File 'lib/fear/some.rb', line 32

def or_nil
  @value
end

#rejectOption

Returns:



51
52
53
54
55
56
57
# File 'lib/fear/some.rb', line 51

def reject
  if yield(value)
    None
  else
    self
  end
end

#selectOption

Returns:



42
43
44
45
46
47
48
# File 'lib/fear/some.rb', line 42

def select
  if yield(value)
    self
  else
    None
  end
end

#zip(other) ⇒ Fear::Option

Parameters:

Returns:



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/fear/some.rb', line 75

def zip(other)
  if other.is_a?(Option)
    other.map do |x|
      if block_given?
        yield(value, x)
      else
        [value, x]
      end
    end
  else
    raise TypeError, "can't zip with #{other.class}"
  end
end