This module provides a type class for unfoldable functors, i.e.
functors which support an
This allows us to unify various operations on arrays, lists, sequences, etc.
class Unfoldable :: (Type -> Type) -> Constraint
class (Unfoldable1 t) <= Unfoldable t where
This class identifies (possibly empty) data structures which can be unfolded.
The generating function
unfoldr f is understood as follows:
unfoldr f bshould be empty.
Just (Tuple a b1), then
unfoldr f bshould consist of
aappended to the result of
unfoldr f b1.
Note that it is not possible to give
Unfoldable instances to types which
represent structures which are guaranteed to be non-empty, such as
NonEmptyArray: consider what
unfoldr (const Nothing) should produce.
Structures which are guaranteed to be non-empty can instead be given
Re-exports from Data.
class Unfoldable1 :: (Type -> Type) -> Constraint
class Unfoldable1 t where
This class identifies data structures which can be unfolded.
The generating function
unfoldr1 f corresponds to the
operation of a non-empty list or array; it always returns a value, and
then optionally a value to continue unfolding from.
Note that, in order to provide an
Unfoldable1 t instance,
t need not
be a type which is guaranteed to be non-empty. For example, the fact that
lists can be empty does not prevent us from providing an
Unfoldable1 List instance. However, the result of
always be non-empty.
Every type which has an
Unfoldable instance can be given an
Unfoldable1 instance (and, in fact, is required to, because
Unfoldable1 is a superclass of
Unfoldable). However, there are types
Unfoldable1 instances but cannot have
In particular, types which are guaranteed to be non-empty, such as
NonEmptyList, cannot be given
The utility of this class, then, is that it provides an
interface while still permitting instances for guaranteed-non-empty types
replicate1A :: forall m f a. Apply m => Unfoldable1 f => Traversable1 f => Int -> m a -> m (f a)
n times (at least once, so values
than 1 will be treated as 1), and accumulate the results.
> replicate1A 2 (randomInt 1 10) :: Effect (NEL.NonEmptyList Int) (NonEmptyList (NonEmpty 8 (2 : Nil))) > replicate1A 0 (randomInt 1 10) :: Effect (NEL.NonEmptyList Int) (NonEmptyList (NonEmpty 4 Nil))
replicate1 :: forall f a. Unfoldable1 f => Int -> a -> f a
Replicate a value
n times. At least one value will be produced, so values
n less than 1 will be treated as 1.
replicate1 2 "foo" == (NEL.cons "foo" (NEL.singleton "foo") :: NEL.NonEmptyList String) replicate1 0 "foo" == (NEL.singleton "foo" :: NEL.NonEmptyList String)
range :: forall f. Unfoldable1 f => Int -> Int -> f Int
Unfoldable1 containing a range of values, including both
range 0 0 == (NEL.singleton 0 :: NEL.NonEmptyList Int) range 1 2 == (NEL.cons 1 (NEL.singleton 2) :: NEL.NonEmptyList Int) range 2 0 == (NEL.cons 2 (NEL.cons 1 (NEL.singleton 0)) :: NEL.NonEmptyList Int)
iterateN :: forall f a. Unfoldable1 f => Int -> (a -> a) -> a -> f a
Unfoldable1 by repeated application of a function to a seed value.
(iterateN 5 (_ + 1) 0 :: Array Int) == [0, 1, 2, 3, 4] (iterateN 5 (_ + 1) 0 :: NonEmptyArray Int) == NonEmptyArray [0, 1, 2, 3, 4] (iterateN 0 (_ + 1) 0 :: Array Int) ==  (iterateN 0 (_ + 1) 0 :: NonEmptyArray Int) == NonEmptyArray