Skip to content

Instantly share code, notes, and snippets.

@jvoorhis
Created April 2, 2009 00:32
Show Gist options
  • Save jvoorhis/88964 to your computer and use it in GitHub Desktop.
Save jvoorhis/88964 to your computer and use it in GitHub Desktop.
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