Skip to content

Commit

Permalink
Flip the argument order of Replace in window folds
Browse files Browse the repository at this point in the history
  • Loading branch information
adithyaov committed Dec 14, 2024
1 parent 007fe33 commit 3f46653
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions core/src/Streamly/Internal/Data/Scanl/Window.hs
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ import Prelude hiding (length, sum, minimum, maximum)
data Incr a =
Insert !a
-- | Delete !a
| Replace !a !a -- ^ Replace new old
| Replace1 !a !a -- ^ Replace old new

instance Functor Incr where
fmap f (Insert x) = Insert (f x)
-- fmap f (Delete x) = Delete (f x)
fmap f (Replace x y) = Replace (f x) (f y)
fmap f (Replace1 x y) = Replace1 (f x) (f y)

-------------------------------------------------------------------------------
-- Utilities
Expand Down Expand Up @@ -164,7 +164,7 @@ incrScanWith n (Scanl step1 initial1 extract1 final1) =

step (SWRing rb st) a = do
(rb1, old) <- RingArray.replace rb a
r <- step1 st (Replace a old, rb1)
r <- step1 st (Replace1 old a, rb1)
return $
case r of
Partial s -> Partial $ SWRing rb1 s
Expand Down Expand Up @@ -205,7 +205,7 @@ incrScanWith n (Scanl step1 initial1 extract1 final1) =
step (SWRing mba rh st) a = do
(rb1@(RingArray _ _ rh1), old) <-
RingArray.insert (RingArray mba (n * SIZE_OF(a)) rh) a
r <- step1 st (Replace a old, rb1)
r <- step1 st (Replace1 old a, rb1)
return $
case r of
Partial s -> Partial $ SWRing mba rh1 s
Expand Down Expand Up @@ -252,7 +252,7 @@ incrRollingMapM f = Scanl.mkScanlM f1 initial

f1 _ (Insert a) = f Nothing a
-- f1 _ (Delete _) = return Nothing
f1 _ (Replace x y) = f (Just y) x
f1 _ (Replace1 old new) = f (Just old) new

-- | Apply a pure function on the latest and the oldest element of the window.
--
Expand All @@ -269,7 +269,7 @@ incrRollingMap f = Scanl.mkScanl f1 initial

f1 _ (Insert a) = f Nothing a
-- f1 _ (Delete _) = Nothing
f1 _ (Replace x y) = f (Just y) x
f1 _ (Replace1 old new) = f (Just old) new

-------------------------------------------------------------------------------
-- Sum
Expand All @@ -296,7 +296,7 @@ incrSumInt = Scanl step initial extract extract

step s (Insert a) = return $ Partial (s + a)
-- step s (Delete a) = return $ Partial (s - a)
step s (Replace new old) = return $ Partial (s + new - old)
step s (Replace1 old new) = return $ Partial (s + new - old)

extract = return

Expand Down Expand Up @@ -353,7 +353,7 @@ incrSum = Scanl step initial extract extract
let incr = -new - err
in add total incr
-}
step (Tuple' total err) (Replace new old) =
step (Tuple' total err) (Replace1 old new) =
-- XXX if (new - old) is large we may lose err
let incr = (new - old) - err
in add total incr
Expand All @@ -374,7 +374,7 @@ incrCount = Scanl.mkScanl step 0

step w (Insert _) = w + 1
-- step w (Delete _) = w - 1
step w (Replace _ _) = w
step w (Replace1 _ _) = w

-- | Sum of the \(k\)th power of all the elements in a rolling window:
--
Expand Down

0 comments on commit 3f46653

Please sign in to comment.