Skip to content

Instantly share code, notes, and snippets.

@globulon
Created March 31, 2012 17:46
Show Gist options
  • Select an option

  • Save globulon/2267069 to your computer and use it in GitHub Desktop.

Select an option

Save globulon/2267069 to your computer and use it in GitHub Desktop.
Replicable
package com.promindis.patterns
final case class Replicable[T](value: T) {
def replicate[L[_]](n: Int)(implicit m: MonoidC[L]): L[T] = {
def replicateIter(acc: L[T], rest: Int): L[T] = {
if (rest == 0) acc
else replicateIter(m.add(acc, m(value)), rest - 1)
}
replicateIter(m.unit, n)
}
}
final case class ReplicableM[T, M[_]](value: M[T]) {
def replicate[L[_]](n: Int)(implicit m: MonoidC[L]): L[M[T]] = {
def replicateIter(acc: L[M[T]], rest: Int): L[M[T]] = {
if (rest == 0) acc
else replicateIter(m.add(acc, m(value)), rest - 1)
}
replicateIter(m.unit, n)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment