@@ -1055,39 +1055,55 @@ concatPairsWith
1055 -> (a -> Stream m b)
1055 -> (a -> Stream m b)
1056 -> Stream m a
1056 -> Stream m a
1057 -> Stream m b
1057 -> Stream m b
1058−concatPairsWith combine f = go Nothing
1058+concatPairsWith combine f str = go (leafPairs str)
1062− go Nothing stream =
1062+ go stream =
1063 mkStream $ \st yld sng stp ->
1063 mkStream $ \st yld sng stp ->
1064 let foldShared = foldStreamShared st yld sng stp
1064 let foldShared = foldStreamShared st yld sng stp
1065− single a = foldShared $ unShare (f a)
1065+ single a = foldShared $ unShare a
1066− yieldk a r = foldShared $ go (Just a) r
1066+ yieldk a r = foldShared $ go1 a r
1067 in foldStream (adaptState st) yieldk single stp stream
1067 in foldStream (adaptState st) yieldk single stp stream
1068− go (Just a1) stream =
1069+ go1 a1 stream =
1069 mkStream $ \st yld sng stp ->
1070 mkStream $ \st yld sng stp ->
1070 let foldShared = foldStreamShared st yld sng stp
1071 let foldShared = foldStreamShared st yld sng stp
1071− stop = foldShared $ unShare (f a1)
1072+ stop = foldShared $ unShare a1
1072− single a = foldShared $ unShare (f a1) `combine` f a
1073+ single a = foldShared $ unShare a1 `combine` a
1073 yieldk a r =
1074 yieldk a r =
1074− foldShared
1075+ foldShared $ go $ combine a1 a `cons` nonLeafPairs r
1075− $ concatPairsWith combine
1076− (\(x,y) -> combine (unShare x) y)
1077− $ (f a1, f a) `cons` makePairs Nothing r
1078 in foldStream (adaptState st) yieldk single stop stream
1076 in foldStream (adaptState st) yieldk single stop stream
1080− makePairs Nothing stream =
1078+ -- Exactly the same as "go" except that stop continuation extracts the
1081 mkStream $ \st yld sng stp ->
1081 mkStream $ \st yld sng stp ->
1082 let foldShared = foldStreamShared st yld sng stp
1082 let foldShared = foldStreamShared st yld sng stp
1083− single a = sng (f a, nil)
1083+ single a = sng (f a)
1084− yieldk a r = foldShared $ makePairs (Just a) r
1084+ yieldk a r = foldShared $ leafPairs1 a r
1085 in foldStream (adaptState st) yieldk single stp stream
1085 in foldStream (adaptState st) yieldk single stp stream
1086− makePairs (Just a1) stream =
1086+
1087+ leafPairs1 a1 stream =
1088+ mkStream $ \st yld sng _ ->
1089+ let stop = sng (f a1)
1090+ single a = sng (f a1 `combine` f a)
1091+ yieldk a r = yld (f a1 `combine` f a) $ leafPairs r
1092+ in foldStream (adaptState st) yieldk single stop stream
1094+ -- Exactly the same as "leafPairs" except that it does not map "f"
1095+ nonLeafPairs stream =
1096+ mkStream $ \st yld sng stp ->
1097+ let foldShared = foldStreamShared st yld sng stp
1099+ yieldk a r = foldShared $ nonLeafPairs1 a r
1100+ in foldStream (adaptState st) yieldk single stp stream
1102+ nonLeafPairs1 a1 stream =
1087 mkStream $ \st yld sng _ ->
1103 mkStream $ \st yld sng _ ->
1088− let stop = sng (f a1, nil)
1104+ let stop = sng a1
1089− single a = sng (f a1, f a)
1105+ single a = sng (a1 `combine` a)
1090− yieldk a r = yld (f a1, f a) (makePairs Nothing r)
1106+ yieldk a r = yld (a1 `combine` a) $ nonLeafPairs r
1091 in foldStream (adaptState st) yieldk single stop stream
1107 in foldStream (adaptState st) yieldk single stop stream
1093instance Monad m => Applicative (Stream m) where
1109instance Monad m => Applicative (Stream m) where