Coder Social home page Coder Social logo

unexceptional's Introduction

Unexceptional

Provides a Result class for more elegant, exception-free error handling. Especially useful for processing input that could be invalid for many different reasons.

API Docs

http://www.rubydoc.info/github/jarrett/unexceptional/master/Unexceptional/Result

Examples

Result.try(
  ->    { Result.ok 2 },
  ->(i) { Result.ok 3 * i }
)
# => Result.ok(6)


Result.try(
  ->    { Result.ok 2 },
  ->(_) { Result.err :uh_oh },
  ->(i) { Result.ok 3 * i }
)
# => Result.err(:uh_oh)


Result.try(
  ->         { Result.ok [1, 2] },
  ->((a, b)) { Result.ok a + b }
)
# => Result.ok(3)


Result.map_while([1, 2]) do |i|
  Result.ok i * 2
end
# => Result.ok([1, 2])


Result.map_while([1, 2, 3]) do |i|
  if i == 2
    Result.err '2 is invalid'
  elsif i == 3
    raise 'This is never executed because Result.map_while aborts on the previous element.'
  else
    Result.ok i * 2
  end
end
# => Result.err('2 is invalid')


Result
  .err(:uh_oh)
  .and_then { 'This block never executes' }
# => Result.err(:uh_oh)


Result
  .ok('This value gets dropped')
  .and_then(Result.ok('This is the final value'))
# => Result.ok('This is the final value')


Result
  .ok(3)
  .and_then { |v| v * 2 }
# => Result.ok(6)


Result
  .ok('This value gets dropped')
  .and_then
# => Result.ok

unexceptional's People

Contributors

jarrett avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.