raft-0.3.2.2: src/Math/Fractions.hs
-----------------------------------------------------------------------------
--
-- Module : Math.Fractions
-- Copyright : (c) 2014-16 Brian W Bush
-- License : MIT
--
-- Maintainer : Brian W Bush <consult@brianwbush.info>
-- Stability : Experimental
-- Portability : Portable
--
-- | Some special operations with fractions.
--
-----------------------------------------------------------------------------
{-# LANGUAGE Safe #-}
module Math.Fractions {-# DEPRECATED "This module will be removed in a future release." #-} (
-- * Functions
fractions
, fractionsMaybe
) where
import Control.Arrow (second)
import Data.List.Util (regroup)
import Data.Maybe (mapMaybe)
-- | Apply a function to a list to extract keys with values that sum to unity.
fractions :: (Ord b, Fractional c) => (a -> (b, c)) -> [a] -> [(b, c)]
fractions extractor x =
let
x' = map (second sum) $ regroup $ map extractor x
t = sum $ map snd x'
in
map (second (/ t)) x'
-- | Apply a function to a list to extract keys with values that sum to unity.
fractionsMaybe :: (Ord b, Fractional c) => (a -> Maybe (b, c)) -> [a] -> [(b, c)]
fractionsMaybe = (fractions id .) . mapMaybe