on 209e990 help sign in
749 lines 25 kB history raw
1{-# OPTIONS_GHC -Wno-deprecations #-}
3-- |
4-- Module : Streamly.Internal.Data.Stream.IsStream.Top
5-- Copyright : (c) 2020 Composewell Technologies
6-- License : BSD-3-Clause
7-- Maintainer : streamly@composewell.com
8-- Stability : experimental
9-- Portability : GHC
10--
11-- Top level IsStream module that can use all other lower level IsStream
12-- modules.
14module Streamly.Internal.Data.Stream.IsStream.Top {-# DEPRECATED "Please use \"Streamly.Data.Stream.*\" instead." #-}
15 (
16 -- * Transformation
17 -- ** Sampling
18 -- | Value agnostic filtering.
19 sampleFromThen
20 , sampleIntervalStart
21 , sampleIntervalEnd
22 , sampleBurstStart
23 , sampleBurstEnd
25 -- ** Reordering
26 , sortBy
28 -- * Nesting
29 -- ** Set like operations
30 -- | These are not exactly set operations because streams are not
31 -- necessarily sets, they may have duplicated elements.
32 , intersectBy
33 , intersectBySorted
34 , differenceBy
35 , mergeDifferenceBy
36 , unionBy
37 , mergeUnionBy
39 -- ** Join operations
40 , crossJoin
41 , joinInner
42 , joinInnerMap
43 , joinInnerMerge
44 -- , joinLeft
45 , mergeLeftJoin
46 , joinLeftMap
47 -- , joinOuter
48 , mergeOuterJoin
49 , joinOuterMap
50 )
51where
53#include "inline.hs"
55import Control.Monad.Catch (MonadCatch)
56import Control.Monad.IO.Class (MonadIO(..))
57-- import Control.Monad.Trans.Class (lift)
58-- import Control.Monad.Trans.State.Strict (get, put)
59-- import Data.Function ((&))
60import Data.IORef (newIORef, readIORef, modifyIORef')
61import Data.Kind (Type)
62-- import Data.Maybe (isJust)
63import Streamly.Internal.Control.Concurrent (MonadAsync)
64import Streamly.Internal.Data.Stream.IsStream.Common (concatM)
65import Streamly.Internal.Data.Stream.IsStream.Type
66 (IsStream(..), adapt, foldl', fromList)
67import Streamly.Internal.Data.Stream.Serial (SerialT)
68import Streamly.Internal.Data.Time.Units (NanoSecond64(..), toRelTime64)
70import qualified Data.List as List
71import qualified Data.Map.Strict as Map
72-- import qualified Streamly.Internal.Data.Array.Generic as Array
73-- (fromStream, length, read)
74-- import qualified Streamly.Data.Array.Mut as MA
75import qualified Streamly.Internal.Data.Fold as Fold
76import qualified Streamly.Internal.Data.Parser as Parser
77 (groupByRollingEither)
78-- import qualified Streamly.Internal.Data.Stream.IsStream.Lift as Stream
79import qualified Streamly.Internal.Data.Stream.IsStream.Eliminate as Stream
80import qualified Streamly.Internal.Data.Stream.IsStream.Generate as Stream
81import qualified Streamly.Internal.Data.Stream.IsStream.Expand as Stream
82import qualified Streamly.Internal.Data.Stream.IsStream.Reduce as Stream
83import qualified Streamly.Internal.Data.Stream.IsStream.Transform as Stream
84import qualified Streamly.Internal.Data.Stream.IsStream.Type as IsStream
85import qualified Streamly.Internal.Data.Stream as StreamD
87import Prelude hiding (Foldable(..), filter, zipWith, concatMap, concat)
89-- $setup
90-- >>> :m
91-- >>> :set -fno-warn-deprecations
92-- >>> import Prelude hiding (filter, zipWith, concatMap, concat)
93-- >>> import qualified Streamly.Prelude as Stream
94-- >>> import qualified Streamly.Internal.Data.Stream.IsStream as Stream
96------------------------------------------------------------------------------
97-- Sampling
98------------------------------------------------------------------------------
100-- XXX We can implement this using addition instead of "mod" to make it more
101-- efficient.
102--
103-- | @sampleFromthen offset stride@ samples the element at @offset@ index and
104-- then every element at strides of @stride@.
105--
106-- >>> Stream.toList $ Stream.sampleFromThen 2 3 $ Stream.enumerateFromTo 0 10
107-- [2,5,8]
108--
109-- /Pre-release/
110--
111{-# INLINE sampleFromThen #-}
112sampleFromThen :: (IsStream t, Monad m, Functor (t m)) =>
113 Int -> Int -> t m a -> t m a
114sampleFromThen offset stride =
115 Stream.with Stream.indexed Stream.filter
116 (\(i, _) -> i >= offset && (i - offset) `mod` stride == 0)
118-- | Continuously evaluate the input stream and sample the last event in time
119-- window of @n@ seconds.
120--
121-- This is also known as @throttle@ in some libraries.
122--
123-- @
124-- sampleIntervalEnd n = Stream.catMaybes . Stream.intervalsOf n Fold.last
125-- @
126--
127-- /Pre-release/
128--
129{-# INLINE sampleIntervalEnd #-}
130sampleIntervalEnd :: (IsStream t, MonadAsync m, Functor (t m)) =>
131 Double -> t m a -> t m a
132sampleIntervalEnd n = Stream.catMaybes . Stream.intervalsOf n Fold.last
134-- | Like 'sampleInterval' but samples at the beginning of the time window.
135--
136-- @
137-- sampleIntervalStart n = Stream.catMaybes . Stream.intervalsOf n Fold.one
138-- @
139--
140-- /Pre-release/
141--
142{-# INLINE sampleIntervalStart #-}
143sampleIntervalStart :: (IsStream t, MonadAsync m, Functor (t m)) =>
144 Double -> t m a -> t m a
145sampleIntervalStart n = Stream.catMaybes . Stream.intervalsOf n Fold.one
147data BurstState t x =
148 BurstNone
149 | BurstWait !t !x
150 | BurstDone !x
151 | BurstDoneNext !x !t !x
153{-# INLINE sampleBurst #-}
154sampleBurst :: (IsStream t, MonadAsync m, Functor (t m)) =>
155 Bool -> Double -> t m a -> t m a
156sampleBurst sampleAtEnd gap xs =
157 -- XXX Ideally we should schedule a timer event exactly after gap time,
158 -- but the tick stream should work well as long as the timer
159 -- granularity is small enough compared to the gap.
160 Stream.mapMaybe extract
161 $ Stream.scanl' step BurstNone
162 $ Stream.timeIndexed
163 $ Stream.interjectSuffix 0.01 (return Nothing) (Stream.map Just xs)
165 where
167 gap1 = toRelTime64 (NanoSecond64 (round (gap * 10^(9::Int))))
169 {-# INLINE step #-}
170 step BurstNone (t1, Just x1) = BurstWait t1 x1
171 step BurstNone _ = BurstNone
173 step (BurstDone _) (t1, Just x1) = BurstWait t1 x1
174 step (BurstDone _) _ = BurstNone
176 step old@(BurstWait t0 x0) (t1, Nothing)
177 | t1 - t0 >= gap1 = BurstDone x0
178 | otherwise = old
179 -- This can happen due to scheduling delays, if we received back to
180 -- back events spaced by more than the timeout without an
181 -- intervening timeout event then we emit the old event instead of
182 -- replacing it by the new.
183 step (BurstWait t0 x0) (t1, Just x1)
184 | t1 - t0 >= gap1 = BurstDoneNext x0 t1 x1
185 | sampleAtEnd = BurstWait t1 x1
186 | otherwise = BurstWait t1 x0
188 step (BurstDoneNext _ t0 x0) (t1, Nothing)
189 | t1 - t0 >= gap1 = BurstDone x0
190 | otherwise = BurstWait t0 x0
191 step (BurstDoneNext _ t0 x0) (t1, Just x1)
192 | t1 - t0 >= gap1 = BurstDoneNext x0 t1 x1
193 | sampleAtEnd = BurstWait t1 x1
194 | otherwise = BurstWait t1 x0
196 {-# INLINE extract #-}
197 extract (BurstDoneNext x _ _) = Just x
198 extract (BurstDone x) = Just x
199 extract _ = Nothing
201-- | Sample one event at the end of each burst of events. A burst is a group
202-- of events close together in time, it ends when an event is spaced by more
203-- than the specified time interval (in seconds) from the previous event.
204--
205-- This is known as @debounce@ in some libraries.
206--
207-- The clock granularity is 10 ms.
208--
209-- /Pre-release/
210--
211{-# INLINE sampleBurstEnd #-}
212sampleBurstEnd :: (IsStream t, MonadAsync m, Functor (t m)) =>
213 Double -> t m a -> t m a
214sampleBurstEnd = sampleBurst True
216-- | Like 'sampleBurstEnd' but samples the event at the beginning of the burst
217-- instead of at the end of it.
218--
219-- /Pre-release/
220--
221{-# INLINE sampleBurstStart #-}
222sampleBurstStart :: (IsStream t, MonadAsync m, Functor (t m)) =>
223 Double -> t m a -> t m a
224sampleBurstStart = sampleBurst False
226------------------------------------------------------------------------------
227-- Reordering
228------------------------------------------------------------------------------
229--
230-- We could possibly choose different algorithms depending on whether the
231-- input stream is almost sorted (ascending/descending) or random. We could
232-- serialize the stream to an array and use quicksort.
233--
234-- | Sort the input stream using a supplied comparison function.
235--
236-- /O(n) space/
237--
238-- Note: this is not the fastest possible implementation as of now.
239--
240-- /Pre-release/
241--
242{-# INLINE sortBy #-}
243sortBy :: MonadCatch m => (a -> a -> Ordering) -> SerialT m a -> SerialT m a
244-- sortBy f = Stream.concatPairsWith (Stream.mergeBy f) Stream.fromPure
245sortBy cmp =
246 let p =
247 Parser.groupByRollingEither
248 (\x -> (< GT) . cmp x)
249 (fmap fromStream Fold.toStreamKRev)
250 (fmap fromStream Fold.toStreamK)
251 in Stream.concatPairsWith (Stream.mergeBy cmp) id
252 . Stream.rights . Stream.parseMany (fmap (either id id) p)
254------------------------------------------------------------------------------
255-- SQL Joins
256------------------------------------------------------------------------------
257--
258-- Some references:
259-- * https://en.wikipedia.org/wiki/Relational_algebra
260-- * https://en.wikipedia.org/wiki/Join_(SQL)
262-- TODO: OrdSet/IntSet/hashmap based versions of these. With Eq only
263-- constraint, the best would be to use an Array with linear search. If the
264-- second stream is sorted we can also use a binary search, using Ord
265-- constraint or an ordering function.
266--
267-- For Storables we can cache the second stream into an unboxed array for
268-- possibly faster access/compact representation?
269--
270-- If we do not want to keep the stream in memory but always read it from the
271-- source (disk/network) every time we iterate through it then we can do that
272-- too by reading the stream every time, the stream must have immutable state
273-- in that case and the user is responsible for the behavior if the stream
274-- source changes during iterations. We can also use an Unfold instead of
275-- stream. We probably need a way to distinguish streams that can be read
276-- mutliple times without any interference (e.g. unfolding a stream using an
277-- immutable handle would work i.e. using pread/pwrite instead of maintianing
278-- an offset in the handle).
280-- XXX We can do this concurrently.
281--
282-- | This is the same as 'Streamly.Internal.Data.Unfold.outerProduct' but less
283-- efficient.
284--
285-- The second stream is evaluated multiple times. If the second stream is
286-- consume-once stream then it can be cached in an 'Data.Array.Array' before
287-- calling this function. Caching may also improve performance if the stream is
288-- expensive to evaluate.
289--
290-- Time: O(m x n)
291--
292-- /Pre-release/
293{-# INLINE crossJoin #-}
294crossJoin :: Monad (t m) => t m a -> t m b -> t m (a, b)
295crossJoin s1 s2 = do
296 -- XXX use concatMap instead?
297 a <- s1
298 b <- s2
299 return (a, b)
301-- XXX We can do this concurrently.
302-- XXX If the second stream is sorted and passed as an Array we could use
303-- binary search if we have an Ord instance or Ordering returning function. The
304-- time complexity would then become (m x log n).
305--
306-- | For all elements in @t m a@, for all elements in @t m b@ if @a@ and @b@
307-- are equal by the given equality pedicate then return the tuple (a, b).
308--
309-- The second stream is evaluated multiple times. If the stream is a
310-- consume-once stream then the caller should cache it (e.g. in a
311-- 'Data.Array.Array') before calling this function. Caching may also improve
312-- performance if the stream is expensive to evaluate.
313--
314-- For space efficiency use the smaller stream as the second stream.
315--
316-- You should almost always use joinInnerMap instead of joinInner. joinInnerMap
317-- is an order of magnitude faster. joinInner may be used when the second
318-- stream is generated from a seed, therefore, need not be stored in memory and
319-- the amount of memory it takes is a concern.
320--
321-- Space: O(n) assuming the second stream is cached in memory.
322--
323-- Time: O(m x n)
324--
325-- /Pre-release/
326{-# INLINE joinInner #-}
327joinInner ::
328 forall (t :: (Type -> Type) -> Type -> Type) m a b.
329 (IsStream t, Monad m) =>
330 (a -> b -> Bool) -> t m a -> t m b -> t m (a, b)
331joinInner eq s1 s2 = do
332 -- ConcatMap works faster than bind
333 Stream.concatMap (\a ->
334 Stream.concatMap (\b ->
335 if a `eq` b
336 then Stream.fromPure (a, b)
337 else Stream.nil
338 ) s2
339 ) s1
341-- XXX Generate error if a duplicate insertion is attempted?
342toMap :: (Monad m, Ord k) => IsStream.SerialT m (k, v) -> m (Map.Map k v)
343toMap = Stream.foldl' (\kv (k, b) -> Map.insert k b kv) Map.empty
345-- If the second stream is too big it can be partitioned based on hashes and
346-- then we can process one parition at a time.
347--
348-- XXX An IntMap may be faster when the keys are Int.
349-- XXX Use hashmap instead of map?
350--
351-- | Like 'joinInner' but uses a 'Map' for efficiency.
352--
353-- If the input streams have duplicate keys, the behavior is undefined.
354--
355-- For space efficiency use the smaller stream as the second stream.
356--
357-- Space: O(n)
358--
359-- Time: O(m + n)
360--
361-- /Pre-release/
362{-# INLINE joinInnerMap #-}
363joinInnerMap :: (IsStream t, Monad m, Ord k) =>
364 t m (k, a) -> t m (k, b) -> t m (k, a, b)
365joinInnerMap s1 s2 =
366 Stream.concatM $ do
367 km <- toMap $ IsStream.adapt s2
368 pure $ Stream.mapMaybe (joinAB km) s1
370 where
372 joinAB kvm (k, a) =
373 case k `Map.lookup` kvm of
374 Just b -> Just (k, a, b)
375 Nothing -> Nothing
377-- | Like 'joinInner' but works only on sorted streams.
378--
379-- Space: O(1)
380--
381-- Time: O(m + n)
382--
383-- /Unimplemented/
384{-# INLINE joinInnerMerge #-}
385joinInnerMerge :: (a -> b -> Ordering) -> t m a -> t m b -> t m (a, b)
386joinInnerMerge = undefined
388{-
389-- XXX We can do this concurrently.
390-- XXX If the second stream is sorted and passed as an Array or a seek capable
391-- stream then we could use binary search if we have an Ord instance or
392-- Ordering returning function. The time complexity would then become (m x log
393-- n).
394--
395-- | For all elements in @t m a@, for all elements in @t m b@ if @a@ and @b@
396-- are equal then return the tuple @(a, Just b)@. If @a@ is not present in @t
397-- m b@ then return @(a, Nothing)@.
398--
399-- The second stream is evaluated multiple times. If the stream is a
400-- consume-once stream then the caller should cache it in an 'Data.Array.Array'
401-- before calling this function. Caching may also improve performance if the
402-- stream is expensive to evaluate.
403--
404-- @
405-- rightJoin = flip joinLeft
406-- @
407--
408-- Space: O(n) assuming the second stream is cached in memory.
409--
410-- Time: O(m x n)
411--
412-- /Unimplemented/
413{-# INLINE joinLeft #-}
414joinLeft :: Monad m =>
415 (a -> b -> Bool) -> SerialT m a -> SerialT m b -> SerialT m (a, Maybe b)
416joinLeft eq s1 s2 = Stream.evalStateT (return False) $ do
417 a <- Stream.liftInner s1
418 -- XXX should we use StreamD monad here?
419 -- XXX Is there a better way to perform some action at the end of a loop
420 -- iteration?
421 lift $ put False
422 let final = do
423 r <- lift get
424 if r
425 then Stream.nil
426 else Stream.fromPure Nothing
427 b <- fmap Just (Stream.liftInner s2) <> final
428 case b of
429 Just b1 ->
430 if a `eq` b1
431 then do
432 lift $ put True
433 return (a, Just b1)
434 else Stream.nil
435 Nothing -> return (a, Nothing)
436-}
438-- | Like 'joinLeft' but uses a hashmap for efficiency.
439--
440-- Space: O(n)
441--
442-- Time: O(m + n)
443--
444-- /Pre-release/
445{-# INLINE joinLeftMap #-}
446joinLeftMap :: (IsStream t, Ord k, Monad m) =>
447 t m (k, a) -> t m (k, b) -> t m (k, a, Maybe b)
448joinLeftMap s1 s2 =
449 Stream.concatM $ do
450 km <- toMap $ IsStream.adapt s2
451 return $ Stream.map (joinAB km) s1
453 where
455 joinAB km (k, a) =
456 case k `Map.lookup` km of
457 Just b -> (k, a, Just b)
458 Nothing -> (k, a, Nothing)
460-- | Like 'joinLeft' but works only on sorted streams.
461--
462-- Space: O(1)
463--
464-- Time: O(m + n)
465--
466-- /Unimplemented/
467{-# INLINE mergeLeftJoin #-}
468mergeLeftJoin :: -- Monad m =>
469 (a -> b -> Ordering) -> t m a -> t m b -> t m (a, Maybe b)
470mergeLeftJoin _eq _s1 _s2 = undefined
472{-
473-- XXX We can do this concurrently.
474--
475-- | For all elements in @t m a@, for all elements in @t m b@ if @a@ and @b@
476-- are equal by the given equality pedicate then return the tuple (Just a, Just
477-- b). If @a@ is not found in @t m b@ then return (a, Nothing), return
478-- (Nothing, b) for vice-versa.
479--
480-- For space efficiency use the smaller stream as the second stream.
481--
482-- Space: O(n)
483--
484-- Time: O(m x n)
485--
486-- /Unimplemented/
487{-# INLINE joinOuter #-}
488joinOuter :: MonadIO m =>
489 (a -> b -> Bool)
490 -> SerialT m a
491 -> SerialT m b
492 -> SerialT m (Maybe a, Maybe b)
493joinOuter eq s1 s =
494 Stream.concatM $ do
495 inputArr <- Array.fromStream s
496 let len = Array.length inputArr
497 foundArr <-
498 Stream.fold
499 (MA.writeN len)
500 (Stream.fromList (Prelude.replicate len False))
501 return $ go inputArr foundArr <> leftOver inputArr foundArr
503 where
505 leftOver inputArr foundArr =
506 let stream1 = IsStream.fromSerial $ Array.read inputArr
507 stream2 = Stream.unfold MA.reader foundArr
508 in Stream.filter
509 isJust
510 ( Stream.zipWith (\x y ->
511 if y
512 then Nothing
513 else Just (Nothing, Just x)
514 ) stream1 stream2
515 ) & Stream.catMaybes
517 go inputArr foundArr = Stream.evalStateT (return False) $ do
518 a <- Stream.liftInner s1
519 -- XXX should we use StreamD monad here?
520 -- XXX Is there a better way to perform some action at the end of a loop
521 -- iteration?
522 lift $ put False
523 let final = do
524 r <- lift get
525 if r
526 then Stream.nil
527 else Stream.fromPure Nothing
528 (i, b) <-
529 let stream = IsStream.fromSerial $ Array.read inputArr
530 in Stream.indexed $ fmap Just (Stream.liftInner stream) <> final
532 case b of
533 Just b1 ->
534 if a `eq` b1
535 then do
536 lift $ put True
537 MA.putIndex i True foundArr
538 return (Just a, Just b1)
539 else Stream.nil
540 Nothing -> return (Just a, Nothing)
541-}
543-- Put the b's that have been paired, in another hash or mutate the hash to set
544-- a flag. At the end go through @t m b@ and find those that are not in that
545-- hash to return (Nothing, b).
546--
547-- | Like 'joinOuter' but uses a 'Map' for efficiency.
548--
549-- Space: O(m + n)
550--
551-- Time: O(m + n)
552--
553-- /Pre-release/
554{-# INLINE joinOuterMap #-}
555joinOuterMap ::
556 (IsStream t, Ord k, MonadIO m) =>
557 t m (k, a) -> t m (k, b) -> t m (k, Maybe a, Maybe b)
558joinOuterMap s1 s2 =
559 Stream.concatM $ do
560 km1 <- kvFold $ IsStream.adapt s1
561 km2 <- kvFold $ IsStream.adapt s2
563 -- XXX Not sure if toList/fromList would fuse optimally. We may have to
564 -- create a fused Map.toStream function.
565 let res1 = Stream.map (joinAB km2) $ Stream.fromList $ Map.toList km1
566 where
567 joinAB km (k, a) =
568 case k `Map.lookup` km of
569 Just b -> (k, Just a, Just b)
570 Nothing -> (k, Just a, Nothing)
572 -- XXX We can take advantage of the lookups in the first pass above to
573 -- reduce the number of lookups in this pass. If we keep mutable cells
574 -- in the second Map, we can flag it in the first pass and not do any
575 -- lookup in the second pass if it is flagged.
576 let res2 = Stream.mapMaybe (joinAB km1) $ Stream.fromList $ Map.toList km2
577 where
578 joinAB km (k, b) =
579 case k `Map.lookup` km of
580 Just _ -> Nothing
581 Nothing -> Just (k, Nothing, Just b)
583 return $ Stream.serial res1 res2
585 where
587 -- XXX Generate error if a duplicate insertion is attempted?
588 kvFold = Stream.foldl' (\kv (k, b) -> Map.insert k b kv) Map.empty
590-- | Like 'joinOuter' but works only on sorted streams.
591--
592-- Space: O(1)
593--
594-- Time: O(m + n)
595--
596-- /Unimplemented/
597{-# INLINE mergeOuterJoin #-}
598mergeOuterJoin :: -- Monad m =>
599 (a -> b -> Ordering) -> t m a -> t m b -> t m (Maybe a, Maybe b)
600mergeOuterJoin _eq _s1 _s2 = undefined
602------------------------------------------------------------------------------
603-- Set operations (special joins)
604------------------------------------------------------------------------------
605--
606-- TODO: OrdSet/IntSet/hashmap based versions of these. With Eq only constraint
607-- the best would be to use an Array with linear search. If the second stream
608-- is sorted we can also use a binary search, using Ord constraint.
610-- | 'intersectBy' is essentially a filtering operation that retains only those
611-- elements in the first stream that are present in the second stream.
612--
613-- >>> Stream.toList $ Stream.intersectBy (==) (Stream.fromList [1,2,2,4]) (Stream.fromList [2,1,1,3])
614-- [1,2,2]
615--
616-- >>> Stream.toList $ Stream.intersectBy (==) (Stream.fromList [2,1,1,3]) (Stream.fromList [1,2,2,4])
617-- [2,1,1]
618--
619-- 'intersectBy' is similar to but not the same as 'joinInner':
620--
621-- >>> Stream.toList $ fmap fst $ Stream.joinInner (==) (Stream.fromList [1,2,2,4]) (Stream.fromList [2,1,1,3])
622-- [1,1,2,2]
623--
624-- Space: O(n) where @n@ is the number of elements in the second stream.
625--
626-- Time: O(m x n) where @m@ is the number of elements in the first stream and
627-- @n@ is the number of elements in the second stream.
628--
629-- /Pre-release/
630{-# INLINE intersectBy #-}
631intersectBy :: (IsStream t, Monad m) =>
632 (a -> a -> Bool) -> t m a -> t m a -> t m a
633intersectBy eq s1 s2 =
634 concatM
635 $ do
636 -- This may work well when s2 is small
637 xs <- Stream.toListRev $ Stream.uniqBy eq $ adapt s2
638 return $ Stream.filter (\x -> List.any (eq x) xs) s1
640-- | Like 'intersectBy' but works only on streams sorted in ascending order.
641--
642-- Space: O(1)
643--
644-- Time: O(m+n)
645--
646-- /Pre-release/
647{-# INLINE intersectBySorted #-}
648intersectBySorted :: (IsStream t, Monad m) =>
649 (a -> a -> Ordering) -> t m a -> t m a -> t m a
650intersectBySorted eq s1 =
651 IsStream.fromStreamD
652 . StreamD.intersectBySorted eq (IsStream.toStreamD s1)
653 . IsStream.toStreamD
655-- Roughly joinLeft s1 s2 = s1 `difference` s2 + s1 `intersection` s2
657-- | Delete first occurrences of those elements from the first stream that are
658-- present in the second stream. If an element occurs multiple times in the
659-- second stream as many occurrences of it are deleted from the first stream.
660--
661-- >>> Stream.toList $ Stream.differenceBy (==) (Stream.fromList [1,2,2]) (Stream.fromList [1,2,3])
662-- [2]
663--
664-- The following laws hold:
665--
666-- @
667-- (s1 `serial` s2) `differenceBy eq` s1 === s2
668-- (s1 `wSerial` s2) `differenceBy eq` s1 === s2
669-- @
670--
671-- Same as the list 'Data.List.//' operation.
672--
673-- Space: O(m) where @m@ is the number of elements in the first stream.
674--
675-- Time: O(m x n) where @m@ is the number of elements in the first stream and
676-- @n@ is the number of elements in the second stream.
677--
678-- /Pre-release/
679{-# INLINE differenceBy #-}
680differenceBy :: (IsStream t, Monad m) =>
681 (a -> a -> Bool) -> t m a -> t m a -> t m a
682differenceBy eq s1 s2 =
683 concatM
684 $ do
685 -- This may work well if s1 is small
686 -- If s1 is big we can go through s1, deleting elements from s2 and
687 -- not emitting an element if it was successfully deleted from s2.
688 -- we will need a deleteBy that can return whether the element was
689 -- deleted or not.
690 xs <- Stream.toList $ adapt s1
691 fmap fromList $ foldl' (flip (List.deleteBy eq)) xs s2
693-- | Like 'differenceBy' but works only on sorted streams.
694--
695-- Space: O(1)
696--
697-- /Unimplemented/
698{-# INLINE mergeDifferenceBy #-}
699mergeDifferenceBy :: -- (IsStream t, Monad m) =>
700 (a -> a -> Ordering) -> t m a -> t m a -> t m a
701mergeDifferenceBy _eq _s1 _s2 = undefined
703-- | This is essentially an append operation that appends all the extra
704-- occurrences of elements from the second stream that are not already present
705-- in the first stream.
706--
707-- >>> Stream.toList $ Stream.unionBy (==) (Stream.fromList [1,2,2,4]) (Stream.fromList [1,1,2,3])
708-- [1,2,2,4,3]
709--
710-- Equivalent to the following except that @s1@ is evaluated only once:
711--
712-- @
713-- unionBy eq s1 s2 = s1 \`serial` (s2 `differenceBy eq` s1)
714-- @
715--
716-- Similar to 'joinOuter' but not the same.
717--
718-- Space: O(n)
719--
720-- Time: O(m x n)
721--
722-- /Pre-release/
723{-# INLINE unionBy #-}
724unionBy :: (IsStream t, MonadAsync m, Semigroup (t m a)) =>
725 (a -> a -> Bool) -> t m a -> t m a -> t m a
726unionBy eq s1 s2 =
727 concatM
728 $ do
729 xs <- Stream.toList $ adapt s2
730 -- XXX we can use postscanlMAfter' instead of IORef
731 ref <- liftIO $ newIORef $! List.nubBy eq xs
732 let f x = do
733 liftIO $ modifyIORef' ref (List.deleteBy eq x)
734 return x
735 s3 = concatM
736 $ do
737 xs1 <- liftIO $ readIORef ref
738 return $ fromList xs1
739 return $ Stream.mapM f s1 <> s3
741-- | Like 'unionBy' but works only on sorted streams.
742--
743-- Space: O(1)
744--
745-- /Unimplemented/
746{-# INLINE mergeUnionBy #-}
747mergeUnionBy :: -- (IsStream t, Monad m) =>
748 (a -> a -> Ordering) -> t m a -> t m a -> t m a
749mergeUnionBy _eq _s1 _s2 = undefined