on master help sign in
Add the "frequency" fold
fe597c2Harendra Kumar
core/src/Streamly/Internal/Data/Fold.hs+13−3
@@ -56,6 +56,7 @@ module Streamly.Internal.Data.Fold
56 , genericLength
56 , genericLength
57 , countDistinct
57 , countDistinct
58 , countDistinctInt
58 , countDistinctInt
59+ , frequency
59 , mean
60 , mean
60 , rollingHash
61 , rollingHash
61 , rollingHashWithSalt
62 , rollingHashWithSalt
@@ -84,6 +85,8 @@ module Streamly.Internal.Data.Fold
84 , toStream
85 , toStream
85 , toStreamRev
86 , toStreamRev
86 , toMap
87 , toMap
88+ , top
89+ , bottom
87
90
88 -- ** Terminating Folds
91 -- ** Terminating Folds
89 -- Element folds. Terminate after inspecting one element. All these can be
92 -- Element folds. Terminate after inspecting one element. All these can be
@@ -146,6 +149,7 @@ module Streamly.Internal.Data.Fold
146 , sampleFromthen
149 , sampleFromthen
147 -- , ldeleteBy
150 -- , ldeleteBy
148 -- , luniq
151 -- , luniq
152+ , nub
149
153
150 -- ** Mapping Filters
154 -- ** Mapping Filters
151 , catMaybes
155 , catMaybes
@@ -278,9 +282,6 @@ module Streamly.Internal.Data.Fold
278 , snoc
282 , snoc
279 , duplicate
283 , duplicate
280 , finish
284 , finish
281− , top
282− , bottom
283− , nub
284
285
285 -- * Deprecated
286 -- * Deprecated
286 , sequence
287 , sequence
@@ -2274,6 +2275,15 @@ classifyMutWith f fld =
2274classify :: (Monad m, Ord k) => Fold m a b -> Fold m (k, a) (Map k b)
2275classify :: (Monad m, Ord k) => Fold m a b -> Fold m (k, a) (Map k b)
2275classify = classifyWith fst . lmap snd
2276classify = classifyWith fst . lmap snd
2276
2277
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+
2277------------------------------------------------------------------------------
2287------------------------------------------------------------------------------
2278-- Unzipping
2288-- Unzipping
2279------------------------------------------------------------------------------
2289------------------------------------------------------------------------------