Created
April 2, 2009 00:32
-
-
Save jvoorhis/88964 to your computer and use it in GitHub Desktop.
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 Additive | |
attr :value | |
def initialize(value) | |
@value = value | |
end | |
def mappend(m) Additive.new(@value + m.value) end | |
end | |
class Multiplicative | |
attr :value | |
def initialize(value) | |
@value = value | |
end | |
def mappend(m) Multiplicative.new(@value * m.value) end | |
end | |
def mconcat(ms) | |
ms.inject { |a,b| a.mappend(b) } | |
end | |
def sum(xs) | |
mconcat( xs.map { |x| Additive.new(x) } ).value | |
end | |
def prod(xs) | |
mconcat( xs.map { |x| Multiplicative.new(x) } ).value | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment