alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (Function1Ops.scala)

This example Scala source code file (Function1Ops.scala) is included in the alvinalexander.com "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

boolean, enum, function1ops, monoid, option, state

The Function1Ops.scala Scala example source code

package scalaz
package syntax
package std

import Leibniz.===

final class Function1Ops[T, R](val self: T => R) extends AnyVal {

  def on[X](f: (R, R) => X, t1: T, t2: T): X = f(self(t1), self(t2))

  def arrow[A[_, _]](implicit a: Arrow[A]): A[T, R] =
    a.arr(self)

  def kleisli[Z[_]](implicit z: Applicative[Z]): Kleisli[Z, T, R] =
    Kleisli.kleisli((t: T) => z.point(self(t)))

  def unary_!(implicit m: Memo[T, R]): T => R = m(self)

  def toValidation[E](e: => E)(implicit ev: R =:= Boolean): T => Validation[NonEmptyList[E], T] =
    (t: T) => (if (self(t): Boolean) Success(t) else Failure(NonEmptyList(e)))

  def byName: (=> T) => R = t => self(t)

  def endo(implicit ev: R === T): Endo[T] =
    Endo.endo(ev.onF(self))

  def comparing(implicit o: Order[R]): (T, T) => Ordering =
    (t1, t2) => o.order(self(t1), self(t2))

  def equaling(implicit e: Equal[R]): (T, T) => Boolean =
    (t1, t2) => e.equal(self(t1), self(t2))

  def succState(implicit e: Enum[T]): State[T, R] =
    e.succState(self)

  def succStateZeroM[Y](k: R => State[T, Y])(implicit e: Enum[T], m: Monoid[T]): Y =
    e.succStateZeroM(self, k)

  def succStateZero[Y](k: R => Y)(implicit e: Enum[T], m: Monoid[T]): Y =
    e.succStateZero(self, k)

  def succStateMinM[Y](k: R => State[T, Y])(implicit e: Enum[T]): Option[Y] =
    e.succStateMinM(self, k)

  def succStateMin[Y](k: R => Y)(implicit e: Enum[T]): Option[Y] =
    e.succStateMin(self, k)

  def predState(implicit e: Enum[T]): State[T, R] =
    e.predState(self)

  def predStateZeroM[Y](k: R => State[T, Y])(implicit e: Enum[T], m: Monoid[T]): Y =
    e.predStateZeroM(self, k)

  def predStateZero[Y](k: R => Y)(implicit e: Enum[T], m: Monoid[T]): Y =
    e.predStateZero(self, k)

  def predStateMaxM[Y](k: R => State[T, Y])(implicit e: Enum[T]): Option[Y] =
    e.predStateMaxM(self, k)

  def predStateMax[Y](k: R => Y)(implicit e: Enum[T]): Option[Y] =
    e.predStateMax(self, k)

}

trait ToFunction1Ops {
  implicit def ToFunction1OpsFromBoolean[A, B](f: A => B): Function1Ops[A, B] = new Function1Ops(f)
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala Function1Ops.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, alvinalexander.com
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.