Skip to content

Instantly share code, notes, and snippets.

@stacycurl
Created October 17, 2017 09:13
Show Gist options
  • Save stacycurl/6b46c0bf0d218b1f828b2718b34e5e74 to your computer and use it in GitHub Desktop.
Save stacycurl/6b46c0bf0d218b1f828b2718b34e5e74 to your computer and use it in GitHub Desktop.
implicit def tuple2Semigroup[A, B](implicit A: Semigroup[A], B: Semigroup[B]): Semigroup[(A, B)] = instance[(A, B)] {
case ((la, lb), (ra, rb)) => (A.append(la, ra), B.append(lb, rb))
}
/** Make an associative binary function into an __Serializable__ instance. */
def instance[A](f: (A, => A) => A): Semigroup[A] = SerializableSemigroup(f)
private case class SerializableSemigroup[A](f: (A, => A) => A) extends Semigroup[A] {
private val self: Semigroup[A] = this
override def append(lhs: A, rhs: => A): A = f(lhs, rhs)
override val semigroupSyntax: SSemigroupSyntax[A] = new SSemigroupSyntax[A] with Serializable {
def F: Semigroup[A] = self
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment