Skip to content

Commit f70cae6

Browse files
committed
Add scanrLazy for lazy lists
1 parent 3566228 commit f70cae6

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/Data/List/Lazy.purs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ module Data.List.Lazy
8989

9090
, foldM
9191
, foldrLazy
92+
, scanrLazy
9293

9394
, module Exports
9495
) where
@@ -755,3 +756,13 @@ foldrLazy op z = go
755756
go xs = case step xs of
756757
Cons x xs' -> Z.defer \_ -> x `op` go xs'
757758
Nil -> z
759+
760+
-- | Perform a right scan lazily
761+
scanrLazy :: forall a b. (a -> b -> b) -> b -> List a -> List b
762+
scanrLazy f acc xs = List (go <$> unwrap xs)
763+
where
764+
go :: Step a -> Step b
765+
go Nil = Nil
766+
go (Cons x xs') =
767+
let acc' = f x acc
768+
in Cons acc' $ scanrLazy f acc' xs'

test/Test/Data/List/Lazy.purs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Control.Lazy (defer)
66
import Data.FoldableWithIndex (foldMapWithIndex, foldlWithIndex, foldrWithIndex)
77
import Data.FunctorWithIndex (mapWithIndex)
88
import Data.Lazy as Z
9-
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, (!!), (..), (:), (\\))
9+
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, (!!), (..), (:), (\\))
1010
import Data.List.Lazy.NonEmpty as NEL
1111
import Data.Maybe (Maybe(..), isNothing, fromJust)
1212
import Data.Monoid.Additive (Additive(..))
@@ -394,6 +394,11 @@ testListLazy = do
394394
infs' = foldrLazy cons nil infs
395395
in take 1000 infs == take 1000 infs'
396396

397+
log "scanrLazy should work ok on infinite lists"
398+
assert let infs = iterate (_ + 1) 1
399+
infs' = scanrLazy (\i _ -> i) 0 infs
400+
in take 1000 infs == take 1000 infs'
401+
397402
log "can find the first 10 primes using lazy lists"
398403
let eratos :: List Int -> List Int
399404
eratos xs = defer \_ ->

0 commit comments

Comments
 (0)