Module: Mayak::Monads::Maybe

Extended by:
T::Generic, T::Helpers, T::Sig
Included in:
None, Some
Defined in:
lib/mayak/monads/maybe.rb

Defined Under Namespace

Modules: Mixin Classes: None, Some

Constant Summary collapse

Value =
type_member(:out)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.check(value, &blk) ⇒ Object



159
160
161
162
163
164
165
# File 'lib/mayak/monads/maybe.rb', line 159

def self.check(value, &blk)
  if blk.call
    Mayak::Monads::Maybe::Some[T.type_parameter(:Value)].new(value)
  else
    Mayak::Monads::Maybe::None[T.type_parameter(:Value)].new
  end
end

.guard(&blk) ⇒ Object



168
169
170
# File 'lib/mayak/monads/maybe.rb', line 168

def self.guard(&blk)
  check(nil, &blk)
end

.sequence(results) ⇒ Object



145
146
147
148
149
150
151
152
# File 'lib/mayak/monads/maybe.rb', line 145

def self.sequence(results)
  init = T.let(Maybe::Some.new([]), Maybe[T::Array[T.type_parameter(:Value)]])
  results.reduce(init) do |result, element|
    result.flat_map do |array|
      element.map { |value| array + [value] }
    end
  end
end

Instance Method Details

#==(another) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/mayak/monads/maybe.rb', line 121

def ==(another)
  case self
  when Mayak::Monads::Maybe::Some
    case another
    when Mayak::Monads::Maybe::Some then T.unsafe(self.value) == another.value
    when Mayak::Monads::Maybe::None then false
    else T.absurd(another)
    end
  when Mayak::Monads::Maybe::None
    case another
    when Mayak::Monads::Maybe::Some then false
    when Mayak::Monads::Maybe::None then true
    else T.absurd(another)
    end
  else
    T.absurd(self)
  end
end

#as(new_value) ⇒ Object



98
99
100
# File 'lib/mayak/monads/maybe.rb', line 98

def as(new_value)
  map { |_| new_value }
end

#either(none_branch, some_branch) ⇒ Object



77
78
# File 'lib/mayak/monads/maybe.rb', line 77

def either(none_branch, some_branch)
end

#filter(&blk) ⇒ Object



41
42
# File 'lib/mayak/monads/maybe.rb', line 41

def filter(&blk)
end

#flat_map(&blk) ⇒ Object



33
34
# File 'lib/mayak/monads/maybe.rb', line 33

def flat_map(&blk)
end

#map(&blk) ⇒ Object



22
23
# File 'lib/mayak/monads/maybe.rb', line 22

def map(&blk)
end

#none?Boolean

Returns:

  • (Boolean)


49
50
# File 'lib/mayak/monads/maybe.rb', line 49

def none?
end

#recover(value) ⇒ Object



108
109
# File 'lib/mayak/monads/maybe.rb', line 108

def recover(value)
end

#recover_with_maybe(maybe) ⇒ Object



117
118
# File 'lib/mayak/monads/maybe.rb', line 117

def recover_with_maybe(maybe)
end

#some?Boolean

Returns:

  • (Boolean)


45
46
# File 'lib/mayak/monads/maybe.rb', line 45

def some?
end

#tee(&blk) ⇒ Object



66
67
# File 'lib/mayak/monads/maybe.rb', line 66

def tee(&blk)
end

#to_result(failure) ⇒ Object



86
87
# File 'lib/mayak/monads/maybe.rb', line 86

def to_result(failure)
end

#to_try(error) ⇒ Object



90
91
# File 'lib/mayak/monads/maybe.rb', line 90

def to_try(error)
end

#value_or(value) ⇒ Object



58
59
# File 'lib/mayak/monads/maybe.rb', line 58

def value_or(value)
end