Giter Site home page Giter Site logo

resonad's Introduction

Resonad

Usage example (assume each method returns a Resonad):

find_widget(widget_id)
  .and_then { |widget| update_widget(widget) }
  .on_success { |widget| logger.info("Updated #{widget}" }
  .on_failure { |error| logger.warn("Widget update failed because #{error}") }

Success type:

result = Resonad.Success(5)
result.success? #=> true
result.failure? #=> false
result.value #=> 5
result.error #=> raises an exception

Failure type:

result = Resonad.Failure(:buzz)
result.success? #=> false
result.failure? #=> true
result.value #=> raises an exception
result.error #=> :buzz

Mapping monads:

result = Resonad.Success(5)
  .map { |i| i + 1 }
  .map { |i| i + 1 }
  .map { |i| i + 1 }
result.success? #=> true
result.value #=> 8

result = Resonad.Failure(:buzz)
  .map { |i| i + 1 }
  .map { |i| i + 1 }
  .map { |i| i + 1 }
result.success? #=> false
result.error #=> :buzz

Flat mapping monads (a.k.a. and_then):

result = Resonad.Success(5)
  .and_then { |i| Resonad.Success(i + 1) }
  .and_then { |i| Resonad.Failure("buzz #{i}") }
  .and_then { |i| Resonad.Success(i + 1) }
  .error #=> "buzz 6"

# can also use the less-nice `flat_map` method
result
  .flat_map { |i| Resonad.Success(i + 1) }

Automatic exception rescuing:

def try_divide(top, bottom)
  Resonad.rescuing_from(ZeroDivisionError) { top / bottom }
end

yep = try_divide(6, 2)
yep.success? #=> true
yep.value #=> 3

nope = try_divide(6, 0)
nope.success? #=> false
node.error #=> #<ZeroDivisionError: ZeroDivisionError>

resonad's People

Contributors

tomdalling 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.