{-# LANGUAGE Safe #-}
{-# LANGUAGE TypeFamilies #-}
module Relude.Extra.Group
( groupBy
, groupOneBy
) where
import Relude
import Relude.Extra.Map
import Data.List.NonEmpty ((<|))
groupBy :: forall f t a . (Foldable f, DynamicMap t, Val t ~ NonEmpty a, Monoid t)
=> (a -> Key t) -> f a -> t
groupBy 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)
groupOneBy :: forall f t a . (Foldable f, DynamicMap t, Val t ~ a, Monoid t)
=> (a -> Key t) -> f a -> t
groupOneBy 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