Skip to content

Rename scanrLazy to scanlLazy and fix arg order #161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/Data/List/Lazy.purs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ module Data.List.Lazy

, foldM
, foldrLazy
, scanrLazy
, scanlLazy

, module Exports
) where
Expand Down Expand Up @@ -757,12 +757,12 @@ foldrLazy op z = go
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)
-- | Perform a left scan lazily
scanlLazy :: forall a b. (b -> a -> b) -> b -> List a -> List b
scanlLazy 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'
let acc' = f acc x
in Cons acc' $ scanlLazy f acc' xs'
9 changes: 6 additions & 3 deletions 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, scanrLazy, 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, scanlLazy, 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,11 +394,14 @@ testListLazy = do
infs' = foldrLazy cons nil infs
in take 1000 infs == take 1000 infs'

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

log "scanlLazy folds to the left"
assert $ scanlLazy (+) 5 (1..4) == l [6, 8, 11, 15]

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