Giter VIP home page Giter VIP logo

monads's Introduction

Monads

Build Status

This library provides simple Ruby implementations of some common monads.

The most important method of each implementation is #and_then (a.k.a. bind or >>=), which is used to connect together a sequence of operations involving the value(s) inside the monad. Each monad also has a .from_value class method (a.k.a. return or unit) for constructing an instance of that monad from an arbitrary value.

Optional

(a.k.a. the maybe monad)

An Optional object contains a value that might be nil.

>> require 'monads/optional'
=> true

>> include Monads
=> Object

>> optional_string = Optional.new('hello world')
=> #<struct Monads::Optional value="hello world">

>> optional_result = optional_string.and_then { |string| Optional.new(string.upcase) }
=> #<struct Monads::Optional value="HELLO WORLD">

>> optional_result.value
=> "HELLO WORLD"

>> optional_string = Optional.new(nil)
=> #<struct Monads::Optional value=nil>

>> optional_result = optional_string.and_then { |string| Optional.new(string.upcase) }
=> #<struct Monads::Optional value=nil>

>> optional_result.value
=> nil

Many

(a.k.a. the list monad)

A Many object contains multiple values.

>> require 'monads/many'
=> true

>> include Monads
=> Object

>> many_strings = Many.new(['hello world', 'goodbye world'])
=> #<struct Monads::Many values=["hello world", "goodbye world"]>

>> many_results = many_strings.and_then { |string| Many.new(string.split(/ /)) }
=> #<struct Monads::Many values=["hello", "world", "goodbye", "world"]>

>> many_results.values
=> ["hello", "world", "goodbye", "world"]

Eventually

(a.k.a. the continuation monad)

An Eventually object contains a value that will eventually be available, perhaps as the result of an asynchronous process (e.g. a network request).

>> require 'monads/eventually'
=> true

>> include Monads
=> Object

>> eventually_string = Eventually.new do |success|
     Thread.new do
       sleep 5
       success.call('hello world')
     end
   end
=> #<struct Monads::Eventually block=#<Proc>>

>> eventually_result = eventually_string.and_then do |string|
     Eventually.new do |success|
       Thread.new do
         sleep 5
         success.call(string.upcase)
       end
     end
   end
=> #<struct Monads::Eventually block=#<Proc>>

>> eventually_result.run { |string| puts string }
=> #<Thread run>
HELLO WORLD

monads's People

Contributors

tomstuart avatar

Watchers

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