module Precursor.Structure.Traversable
(
Traversable
, traverse
, traverse_
, sequenceA
, sequence
, sequence_
, for
, for_
, mapAccumL
, mapAccumR
, zipInto
) where
import Data.Foldable (Foldable, for_, sequenceA_,
traverse_)
import Data.Traversable hiding (sequence)
import Precursor.Control.Applicative
import Precursor.Control.Category
import Precursor.Control.Functor
import Precursor.Data.Maybe
import Precursor.Function
import Precursor.Structure.Foldable
sequence :: (Traversable t, Applicative f) => t (f a) -> f (t a)
sequence = sequenceA
sequence_ :: (Foldable t, Applicative f) => t (f a) -> f ()
sequence_ = sequenceA_
newtype List a =
List (forall b. b -> (a -> List a -> b) -> b)
newtype State s a =
State (forall c. (a -> s -> c) -> s -> c)
instance Functor (State s) where
fmap f (State m) = State (\t -> m (t . f))
instance Applicative (State s) where
pure x = State (\t -> t x)
State fs <*> State xs =
State (\t -> fs (\f -> xs (t . f)))
evalState :: State s a -> s -> a
evalState (State x) = x const
zipInto
:: (Traversable t, Foldable f)
=> (a -> Maybe b -> c)
-> t a
-> f b
-> t c
zipInto f xs =
evalState (traverse (flip fmap pop . f) xs) . foldr cons nil where
cons y ys = List (const (\g -> g y ys))
nil = List const
pop = State (\t (List l) -> l (t Nothing nil) (t . Just))