packages feed

relude-1.2.2.1 / src / Relude / Extra / Group.hs

{-# LANGUAGE Safe          #-}{-# LANGUAGE TypeFamilies  #-}{-# LANGUAGE TypeOperators #-}{- |Module                  : Relude.Extra.GroupCopyright               : (c) 2018-2023 KowainikSPDX-License-Identifier : MITMaintainer              : Kowainik <xrom.xkov@gmail.com>Stability               : StablePortability             : PortablePolymorphic grouping functions.@since 0.1.0-}module Relude.Extra.Group    ( groupBy    , groupOneBy    ) whereimport Reludeimport Relude.Extra.Mapimport Data.List.NonEmpty ((<|)){- | Groups elements using results of the given function as keys.>>> groupBy even [1..6] :: HashMap Bool (NonEmpty Int)fromList [(False,5 :| [3,1]),(True,6 :| [4,2])]@since 0.1.0-}groupBy :: forall f t a . (Foldable f, DynamicMap t, Val t ~ NonEmpty a, Monoid t)        => (a -> Key t) -> f a -> tgroupBy f = flipfoldl' hmGroup mempty  where    hmGroup :: a -> t -> t    hmGroup x =        let val :: Maybe (NonEmpty a) -> NonEmpty a            val Nothing   = one x            val (Just xs) = x <| xs        in alter (Just . val) (f x){- | Similar to 'groupBy' but keeps only one element as value.>>> groupOneBy even [1 .. 6] :: HashMap Bool IntfromList [(False,1),(True,2)]@since 0.1.0-}groupOneBy :: forall f t a . (Foldable f, DynamicMap t, Val t ~ a, Monoid t)           => (a -> Key t) -> f a -> tgroupOneBy f = flipfoldl' hmGroup mempty  where    hmGroup :: a -> t -> t    hmGroup val m = let key = f val in        case lookup key m of            Nothing -> insert key val m            Just _  -> m