Created
May 8, 2013 19:30
-
-
Save pasberth/5542977 to your computer and use it in GitHub Desktop.
動的言語ではObjectをうまく活用することで型クラスのように振る舞わせることができる
This file contains 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
module Monad | |
def return!(a); end | |
def bind(m,k); end | |
def join(m,k); (bind m,->(_){k}); end | |
end | |
module MonadPlus; include Monad | |
def mzero; end | |
def guard(cond); cond ? (return! nil) : mzero; end | |
end | |
class List; include Monad, MonadPlus | |
def return!(a); [a]; end | |
def bind(m,k); m.map(&k).flatten(1); end | |
def mzero; []; end | |
end | |
p List.new.guard(true) # [nil] = return nil | |
p List.new.guard(false) # [] = mzero |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment