{-
	Copyright (C) 2021 Dr. Alistair Ward

	This file is part of BishBosh.

	BishBosh is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	BishBosh is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with BishBosh.  If not, see <http://www.gnu.org/licenses/>.
-}
{- |
 [@AUTHOR@]	Dr. Alistair Ward

 [@DESCRIPTION@]	Arbitrary operations on foldable data.
-}

module BishBosh.Data.Foldable(
	findDuplicates
) where

import qualified	Data.Foldable
import qualified	Data.Map.Strict	as Map

-- | Returns a unique instance of any item which has been specified more than once.
findDuplicates :: (Foldable foldable, Ord a) => foldable a -> [a]
findDuplicates :: foldable a -> [a]
findDuplicates	= Map a Int -> [a]
forall k a. Map k a -> [k]
Map.keys (Map a Int -> [a])
-> (foldable a -> Map a Int) -> foldable a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> Bool) -> Map a Int -> Map a Int
forall a k. (a -> Bool) -> Map k a -> Map k a
Map.filter (Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1) (Map a Int -> Map a Int)
-> (foldable a -> Map a Int) -> foldable a -> Map a Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Map a Int -> Map a Int)
-> Map a Int -> foldable a -> Map a Int
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
Data.Foldable.foldr (
	(a -> Int -> Map a Int -> Map a Int)
-> Int -> a -> Map a Int -> Map a Int
forall a b c. (a -> b -> c) -> b -> a -> c
flip ((Int -> Int -> Int) -> a -> Int -> Map a Int -> Map a Int
forall k a. Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a
Map.insertWith ((Int -> Int -> Int) -> a -> Int -> Map a Int -> Map a Int)
-> (Int -> Int -> Int) -> a -> Int -> Map a Int -> Map a Int
forall a b. (a -> b) -> a -> b
$ (Int -> Int) -> Int -> Int -> Int
forall a b. a -> b -> a
const Int -> Int
forall a. Enum a => a -> a
succ) (Int
1 :: Int)	-- Count instances.
 ) Map a Int
forall k a. Map k a
Map.empty