on master help sign in
Expose topBy Fold
769538dHarendra Kumar
core/src/Streamly/Data/Fold.hs+1
@@ -174,6 +174,7 @@ module Streamly.Data.Fold
174 , toMapIO
174 , toMapIO
175 , demuxToMap
175 , demuxToMap
176 , demuxToMapIO
176 , demuxToMapIO
177+ , topBy
177
178
178 -- ** Non-Empty Accumulators
179 -- ** Non-Empty Accumulators
179 -- | Accumulators that do not have a default value, therefore, return
180 -- | Accumulators that do not have a default value, therefore, return
core/src/Streamly/Internal/Data/Fold.hs+24−2
@@ -83,8 +83,9 @@ module Streamly.Internal.Data.Fold
83 -- $toListRev
83 -- $toListRev
84 , toStream
84 , toStream
85 , toStreamRev
85 , toStreamRev
86− , bottomBy
86+ , topBy
87 , top
87 , top
88+ , bottomBy
88 , bottom
89 , bottom
89
90
90 -- *** Scanners
91 -- *** Scanners
@@ -2468,11 +2469,32 @@ bottomBy cmp n = Fold step initial extract
2468
2469
2469 extract = return . fst
2470 extract = return . fst
2470
2471
2472+-- | Get the top @n@ elements using the supplied comparison function.
2473+--
2474+-- To get bottom n elements instead:
2475+--
2476+-- >>> bottomBy cmp = Fold.topBy (flip cmp)
2477+--
2478+-- Example:
2479+--
2480+-- >>> stream = Stream.fromList [2::Int,7,9,3,1,5,6,11,17]
2481+-- >>> Stream.fold (Fold.topBy compare 3) stream >>= MA.toList
2482+-- [17,11,9]
2483+--
2484+-- /Pre-release/
2485+--
2486+{-# INLINE topBy #-}
2487+topBy :: (MonadIO m, Unbox a) =>
2488+ (a -> a -> Ordering)
2489+ -> Int
2490+ -> Fold m a (MA.Array a)
2491+topBy cmp = bottomBy (flip cmp)
2492+
2471-- | Fold the input stream to top n elements.
2493-- | Fold the input stream to top n elements.
2472--
2494--
2473-- Definition:
2495-- Definition:
2474--
2496--
2475−-- >>> top = Fold.bottomBy $ flip compare
2497+-- >>> top = Fold.topBy compare
2476--
2498--
2477-- >>> stream = Stream.fromList [2::Int,7,9,3,1,5,6,11,17]
2499-- >>> stream = Stream.fromList [2::Int,7,9,3,1,5,6,11,17]
2478-- >>> Stream.fold (Fold.top 3) stream >>= MA.toList
2500-- >>> Stream.fold (Fold.top 3) stream >>= MA.toList