We can compose Foldable[F[_]]
and Foldable[G[_]]
instances to obtain Foldable[F[G]]
.
We can compose Foldable[F[_]]
and Foldable[G[_]]
instances to obtain Foldable[F[G]]
.
exists
checks whether at least one element satisfies the predicate.
exists
checks whether at least one element satisfies the predicate.
Convert F[A]
to List[A]
only including the elements that match a predicate.
Convert F[A]
to List[A]
only including the elements that match a predicate.
find
searches for the first element matching the predicate, if one exists.
find
searches for the first element matching the predicate, if one exists.
fold
, also called combineAll
, combines every value in the foldable using the given Monoid
instance.
fold
, also called combineAll
, combines every value in the foldable using the given Monoid
instance.
foldLeft
is an eager left-associative fold on F
using the given function.
foldLeft
is an eager left-associative fold on F
using the given function.
foldMap
is similar to fold
but maps every A
value into B
and then
combines them using the given Monoid[B]
instance.
foldMap
is similar to fold
but maps every A
value into B
and then
combines them using the given Monoid[B]
instance.
foldRight
is a lazy right-associative fold on F
using the given function.
foldRight
is a lazy right-associative fold on F
using the given function.
The function has the signature (A, Eval[B]) => Eval[B]
to support laziness in
a stack-safe way.
foldK
is similar to fold
but combines every value in the foldable using the given MonoidK[G]
instance
instead of Monoid[G]
.
foldK
is similar to fold
but combines every value in the foldable using the given MonoidK[G]
instance
instead of Monoid[G]
.
forall
checks whether all elements satisfy the predicate.
forall
checks whether all elements satisfy the predicate.
Hence when defining some new data structure, if we can define a foldLeft
and
foldRight
we are able to provide many other useful operations, if not always
the most efficient implementations, over the structure without further
implementation.
Hence when defining some new data structure, if we can define a foldLeft
and
foldRight
we are able to provide many other useful operations, if not always
the most efficient implementations, over the structure without further
implementation.
There are a few more methods that we haven't talked about but you probably can guess what they do:
Convert F[A]
to List[A]
.
Convert F[A]
to List[A]
.
traverse
the foldable mapping A
values to G[B]
, and combining
them using Applicative[G]
and discarding the results.
traverse
the foldable mapping A
values to G[B]
, and combining
them using Applicative[G]
and discarding the results.
This method is primarily useful when G[_]
represents an action
or effect, and the specific A
aspect of G[A]
is not otherwise
needed. The A
will be discarded and Unit
returned instead.
This method has been deprecated in favor of macro assertion and will be removed in a future version of ScalaTest. If you need this, please copy the source code into your own trait instead.
This method has been deprecated in favor of macro assertion and will be removed in a future version of ScalaTest. If you need this, please copy the source code into your own trait instead.
This method has been deprecated in favor of macro assumption and will be removed in a future version of ScalaTest. If you need this, please copy the source code into your own trait instead.
This method has been deprecated in favor of macro assumption and will be removed in a future version of ScalaTest. If you need this, please copy the source code into your own trait instead.
Please use 'an [Exception] should be thrownBy { ... }' syntax instead
This expect method has been deprecated. Please replace all invocations of expect with an identical invocation of assertResult instead.
This expect method has been deprecated. Please replace all invocations of expect with an identical invocation of assertResult instead.
This expectResult method has been deprecated. Please replace all invocations of expectResult with an identical invocation of assertResult instead.
This expectResult method has been deprecated. Please replace all invocations of expectResult with an identical invocation of assertResult instead.
Foldable type class instances can be defined for data structures that can be folded to a summary value.
In the case of a collection (such as
List
orSet
), these methods will fold together (combine) the values contained in the collection to produce a single result. Most collection types havefoldLeft
methods, which will usually be used by the associatedFoldable[_]
instance.Foldable[F]
is implemented in terms of two basic methods:foldLeft(fa, b)(f)
eagerly foldsfa
from left-to-right.foldRight(fa, b)(f)
lazily foldsfa
from right-to-left.These form the basis for many other operations, see also: A tutorial on the universality and expressiveness of fold
First some standard imports.
Apart from the familiar
foldLeft
andfoldRight
,Foldable
has a number of other useful functions.