| career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (Endomorphic.scala)

This example Scala source code file (Endomorphic.scala) is included in the "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Scala by Example" TM.

Learn more about this Scala project at its project page.

Java - Scala tags/keywords

compose, endomorphic, endomorphicinstances0, endomorphicinstances1, endomorphicinstances2, endomorphicsemigroup, monad, monoid, semigroup

The Endomorphic.scala Scala example source code

package scalaz

/** Endomorphisms have special properties among arrows, so are captured in this newtype.
  * Endomorphic[Function1, A] is equivalent to Endo[A]
final case class Endomorphic[=>:[_, _], A](run: A =>: A) {

  final def compose(that: Endomorphic[=>:, A])(implicit F: Compose[=>:]): Endomorphic[=>:, A] =
    Endomorphic[=>:, A](F.compose(run,

  final def andThen(that: Endomorphic[=>:, A])(implicit F: Compose[=>:]): Endomorphic[=>:, A] =


object Endomorphic extends EndomorphicInstances {

  /** Endomorphic Kleisli arrow */
  final def endoKleisli[F[_]: Monad, A](f: A => F[A]): Endomorphic[Kleisli[F, ?, ?], A] =
    Endomorphic[Kleisli[F, ?, ?], A](Kleisli(f))

sealed abstract class EndomorphicInstances extends EndomorphicInstances0 {

  implicit def endomorphicMonoid[=>:[_, _], A](implicit G: Category[=>:]): Monoid[Endomorphic[=>:, A]] =
    new Monoid[Endomorphic[=>:, A]] with EndomorphicSemigroup[=>:, A] {
      val F = G
      def zero: Endomorphic[=>:, A] = Endomorphic(


sealed abstract class EndomorphicInstances0 extends EndomorphicInstances1 {

  implicit def kleisliEndoInstance[F[_]: Monad, A]: Monoid[Endomorphic[Kleisli[F, ?, ?], A]] =
    Endomorphic.endomorphicMonoid[Kleisli[F, ?, ?], A]

  implicit def cokleisliEndoInstance[F[_]: Comonad, A]: Monoid[Endomorphic[Cokleisli[F, ?, ?], A]] =
    Endomorphic.endomorphicMonoid[Cokleisli[F, ?, ?], A]


sealed abstract class EndomorphicInstances1 extends EndomorphicInstances2 {

  implicit def endomorphicSemigroup[=>:[_, _], A](implicit G: Compose[=>:]): Semigroup[Endomorphic[=>:, A]] =
    new EndomorphicSemigroup[=>:, A] {
      val F = G


sealed abstract class EndomorphicInstances2 {

  implicit def kleisliEndoSemigroup[F[_]: Bind, A]: Semigroup[Endomorphic[Kleisli[F, ?, ?], A]] =
    Endomorphic.endomorphicSemigroup[Kleisli[F, ?, ?], A]

  implicit def cokleisliEndoSemigroup[F[_]: Cobind, A]: Semigroup[Endomorphic[Cokleisli[F, ?, ?], A]] =
    Endomorphic.endomorphicSemigroup[Cokleisli[F, ?, ?], A]


private trait EndomorphicSemigroup[=>:[_, _], A] extends Semigroup[Endomorphic[=>:, A]] {
  implicit def F: Compose[=>:]
  def append(f1: Endomorphic[=>:, A], f2: => Endomorphic[=>:, A]) = Endomorphic(F.compose(,

Other Scala examples (source code examples)

Here is a short list of links related to this Scala Endomorphic.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller


new blog posts


Copyright 1998-2021 Alvin Alexander,
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.