-
Notifications
You must be signed in to change notification settings - Fork 705
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Type classes: use standard inheritance but hide it
through abstract type of InstanceOfModule
- Loading branch information
Showing
85 changed files
with
462 additions
and
619 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package scalaz | ||
|
||
sealed abstract class InstanceOfModule { | ||
type InstanceOf[T] <: T | ||
def instanceOf[T](t: T): InstanceOf[T] | ||
} | ||
|
||
object InstanceOfModule { | ||
val impl : InstanceOfModule = new InstanceOfModule { | ||
override type InstanceOf[T] = T | ||
override def instanceOf[T](t: T) = t | ||
} | ||
} |
This file contains 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
This file contains 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
62 changes: 38 additions & 24 deletions
62
base/shared/src/main/scala/scalaz/data/ConstInstances.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,66 @@ | ||
package scalaz | ||
package data | ||
|
||
import typeclass.{FoldableClass, TraversableClass} | ||
import scalaz.typeclass._ | ||
|
||
import Prelude._ | ||
import FoldableClass._ | ||
import TraversableClass._ | ||
|
||
trait ConstInstances { | ||
implicit def constTraverse[R]: Traversable[Const[R, ?]] = new TraversableClass[Const[R, ?]] with FoldRight[Const[R, ?]] with Traverse[Const[R, ?]] { | ||
def map[A, B](ma: Const[R, A])(f: A => B): Const[R, B] = ma.retag | ||
implicit def constTraverse[R]: Traversable[Const[R, ?]] = instanceOf(new TraversableClass.DeriveSequence[Const[R, ?]] with FoldableClass.DeriveFoldMap[Const[R, ?]] with ConstFunctor[R] { | ||
|
||
def traverse[F[_], A, B](ta: Const[R, A])(f: A => F[B])(implicit F: Applicative[F]): F[Const[R, B]] = | ||
override def traverse[F[_], A, B](ta: Const[R, A])(f: A => F[B])(implicit F: Applicative[F]): F[Const[R, B]] = | ||
F.pure(ta.retag) | ||
|
||
def foldLeft[A, B](fa: Const[R, A], z: B)(f: (B, A) => B): B = z | ||
override def foldLeft[A, B](fa: Const[R, A], z: B)(f: (B, A) => B): B = z | ||
|
||
def foldRight[A, B](fa: Const[R, A], z: => B)(f: (A, => B) => B): B = z | ||
override def foldRight[A, B](fa: Const[R, A], z: => B)(f: (A, => B) => B): B = z | ||
|
||
override def toList[A](fa: Const[R, A]): List[A] = Nil | ||
} | ||
}) | ||
|
||
implicit def constFunctor[R]: Functor[Const[R, ?]] = new Functor[Const[R, ?]] { | ||
def map[A, B](fa: Const[R, A])(f: A => B): Const[R, B] = | ||
private trait ConstFunctor[R] extends FunctorClass[Const[R, ?]] { | ||
final override def map[A, B](fa: Const[R, A])(f: A => B): Const[R, B] = | ||
fa.retag[B] | ||
} | ||
|
||
implicit def constApply[R](implicit R: Semigroup[R]): Apply[Const[R, ?]] = new Apply[Const[R, ?]] { | ||
def functor: Functor[Const[R, ?]] = Const.constFunctor[R] | ||
def ap[A, B](fa: Const[R, A])(f: Const[R, A => B]): Const[R, B] = | ||
private trait ConstApply[R] extends ApplyClass[Const[R, ?]] with ConstFunctor[R] { | ||
def R: SemigroupClass[R] | ||
final override def ap[A, B](fa: Const[R, A])(f: Const[R, A => B]): Const[R, B] = | ||
Const(R.append(fa.getConst, f.getConst)) | ||
} | ||
|
||
implicit def constApplicative[R](implicit R: Monoid[R]): Applicative[Const[R, ?]] = new Applicative[Const[R, ?]] { | ||
def apply: Apply[Const[R, ?]] = Const.constApply[R] | ||
def pure[A](a: A): Const[R, A] = Const(R.empty) | ||
private trait ConstApplicative[R] extends ApplicativeClass[Const[R, ?]] with ConstApply[R] { | ||
override def R: MonoidClass[R] | ||
final override def pure[A](a: A): Const[R, A] = Const(R.empty) | ||
} | ||
|
||
implicit def constSemigroup[A, B](implicit A: Semigroup[A]): Semigroup[Const[A, B]] = new Semigroup[Const[A, B]] { | ||
def append(a1: Const[A, B], a2: => Const[A, B]): Const[A, B] = | ||
private trait ConstSemigroup[A, B] extends SemigroupClass[Const[A, B]] { | ||
def A: SemigroupClass[A] | ||
final override def append(a1: Const[A, B], a2: => Const[A, B]): Const[A, B] = | ||
Const(A.append(a1.getConst, a2.getConst)) | ||
} | ||
|
||
implicit def constMonoid[A, B](implicit A: Monoid[A]): Monoid[Const[A, B]] = new Monoid[Const[A, B]] { | ||
def semigroup: Semigroup[Const[A, B]] = implicitly | ||
def empty: Const[A, B] = Const(A.empty) | ||
private trait ConstMonoid[A, B] extends MonoidClass[Const[A, B]] with ConstSemigroup[A, B] { | ||
override def A: MonoidClass[A] | ||
final override def empty: Const[A, B] = Const(A.empty) | ||
} | ||
|
||
implicit def constDebug[A, B](implicit A: Debug[A]): Debug[Const[A, B]] = | ||
implicit def constApply[R: Semigroup]: Apply[Const[R, ?]] = instanceOf(new ConstApply[R] { | ||
override val R = implicitly | ||
}) | ||
|
||
implicit def constApplicative[R: Monoid]: Applicative[Const[R, ?]] = instanceOf(new ConstApplicative[R] { | ||
override val R = implicitly | ||
}) | ||
|
||
implicit def constSemigroup[A: Semigroup, B]: Semigroup[Const[A, B]] = instanceOf(new ConstSemigroup[A, B] { | ||
override val A = implicitly | ||
}) | ||
|
||
implicit def constMonoid[A: Monoid, B]: Monoid[Const[A, B]] = instanceOf(new ConstMonoid[A, B] { | ||
override val A = implicitly | ||
}) | ||
|
||
implicit def constDebug[A, B](implicit A: DebugClass[A]): DebugClass[Const[A, B]] = instanceOf[DebugClass[Const[A, B]]]( | ||
a => A.debug(a.getConst) | ||
) | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
8 changes: 4 additions & 4 deletions
8
base/shared/src/main/scala/scalaz/data/IdentityInstances.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
package scalaz | ||
package data | ||
|
||
import typeclass.MonadClass | ||
import scalaz.typeclass.MonadClass | ||
|
||
trait IdentityInstances { | ||
implicit val monad: Monad[Identity] = new MonadClass[Identity] { | ||
implicit val monad: Monad[Identity] = instanceOf(new MonadClass[Identity] { | ||
override def map[A, B](fa: Identity[A])(f: A => B): Identity[B] = Identity(f(fa.run)) | ||
override def ap[A, B](fa: Identity[A])(f: Identity[A => B]): Identity[B] = Identity(f.run.apply(fa.run)) | ||
override def ap[A, B](fa: Identity[A])(f: Identity[A => B]): Identity[B] = Identity(f.run(fa.run)) | ||
override def pure[A](a: A): Identity[A] = Identity(a) | ||
override def flatMap[A, B](oa: Identity[A])(f: A => Identity[B]): Identity[B] = f(oa.run) | ||
override def flatten[A](ma: Identity[Identity[A]]): Identity[A] = ma.run | ||
} | ||
}) | ||
} |
Oops, something went wrong.