Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

foldLeft, foldRight, other Foldable specializations #11592

Merged
merged 7 commits into from
Nov 9, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
a few docs for FoldableContravariant
  • Loading branch information
S11001001 committed Nov 9, 2021
commit 962513aec2b460cc361b75724b066aa196d8e30f
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import scalaz.{Monoid, Foldable, Semigroup}
import FoldableContravariant._

// Specialized overrides for when a Foldable wraps another Foldable.
// If you need to hand-write some of these, just mix in the traits you
// don't want to hand-write.
private[daml] trait FoldableContravariant[X[_], Y[_]]
extends CoreOps[X, Y]
with Semigroupoids[X, Y]
Expand All @@ -19,13 +21,15 @@ private[daml] object FoldableContravariant {
protected[this] def ctmap[A](ax: X[A]): Y[A]
}

// non-derived combinators
trait CoreOps[X[_], Y[_]] extends Foldable[X] with CtMap[X, Y] {
override final def foldLeft[A, B](xa: X[A], z: B)(f: (B, A) => B) = Y.foldLeft(ctmap(xa), z)(f)
override final def foldRight[A, B](xa: X[A], z: => B)(f: (A, => B) => B) =
Y.foldRight(ctmap(xa), z)(f)
override final def foldMap[A, Z: Monoid](xa: X[A])(f: A => Z) = Y.foldMap(ctmap(xa))(f)
}

// plays on functions from the semigroupoids library
trait Semigroupoids[X[_], Y[_]] extends Foldable[X] with CtMap[X, Y] {
override final def foldMapRight1Opt[A, B](xa: X[A])(z: A => B)(f: (A, B) => B) =
Y.foldMapRight1Opt(ctmap(xa))(z)(f)
Expand All @@ -35,6 +39,7 @@ private[daml] object FoldableContravariant {
Y.foldMap1Opt(ctmap(xa))(f)
}

// to (collection type) converters
trait Conversions[X[_], Y[_]] extends Foldable[X] with CtMap[X, Y] {
override final def toStream[A](xa: X[A]) = Y.toStream(ctmap(xa))
override final def toSet[A](xa: X[A]) = Y.toSet(ctmap(xa))
Expand All @@ -45,6 +50,7 @@ private[daml] object FoldableContravariant {
Y.toEphemeralStream(ctmap(xa))
}

// list-like operations
trait Lookups[X[_], Y[_]] extends Foldable[X] with CtMap[X, Y] {
override final def index[A](xa: X[A], i: Int) = Y.index(ctmap(xa), i)
override final def length[A](xa: X[A]) = Y.length(ctmap(xa))
Expand Down