Skip to content

Commit 236f3e4

Browse files
committed
Rename scanrLazy to scanlLazy and fix arg order
The previously-named scanrLazy was actually folding to the left. I've renamed the function and fixed the argument ordering for the folding function.
1 parent 62900a5 commit 236f3e4

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

src/Data/List/Lazy.purs

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

9090
, foldM
9191
, foldrLazy
92-
, scanrLazy
92+
, scanlLazy
9393

9494
, module Exports
9595
) where
@@ -757,12 +757,12 @@ foldrLazy op z = go
757757
Cons x xs' -> Z.defer \_ -> x `op` go xs'
758758
Nil -> z
759759

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)
760+
-- | Perform a left scan lazily
761+
scanlLazy :: forall a b. (b -> a -> b) -> b -> List a -> List b
762+
scanlLazy f acc xs = List (go <$> unwrap xs)
763763
where
764764
go :: Step a -> Step b
765765
go Nil = Nil
766766
go (Cons x xs') =
767-
let acc' = f x acc
768-
in Cons acc' $ scanrLazy f acc' xs'
767+
let acc' = f acc x
768+
in Cons acc' $ scanlLazy f acc' xs'

test/Test/Data/List/Lazy.purs

Lines changed: 6 additions & 3 deletions
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, scanrLazy, 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, scanlLazy, 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,11 +394,14 @@ 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"
397+
log "scanlLazy should work ok on infinite lists"
398398
assert let infs = iterate (_ + 1) 1
399-
infs' = scanrLazy (\i _ -> i) 0 infs
399+
infs' = scanlLazy (\_ i -> i) 0 infs
400400
in take 1000 infs == take 1000 infs'
401401

402+
log "scanlLazy folds to the left"
403+
assert $ scanlLazy (+) 5 (1..4) == l [6, 8, 11, 15]
404+
402405
log "can find the first 10 primes using lazy lists"
403406
let eratos :: List Int -> List Int
404407
eratos xs = defer \_ ->

0 commit comments

Comments
 (0)