Data.Unfoldable
This module provides a type class for unfoldable functors, i.e.
functors which support an unfoldr
operation.
This allows us to unify various operations on arrays, lists, sequences, etc.
#Unfoldable
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 f
in unfoldr f
is understood as follows:
- If
f b
isNothing
, thenunfoldr f b
should be empty. - If
f b
isJust (Tuple a b1)
, thenunfoldr f b
should consist ofa
appended to the result ofunfoldr 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
Unfoldable1
instances.
Members
Instances
#replicate
replicate :: forall f a. Unfoldable f => Int -> a -> f a
Replicate a value some natural number of times. For example:
replicate 2 "foo" == (["foo", "foo"] :: Array String)
#replicateA
replicateA :: forall m f a. Applicative m => Unfoldable f => Traversable f => Int -> m a -> m (f a)
Perform an Applicative action n
times, and accumulate all the results.
> replicateA 5 (randomInt 1 10) :: Effect (Array Int)
[1,3,2,7,5]
#none
none :: forall f a. Unfoldable f => f a
The container with no elements - unfolded with zero iterations. For example:
none == ([] :: Array Unit)
#fromMaybe
fromMaybe :: forall f a. Unfoldable f => Maybe a -> f a
Convert a Maybe to any Unfoldable, such as lists or arrays.
fromMaybe (Nothing :: Maybe Int) == []
fromMaybe (Just 1) == [1]
Re-exports from Data.Unfoldable1
#Unfoldable1
class Unfoldable1 :: (Type -> Type) -> Constraint
class Unfoldable1 t where
This class identifies data structures which can be unfolded.
The generating function f
in unfoldr1 f
corresponds to the uncons
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 unfoldr1
should
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
which have Unfoldable1
instances but cannot have Unfoldable
instances.
In particular, types which are guaranteed to be non-empty, such as
NonEmptyList
, cannot be given Unfoldable
instances.
The utility of this class, then, is that it provides an Unfoldable
-like
interface while still permitting instances for guaranteed-non-empty types
like NonEmptyList
.
Members
Instances
#singleton
singleton :: forall f a. Unfoldable1 f => a -> f a
Contain a single value. For example:
singleton "foo" == (NEL.singleton "foo" :: NEL.NonEmptyList String)
#replicate1A
replicate1A :: forall m f a. Apply m => Unfoldable1 f => Traversable1 f => Int -> m a -> m (f a)
Perform an Apply
action n
times (at least once, so values n
less
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
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
range :: forall f. Unfoldable1 f => Int -> Int -> f Int
Create an Unfoldable1
containing a range of values, including both
endpoints.
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)
Modules
- Ansi.Codes
- Ansi.Output
- Ch5
- Ch6
- Ch7a
- Ch7b
- Ch9
- Control.Alt
- Control.Alternative
- Control.Applicative
- Control.Apply
- Control.Biapplicative
- Control.Biapply
- Control.Bind
- Control.Category
- Control.Comonad
- Control.Comonad.Cofree
- Control.Comonad.Cofree.Class
- Control.Comonad.Env
- Control.Comonad.Env.Class
- Control.Comonad.Env.Trans
- Control.Comonad.Store
- Control.Comonad.Store.Class
- Control.Comonad.Store.Trans
- Control.Comonad.Traced
- Control.Comonad.Traced.Class
- Control.Comonad.Traced.Trans
- Control.Comonad.Trans.Class
- Control.Extend
- Control.Lazy
- Control.Monad
- Control.Monad.Cont
- Control.Monad.Cont.Class
- Control.Monad.Cont.Trans
- Control.Monad.Error.Class
- Control.Monad.Except
- Control.Monad.Except.Trans
- Control.Monad.Fork.Class
- Control.Monad.Free
- Control.Monad.Free.Class
- Control.Monad.Gen
- Control.Monad.Gen.Class
- Control.Monad.Gen.Common
- Control.Monad.Identity.Trans
- Control.Monad.List.Trans
- Control.Monad.Maybe.Trans
- Control.Monad.Morph
- Control.Monad.RWS
- Control.Monad.RWS.Trans
- Control.Monad.Reader
- Control.Monad.Reader.Class
- Control.Monad.Reader.Trans
- Control.Monad.Rec.Class
- Control.Monad.ST
- Control.Monad.ST.Class
- Control.Monad.ST.Global
- Control.Monad.ST.Internal
- Control.Monad.ST.Ref
- Control.Monad.State
- Control.Monad.State.Class
- Control.Monad.State.Trans
- Control.Monad.Trampoline
- Control.Monad.Trans.Class
- Control.Monad.Writer
- Control.Monad.Writer.Class
- Control.Monad.Writer.Trans
- Control.MonadPlus
- Control.MonadZero
- Control.Parallel
- Control.Parallel.Class
- Control.Plus
- Control.Semigroupoid
- Data.Array
- Data.Array.NonEmpty
- Data.Array.NonEmpty.Internal
- Data.Array.Partial
- Data.Array.ST
- Data.Array.ST.Iterator
- Data.Array.ST.Partial
- Data.Bifoldable
- Data.Bifunctor
- Data.Bifunctor.Join
- Data.Bitraversable
- Data.Boolean
- Data.BooleanAlgebra
- Data.Bounded
- Data.Bounded.Generic
- Data.CatList
- Data.CatQueue
- Data.Char
- Data.Char.Gen
- Data.CommutativeRing
- Data.Comparison
- Data.Const
- Data.Coyoneda
- Data.Date
- Data.Date.Component
- Data.Date.Component.Gen
- Data.Date.Gen
- Data.DateTime
- Data.DateTime.Gen
- Data.DateTime.Instant
- Data.Decidable
- Data.Decide
- Data.Distributive
- Data.Divide
- Data.Divisible
- Data.DivisionRing
- Data.Either
- Data.Either.Inject
- Data.Either.Nested
- Data.Enum
- Data.Enum.Gen
- Data.Enum.Generic
- Data.Eq
- Data.Eq.Generic
- Data.Equivalence
- Data.EuclideanRing
- Data.Exists
- Data.Field
- Data.Foldable
- Data.FoldableWithIndex
- Data.Function
- Data.Function.Uncurried
- Data.Functor
- Data.Functor.App
- Data.Functor.Clown
- Data.Functor.Compose
- Data.Functor.Contravariant
- Data.Functor.Coproduct
- Data.Functor.Coproduct.Inject
- Data.Functor.Coproduct.Nested
- Data.Functor.Costar
- Data.Functor.Flip
- Data.Functor.Invariant
- Data.Functor.Joker
- Data.Functor.Product
- Data.Functor.Product.Nested
- Data.Functor.Product2
- Data.FunctorWithIndex
- Data.Generic.Rep
- Data.HeytingAlgebra
- Data.HeytingAlgebra.Generic
- Data.Identity
- Data.Int
- Data.Int.Bits
- Data.Interval
- Data.Interval.Duration
- Data.Interval.Duration.Iso
- Data.Lazy
- Data.List
- Data.List.Internal
- Data.List.Lazy
- Data.List.Lazy.NonEmpty
- Data.List.Lazy.Types
- Data.List.NonEmpty
- Data.List.Partial
- Data.List.Types
- Data.List.ZipList
- Data.Map
- Data.Map.Gen
- Data.Map.Internal
- Data.Maybe
- Data.Maybe.First
- Data.Maybe.Last
- Data.Monoid
- Data.Monoid.Additive
- Data.Monoid.Alternate
- Data.Monoid.Conj
- Data.Monoid.Disj
- Data.Monoid.Dual
- Data.Monoid.Endo
- Data.Monoid.Generic
- Data.Monoid.Multiplicative
- Data.NaturalTransformation
- Data.Newtype
- Data.NonEmpty
- Data.Number
- Data.Number.Approximate
- Data.Number.Format
- Data.Op
- Data.Ord
- Data.Ord.Down
- Data.Ord.Generic
- Data.Ord.Max
- Data.Ord.Min
- Data.Ordering
- Data.Predicate
- Data.Profunctor
- Data.Profunctor.Choice
- Data.Profunctor.Closed
- Data.Profunctor.Cochoice
- Data.Profunctor.Costrong
- Data.Profunctor.Join
- Data.Profunctor.Split
- Data.Profunctor.Star
- Data.Profunctor.Strong
- Data.Ring
- Data.Ring.Generic
- Data.Semigroup
- Data.Semigroup.First
- Data.Semigroup.Foldable
- Data.Semigroup.Generic
- Data.Semigroup.Last
- Data.Semigroup.Traversable
- Data.Semiring
- Data.Semiring.Generic
- Data.Set
- Data.Set.NonEmpty
- Data.Show
- Data.Show.Generic
- Data.String
- Data.String.CaseInsensitive
- Data.String.CodePoints
- Data.String.CodeUnits
- Data.String.Common
- Data.String.Gen
- Data.String.NonEmpty
- Data.String.NonEmpty.CaseInsensitive
- Data.String.NonEmpty.CodePoints
- Data.String.NonEmpty.CodeUnits
- Data.String.NonEmpty.Internal
- Data.String.Pattern
- Data.String.Regex
- Data.String.Regex.Flags
- Data.String.Regex.Unsafe
- Data.String.Unsafe
- Data.Symbol
- Data.Time
- Data.Time.Component
- Data.Time.Component.Gen
- Data.Time.Duration
- Data.Time.Duration.Gen
- Data.Time.Gen
- Data.Traversable
- Data.Traversable.Accum
- Data.Traversable.Accum.Internal
- Data.TraversableWithIndex
- Data.Tuple
- Data.Tuple.Nested
- Data.Unfoldable
- Data.Unfoldable1
- Data.Unit
- Data.Void
- Data.Yoneda
- Effect
- Effect.AVar
- Effect.Aff
- Effect.Aff.AVar
- Effect.Aff.Class
- Effect.Aff.Compat
- Effect.Class
- Effect.Class.Console
- Effect.Console
- Effect.Exception
- Effect.Exception.Unsafe
- Effect.Now
- Effect.Ref
- Effect.Uncurried
- Effect.Unsafe
- Main
- Math
- PSCI.Support
- Partial
- Partial.Unsafe
- Pipes
- Pipes.Core
- Pipes.Internal
- Pipes.ListT
- Pipes.Prelude
- Prelude
- Prim
- Prim.Boolean
- Prim.Coerce
- Prim.Ordering
- Prim.Row
- Prim.RowList
- Prim.Symbol
- Prim.TypeError
- Record.Unsafe
- Safe.Coerce
- Test.Main
- Test.Spec
- Test.Spec.Assertions
- Test.Spec.Assertions.String
- Test.Spec.Console
- Test.Spec.Reporter
- Test.Spec.Reporter.Base
- Test.Spec.Reporter.Console
- Test.Spec.Reporter.Dot
- Test.Spec.Reporter.Spec
- Test.Spec.Reporter.Tap
- Test.Spec.Result
- Test.Spec.Runner
- Test.Spec.Runner.Event
- Test.Spec.Speed
- Test.Spec.Style
- Test.Spec.Summary
- Test.Spec.Tree
- Type.Data.Row
- Type.Data.RowList
- Type.Equality
- Type.Proxy
- Unsafe.Coerce