Skip to content

Instantly share code, notes, and snippets.

@pasberth
Created May 8, 2013 19:30
Show Gist options
  • Save pasberth/5542977 to your computer and use it in GitHub Desktop.
Save pasberth/5542977 to your computer and use it in GitHub Desktop.
動的言語ではObjectをうまく活用することで型クラスのように振る舞わせることができる
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