module Unsound::Control

Public Instance Methods

maybe(&block) click to toggle source

Execute the block. If the block results in Nil, return {Data::Nothing}, otherwise wrap the result in a {Data::Just}

@param block [Block] the block to execute @return [Data::Nothing, Data::Just] an instance of {Data::Maybe}

# File lib/unsound/control.rb, line 35
def maybe(&block)
  if (result = block.call).nil?
    Data::Nothing.new
  else
    Data::Just.new(result)
  end
end
try(&block) click to toggle source

Try executing the block. If the block raises an exception wrap it in a {Data::Left}, otherwise wrap it in a {Data::Right}.

@param block [Block] the block to execute @return [Data::Right, Data::Left] an instance of

a {Data::Right} or {Data::Left} to indicate success or failure.
# File lib/unsound/control.rb, line 14
def try(&block)
  Data::Right.new(block.call)
rescue
  Data::Left.new($ERROR_INFO)
end
wrap_with_maybe(&block) click to toggle source

Like .maybe, but wraps the block in a Proc for lazy execution @param block [Block] the block to execute @return [Proc] a proc which will execute the supplied block using .maybe

when executed.
# File lib/unsound/control.rb, line 47
def wrap_with_maybe(&block)
  ->(*args) { maybe{ block.call(*args) } }
end
wrap_with_try(&block) click to toggle source

Like .try, but wraps the block in a Proc for lazy execution @param block [Block] the block to execute @return [Proc] a proc which will execute the supplied block using .try

when executed.
# File lib/unsound/control.rb, line 25
def wrap_with_try(&block)
  ->(*args) { try { block.call(*args) } }
end