Module: Mayak::Monads::Maybe::Mixin

Extended by:
T::Sig
Includes:
Kernel
Defined in:
lib/mayak/monads/maybe.rb

Instance Method Summary collapse

Instance Method Details

#check_maybe!(value, &blk) ⇒ Object



480
481
482
# File 'lib/mayak/monads/maybe.rb', line 480

def check_maybe!(value, &blk)
  do_maybe!(Mayak::Monads::Maybe.check(value, &blk))
end

#do_maybe!(value) ⇒ Object



464
465
466
467
468
469
470
471
472
473
# File 'lib/mayak/monads/maybe.rb', line 464

def do_maybe!(value)
  case value
  when Mayak::Monads::Maybe::Some
    value.value
  when Mayak::Monads::Maybe::None
    raise Halt[T.type_parameter(:Value)].new(value)
  else
    T.absurd(value)
  end
end

#for_maybe(&blk) ⇒ Object



452
453
454
455
456
457
# File 'lib/mayak/monads/maybe.rb', line 452

def for_maybe(&blk)
  result = blk.call
  Mayak::Monads::Maybe::Some[T.type_parameter(:Value)].new(result)
rescue Halt => e
  e.result
end

#guard_maybe!(&blk) ⇒ Object



485
486
487
# File 'lib/mayak/monads/maybe.rb', line 485

def guard_maybe!(&blk)
  do_maybe!(Mayak::Monads::Maybe.guard(&blk))
end

#Maybe(value) ⇒ Object



433
434
435
436
437
438
439
440
# File 'lib/mayak/monads/maybe.rb', line 433

def Maybe(value)
  case value
  when nil
    Mayak::Monads::Maybe::None[T.type_parameter(:Value)].new
  else
    Mayak::Monads::Maybe::Some[T.type_parameter(:Value)].new(value)
  end
end

#NoneObject



443
444
445
# File 'lib/mayak/monads/maybe.rb', line 443

def None
  Mayak::Monads::Maybe::None.new
end