• Stars
    star
    597
  • Rank 74,317 (Top 2 %)
  • Language
    Ruby
  • License
    MIT License
  • Created about 10 years ago
  • Updated almost 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple Ruby implementations of some common monads.

Monads

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

More Repositories

1

computationbook

Example code for Understanding Computation
Ruby
494
star
2

nothing

Programming with Nothing
Ruby
244
star
3

utf-8-challenges

A short tutorial on UTF-8. Run with `ruby utf_8_challenges.rb`, unskip each test and make it pass!
Ruby
48
star
4

kanren

An example Ruby implementation of μKanren.
Ruby
22
star
5

tradfri

A Ruby interface to IKEA’s smart lighting system
Ruby
18
star
6

dual_number

A Ruby implementation of dual numbers.
Ruby
17
star
7

vector_space

A Ruby library for treating multidimensional values as elements of a vector space.
Ruby
13
star
8

wasminna

Live coding a WebAssembly interpreter in pure Ruby with no dependencies, guided by the Wasm spec’s test suite
Ruby
13
star
9

something

Programming with Something
Ruby
9
star
10

little_scheme

Growing a little Scheme interpreter, guided by The Little Schemer
Ruby
9
star
11

inference-rules

A simple implementation of generic inference rules
Ruby
7
star
12

react-workshop

What Even Is A React (And So Can You!)
JavaScript
6
star
13

govuk-exhibit

A GOV.UK exhibit
Ruby
5
star
14

lox

An implementation of the Lox language from Robert Nystrom’s “Crafting Interpreters”
Ruby
3
star
15

neural-network

HTML
2
star
16

subsequence_matchers

Ruby
2
star
17

capybara-envjs-button-bug

Ruby
1
star
18

genetic-algorithms

A scrappy genetic algorithm visualisation for London Computation Club
HTML
1
star
19

rubyforge-redirects

Crowdsourced redirects for old RubyForge URLs
Ruby
1
star
20

recursion-workshop

JavaScript
1
star
21

mandelbrot

Some scrappy Mandelbrot visualisations for London Computation Club
HTML
1
star
22

heylist

Ruby
1
star
23

negative-numbers

Representing negative numbers in Ruby
Ruby
1
star
24

tapl

A Ruby implementation of typecheckers from Types and Programming Languages
Ruby
1
star
25

hoas

An implementation of higher-order abstract syntax in Ruby
Ruby
1
star