Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tomstuart/monads
Simple Ruby implementations of some common monads.
https://github.com/tomstuart/monads
Last synced: about 2 months ago
JSON representation
Simple Ruby implementations of some common monads.
- Host: GitHub
- URL: https://github.com/tomstuart/monads
- Owner: tomstuart
- License: mit
- Created: 2014-08-02T13:52:07.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2019-09-08T09:18:50.000Z (about 5 years ago)
- Last Synced: 2024-07-27T18:51:04.207Z (about 2 months ago)
- Language: Ruby
- Homepage: https://tomstu.art/refactoring-ruby-with-monads
- Size: 32.2 KB
- Stars: 597
- Watchers: 24
- Forks: 93
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Monads
![](https://github.com/tomstuart/monads/workflows/Ruby/badge.svg)
This library provides simple Ruby implementations of some common [monads](http://en.wikipedia.org/wiki/Monad_(functional_programming)).
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](http://en.wikipedia.org/wiki/Monad_%28functional_programming%29#The_Maybe_monad))
An `Optional` object contains a value that might be `nil`.
```irb
>> require 'monads/optional'
=> true>> include Monads
=> Object>> optional_string = Optional.new('hello world')
=> #>> optional_result = optional_string.and_then { |string| Optional.new(string.upcase) }
=> #>> optional_result.value
=> "HELLO WORLD">> optional_string = Optional.new(nil)
=> #>> optional_result = optional_string.and_then { |string| Optional.new(string.upcase) }
=> #>> optional_result.value
=> nil
```## `Many`
(a.k.a. the [list monad](http://en.wikipedia.org/wiki/Monad_%28functional_programming%29#Collections))
A `Many` object contains multiple values.
```irb
>> require 'monads/many'
=> true>> include Monads
=> Object>> many_strings = Many.new(['hello world', 'goodbye world'])
=> #>> many_results = many_strings.and_then { |string| Many.new(string.split(/ /)) }
=> #>> many_results.values
=> ["hello", "world", "goodbye", "world"]
```## `Eventually`
(a.k.a. the [continuation monad](http://en.wikipedia.org/wiki/Monad_%28functional_programming%29#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).
```irb
>> require 'monads/eventually'
=> true>> include Monads
=> Object>> eventually_string = Eventually.new do |success|
Thread.new do
sleep 5
success.call('hello world')
end
end
=> #>>> eventually_result = eventually_string.and_then do |string|
Eventually.new do |success|
Thread.new do
sleep 5
success.call(string.upcase)
end
end
end
=> #>>> eventually_result.run { |string| puts string }
=> #
HELLO WORLD
```