Skip to content

Commit

Permalink
Merge pull request #155 from drewolson/scanr-lazy
Browse files Browse the repository at this point in the history
Add scanrLazy for lazy lists
  • Loading branch information
garyb committed Feb 9, 2019
2 parents 3566228 + f70cae6 commit 6629e0c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/Data/List/Lazy.purs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ module Data.List.Lazy

, foldM
, foldrLazy
, scanrLazy

, module Exports
) where
Expand Down Expand Up @@ -755,3 +756,13 @@ foldrLazy op z = go
go xs = case step xs of
Cons x xs' -> Z.defer \_ -> x `op` go xs'
Nil -> z

-- | Perform a right scan lazily
scanrLazy :: forall a b. (a -> b -> b) -> b -> List a -> List b
scanrLazy f acc xs = List (go <$> unwrap xs)
where
go :: Step a -> Step b
go Nil = Nil
go (Cons x xs') =
let acc' = f x acc
in Cons acc' $ scanrLazy f acc' xs'
7 changes: 6 additions & 1 deletion test/Test/Data/List/Lazy.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Control.Lazy (defer)
import Data.FoldableWithIndex (foldMapWithIndex, foldlWithIndex, foldrWithIndex)
import Data.FunctorWithIndex (mapWithIndex)
import Data.Lazy as Z
import Data.List.Lazy (List, Pattern(..), alterAt, catMaybes, concat, concatMap, cons, delete, deleteAt, deleteBy, drop, dropWhile, elemIndex, elemLastIndex, filter, filterM, findIndex, findLastIndex, foldM, foldMap, foldl, foldr, foldrLazy, fromFoldable, group, groupBy, head, init, insert, insertAt, insertBy, intersect, intersectBy, iterate, last, length, mapMaybe, modifyAt, nil, nub, nubBy, null, partition, range, repeat, replicate, replicateM, reverse, singleton, slice, snoc, span, stripPrefix, tail, take, takeWhile, transpose, uncons, union, unionBy, unzip, updateAt, zip, zipWith, zipWithA, (!!), (..), (:), (\\))
import Data.List.Lazy (List, Pattern(..), alterAt, catMaybes, concat, concatMap, cons, delete, deleteAt, deleteBy, drop, dropWhile, elemIndex, elemLastIndex, filter, filterM, findIndex, findLastIndex, foldM, foldMap, foldl, foldr, foldrLazy, fromFoldable, group, groupBy, head, init, insert, insertAt, insertBy, intersect, intersectBy, iterate, last, length, mapMaybe, modifyAt, nil, nub, nubBy, null, partition, range, repeat, replicate, replicateM, reverse, scanrLazy, singleton, slice, snoc, span, stripPrefix, tail, take, takeWhile, transpose, uncons, union, unionBy, unzip, updateAt, zip, zipWith, zipWithA, (!!), (..), (:), (\\))
import Data.List.Lazy.NonEmpty as NEL
import Data.Maybe (Maybe(..), isNothing, fromJust)
import Data.Monoid.Additive (Additive(..))
Expand Down Expand Up @@ -394,6 +394,11 @@ testListLazy = do
infs' = foldrLazy cons nil infs
in take 1000 infs == take 1000 infs'

log "scanrLazy should work ok on infinite lists"
assert let infs = iterate (_ + 1) 1
infs' = scanrLazy (\i _ -> i) 0 infs
in take 1000 infs == take 1000 infs'

log "can find the first 10 primes using lazy lists"
let eratos :: List Int -> List Int
eratos xs = defer \_ ->
Expand Down

0 comments on commit 6629e0c

Please sign in to comment.