Commit 9e724f6e authored by vdukhovni's avatar vdukhovni Committed by Marge Bot
Browse files

Small ZipList optimisation

In (<|>) for ZipList, avoid processing the first argument twice (both as first
argument of (++) and for its length in drop count of the second argument).

Previously, the entire first argument was forced into memory, now (<|>) can run
in constant space even with long inputs.
parent 40c0f67f
Pipeline #36603 passed with stages
in 273 minutes and 32 seconds
......@@ -60,7 +60,7 @@ import Data.Functor.Const (Const(..))
import GHC.Base
import GHC.Generics
import GHC.List (repeat, zipWith, drop)
import GHC.List (repeat, zipWith)
import GHC.Read (Read)
import GHC.Show (Show)
......@@ -140,7 +140,11 @@ instance Applicative ZipList where
-- | @since 4.11.0.0
instance Alternative ZipList where
empty = ZipList []
ZipList xs <|> ZipList ys = ZipList (xs ++ drop (length xs) ys)
ZipList xs0 <|> ZipList ys0 = ZipList $ go xs0 ys0
where
go (x:xs) (_:ys) = x : go xs ys
go [] ys = ys
go xs _ = xs
-- extra functions
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment