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 16, 2024
1 parent 68c8de2 commit 4e7b848
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions core/src/Streamly/Internal/Data/Scanl/Window.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ module Streamly.Internal.Data.Scanl.Window
-- @(Insert a)@ indicates that the input element @a@ is being inserted in
-- the window without ejecting an old value, increasing the window size by
-- 1. An input of type @(Replace a a)@ indicates that the first argument of
-- Replace is being inserted in the window and the second argument is being
-- removed from the window, the window size remains the same. The window
-- Replace is being removed from the window and the second argument is being
-- inserted in the window, the window size remains the same. The window
-- size can only increase and never decrease.
--
-- You can compute the statistics over the entire stream using window folds
Expand Down Expand Up @@ -102,15 +102,14 @@ import Prelude hiding (length, sum, minimum, maximum)
--
-- Replace can be implemented using Insert and Delete.
--
-- XXX Use "Replace old new" instead.

-- | Represents incremental input for a scan. 'Insert' means a new element is
-- being added to the collection, 'Replace' means an old value in the
-- collection is being replaced with a new value.
data Incr a =
Insert !a
-- | Delete !a
| Replace !a !a -- ^ Replace new old
| Replace !a !a -- ^ Replace old new

instance Functor Incr where
fmap f (Insert x) = Insert (f x)
Expand Down Expand Up @@ -164,7 +163,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 (Replace old a, rb1)
return $
case r of
Partial s -> Partial $ SWRing rb1 s
Expand Down Expand Up @@ -205,7 +204,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 (Replace old a, rb1)
return $
case r of
Partial s -> Partial $ SWRing mba rh1 s
Expand Down Expand Up @@ -252,7 +251,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 _ (Replace old new) = f (Just old) new

-- | Apply a pure function on the latest and the oldest element of the window.
--
Expand All @@ -269,7 +268,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 _ (Replace old new) = f (Just old) new

-------------------------------------------------------------------------------
-- Sum
Expand All @@ -296,7 +295,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 (Replace old new) = return $ Partial (s + new - old)

extract = return

Expand Down Expand Up @@ -353,7 +352,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) (Replace old new) =
-- XXX if (new - old) is large we may lose err
let incr = (new - old) - err
in add total incr
Expand Down

0 comments on commit 4e7b848

Please sign in to comment.