cats

Fold

sealed abstract class Fold[A] extends Product with Serializable

Fold is designed to allow laziness/short-circuiting in foldRight.

It is a sum type that has three possible subtypes:

The meaning of these types can be made more clear with an example of the foldRight method in action. Here's a method to count how many elements appear in a list before the value 3:

def f(n: Int): Fold[Int] =
  if (n == 3) Fold.Return(0) else Fold.Continue(_ + 1)

val count: Lazy[Int] = List(1,2,3,4).foldRight(Lazy(0))(f)

When we call count.value, the following occurs:

Now we unwind back through the continue instances:

And so the result is 2.

This code searches an infinite stream for 77:

val found: Lazy[Boolean] =
Stream.from(0).foldRight(Lazy(false)) { n =>
  if (n == 77) Fold.Return(true) else Fold.Pass
}

Here's another example that sums the list until it reaches a negative number:

val sum: Lazy[Double] =
numbers.foldRight(Lazy(0.0)) { n =>
  if (n < 0) Fold.Return(0.0) else Fold.Continue(n + _)
}

This strange example counts an infinite stream. Since the result is lazy, it will only hang the program once count.value is called:

val count: Lazy[Long] =
Stream.from(0).foldRight(Lazy(0L)) { _ =>
  Fold.Continue(_ + 1L)
}

You can even implement foldLeft in terms of foldRight (!):

def foldl[A, B](as: List[A], b: B)(f: (B, A) => B): B =
as.foldRight(Lazy((b: B) => b)) { a =>
  Fold.Continue(g => (b: B) => g(f(b, a)))
}.value(b)

(In practice you would not want to use the foldl because it is not stack-safe.)

Linear Supertypes
Known Subclasses
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. Fold
  2. Serializable
  3. Serializable
  4. Product
  5. Equals
  6. AnyRef
  7. Any
  1. Hide All
  2. Show all
Learn more about member selection
Visibility
  1. Public
  2. All

Instance Constructors

  1. new Fold()

Abstract Value Members

  1. abstract def canEqual(that: Any): Boolean

    Definition Classes
    Equals
  2. abstract def productArity: Int

    Definition Classes
    Product
  3. abstract def productElement(n: Int): Any

    Definition Classes
    Product

Concrete Value Members

  1. final def !=(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  2. final def !=(arg0: Any): Boolean

    Definition Classes
    Any
  3. final def ##(): Int

    Definition Classes
    AnyRef → Any
  4. final def ==(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  5. final def ==(arg0: Any): Boolean

    Definition Classes
    Any
  6. final def asInstanceOf[T0]: T0

    Definition Classes
    Any
  7. def clone(): AnyRef

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  8. def complete(la: Lazy[A]): A

  9. def compose(f: (A) ⇒ A): Fold[A]

  10. final def eq(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  11. def equals(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  12. def finalize(): Unit

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  13. final def getClass(): Class[_]

    Definition Classes
    AnyRef → Any
  14. def hashCode(): Int

    Definition Classes
    AnyRef → Any
  15. def imap[B](f: (A) ⇒ B)(g: (B) ⇒ A): Fold[B]

  16. final def isInstanceOf[T0]: Boolean

    Definition Classes
    Any
  17. final def ne(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  18. final def notify(): Unit

    Definition Classes
    AnyRef
  19. final def notifyAll(): Unit

    Definition Classes
    AnyRef
  20. def productIterator: Iterator[Any]

    Definition Classes
    Product
  21. def productPrefix: String

    Definition Classes
    Product
  22. final def synchronized[T0](arg0: ⇒ T0): T0

    Definition Classes
    AnyRef
  23. def toString(): String

    Definition Classes
    AnyRef → Any
  24. final def wait(): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  25. final def wait(arg0: Long, arg1: Int): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  26. final def wait(arg0: Long): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from Serializable

Inherited from Serializable

Inherited from Product

Inherited from Equals

Inherited from AnyRef

Inherited from Any

Ungrouped