module Query where

import Control.Monad (liftM2, )
import Data.List (genericLength, )
import qualified Data.Map as Map


showTable :: Show a => [a] -> IO ()
showTable =
   mapM_ print

cross :: [a] -> [b] -> [(a,b)]
cross = liftM2 (,)

join :: (Eq f) =>
   (a -> f) -> (b -> f) -> [a] -> [b] -> [(a,b)]
join af bf as bs =
   filter (\(a,b) -> af a == bf b) $
   liftM2 (,) as bs

average :: (Fractional a) => [a] -> a
average xs =
   sum xs / genericLength xs

averageInt :: (Integral a) => [a] -> a
averageInt xs =
   let n = genericLength xs
   in  div (sum xs + div n 2) n

groupBy ::
   (Ord key) =>
   (a -> key) -> [a] -> [(key, [a])]
groupBy f =
   Map.toAscList .
   Map.fromListWith (++) .
   map (\a -> (f a, [a]))
