Skip to content

Instantly share code, notes, and snippets.

@Prajjwal
Created April 23, 2015 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Prajjwal/58e8ba5c36f5f0224dc1 to your computer and use it in GitHub Desktop.
Save Prajjwal/58e8ba5c36f5f0224dc1 to your computer and use it in GitHub Desktop.
An attempt at monads in ruby
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