Skip to content

Instantly share code, notes, and snippets.

@tonyday567
Created March 12, 2014 23:37
Show Gist options
  • Select an option

  • Save tonyday567/9518986 to your computer and use it in GitHub Desktop.

Select an option

Save tonyday567/9518986 to your computer and use it in GitHub Desktop.
{-# LANGUAGE ExistentialQuantification, RankNTypes #-}
module Scanl where
import Control.Foldl (Fold(..))
import Data.Foldable (Foldable)
import qualified Data.Foldable as F
import Data.Traversable
scan :: (Foldable f) => Fold a b -> f a -> [b]
scan (Fold step begin done) as = F.foldr step' done' as begin'
where
step' x k z = k $! (step (head z) x:z)
done' = map done . reverse
begin' = [begin]
{-# INLINABLE scan #-}
scan' :: (Traversable t) => Fold a b -> t a -> t b
scan' (Fold step begin done) as = snd (mapAccumL step' begin as)
where
step' x a = (step x a, done (step x a))
{-# INLINABLE scan' #-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment