Created
March 12, 2014 23:37
-
-
Save tonyday567/9518986 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-# 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