on master help sign in
Add the "frequency" fold
commitfe597c2339295be18ef0a1e15d7be697ddf57cd9 authorHarendra Kumar <harendra@composewell.com> dateTue 5 Jul 2022, 00:46 (4 years ago) parent0f951fb
1 file changed+13−3
core/src/Streamly/Internal/Data/Fold.hs+13−3
core/src/Streamly/Internal/Data/Fold.hs+13−3
@@ -56,6 +56,7 @@ module Streamly.Internal.Data.Fold
5656 , genericLength
5757 , countDistinct
5858 , countDistinctInt
59+ , frequency
5960 , mean
6061 , rollingHash
6162 , rollingHashWithSalt
@@ -84,6 +85,8 @@ module Streamly.Internal.Data.Fold
8485 , toStream
8586 , toStreamRev
8687 , toMap
88+ , top
89+ , bottom
8790
8891 -- ** Terminating Folds
8992 -- Element folds. Terminate after inspecting one element. All these can be
@@ -146,6 +149,7 @@ module Streamly.Internal.Data.Fold
146149 , sampleFromthen
147150 -- , ldeleteBy
148151 -- , luniq
152+ , nub
149153
150154 -- ** Mapping Filters
151155 , catMaybes
@@ -278,9 +282,6 @@ module Streamly.Internal.Data.Fold
278282 , snoc
279283 , duplicate
280284 , finish
281− , top
282− , bottom
283− , nub
284285
285286 -- * Deprecated
286287 , sequence
@@ -2274,6 +2275,15 @@ classifyMutWith f fld =
22742275classify :: (Monad m, Ord k) => Fold m a b -> Fold m (k, a) (Map k b)
22752276classify = classifyWith fst . lmap snd
22762277
2278+-- | Determine the frequency of each element in the stream.
2279+--
2280+-- You can just collect the keys of the resulting map to get the unique
2281+-- elements in the stream.
2282+--
2283+{-# INLINE frequency #-}
2284+frequency :: (Monad m, Ord a) => Fold m a (Map a Int)
2285+frequency = classifyWith id length
2286+
22772287------------------------------------------------------------------------------
22782288-- Unzipping
22792289------------------------------------------------------------------------------