@@ -20,6 +20,9 @@ module Streamly.Internal.Data.Stream.IsStream.Top
20 , sampleBurstStart
20 , sampleBurstStart
21 , sampleBurstEnd
21 , sampleBurstEnd
23 -- * Nesting
26 -- * Nesting
24 -- ** Set like operations
27 -- ** Set like operations
25 -- | These are not exactly set operations because streams are not
28 -- | These are not exactly set operations because streams are not
@@ -68,6 +71,7 @@ import qualified Streamly.Internal.Data.Array as Array
68import qualified Streamly.Internal.Data.Fold as Fold
71import qualified Streamly.Internal.Data.Fold as Fold
69import qualified Streamly.Internal.Data.Stream.IsStream.Lift as Stream
72import qualified Streamly.Internal.Data.Stream.IsStream.Lift as Stream
70import qualified Streamly.Internal.Data.Stream.IsStream.Eliminate as Stream
73import qualified Streamly.Internal.Data.Stream.IsStream.Eliminate as Stream
74+import qualified Streamly.Internal.Data.Stream.IsStream.Generate as Stream
71import qualified Streamly.Internal.Data.Stream.IsStream.Nesting as Stream
75import qualified Streamly.Internal.Data.Stream.IsStream.Nesting as Stream
72import qualified Streamly.Internal.Data.Stream.IsStream.Transform as Stream
76import qualified Streamly.Internal.Data.Stream.IsStream.Transform as Stream
73import qualified Streamly.Internal.Data.Stream.StreamK as StreamK
77import qualified Streamly.Internal.Data.Stream.StreamK as StreamK
@@ -164,6 +168,27 @@ sampleBurstStart gap =
164 . Stream.groupsByRolling f Fold.head
168 . Stream.groupsByRolling f Fold.head
165 . Stream.timeIndexed
169 . Stream.timeIndexed
171+------------------------------------------------------------------------------
173+------------------------------------------------------------------------------
175+-- We could possibly choose different algorithms depending on whether the
176+-- input stream is almost sorted (ascending/descending) or random. We could
177+-- serialize the stream to an array and use quicksort.
179+-- | Sort the input stream using a supplied comparison function.
183+-- Note: this is not the fastest possible implementation as of now.
187+{-# INLINE sortBy #-}
188+sortBy :: (IsStream t, Monad m) => (a -> a -> Ordering) -> t m a -> t m a
189+-- XXX creating StreamD and using D.mergeBy may be more efficient due to fusion
190+sortBy f = Stream.concatPairsWith (Stream.mergeBy f) Stream.yield
167------------------------------------------------------------------------------
192------------------------------------------------------------------------------
168-- SQL Joins
193-- SQL Joins
169------------------------------------------------------------------------------
194------------------------------------------------------------------------------