Created
April 23, 2015 15:40
-
-
Save Prajjwal/58e8ba5c36f5f0224dc1 to your computer and use it in GitHub Desktop.
An attempt at monads in ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Array | |
def m_return(value) | |
[value] | |
end | |
def m_bind(f) | |
self.map(&f).flatten | |
end | |
end | |
arr = [4, 5] | |
a = arr.m_bind(lambda { |x| [x * x] }) | |
p a | |
b = a.m_bind(lambda { |x| [x + x] }) | |
p b | |
# OUTPUT: | |
# λ ppc temp → ruby monad.rb | |
# [16, 25] | |
# [32, 50] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment