Skip to content

Commit fc35f2d

Browse files
authored
Merge pull request #161 from drewolson/fix-scanrLazy
Rename scanrLazy to scanlLazy and fix arg order
2 parents 0550216 + 236f3e4 commit fc35f2d

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
@@ -742,12 +742,12 @@ foldrLazy op z = go
742742
Cons x xs' -> Z.defer \_ -> x `op` go xs'
743743
Nil -> z
744744

745-
-- | Perform a right scan lazily
746-
scanrLazy :: forall a b. (a -> b -> b) -> b -> List a -> List b
747-
scanrLazy f acc xs = List (go <$> unwrap xs)
745+
-- | Perform a left scan lazily
746+
scanlLazy :: forall a b. (b -> a -> b) -> b -> List a -> List b
747+
scanlLazy f acc xs = List (go <$> unwrap xs)
748748
where
749749
go :: Step a -> Step b
750750
go Nil = Nil
751751
go (Cons x xs') =
752-
let acc' = f x acc
753-
in Cons acc' $ scanrLazy f acc' xs'
752+
let acc' = f acc x
753+
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)