group-with-0.2.0.3: Classify objects by key-generating function, like SQL GROUP BY

Copyright(c) Uli Köhler 2014
LicenseApache License v2.0
Maintainerukoehler@techoverflow.net
Stabilityprovisional
Portabilityportable
Safe HaskellSafe
LanguageHaskell2010

Control.GroupWith

Description

A collection of grouping utility functions. For a given function that assigns a key to objects, provides functions that group said objects into a multimap by said key.

This can be used similarly to the SQL GROUP BY statement.

Provides a more flexible approach to GHC.Exts.groupWith

groupWith (take 1) ["a","ab","bc"] == Map.fromList [("a",["a","ab"]), ("b",["bc"])]

In order to use monadic / applicative functions as key generators, use the A- or M-postfixed variants like groupWithA or groupWithMultipleM

Synopsis

Documentation

type MultiMap a b = Map a [b] Source

groupWith Source

Arguments

:: Ord b 
=> (a -> b)

The function used to map a list value to its key

-> [a]

The list to be grouped

-> MultiMap b a

The resulting key --> value multimap

Group values in a list by a key, generated by a given function. The resulting map contains for each generated key the values (from the given list) that yielded said key by applying the function on said value.

groupWithMultiple Source

Arguments

:: Ord b 
=> (a -> [b])

The function used to map a list value to its keys

-> [a]

The list to be grouped

-> MultiMap b a

The resulting map

Like groupWith, but the identifier-generating function may generate multiple keys for each value (or none at all). The corresponding value from the original list will be placed in the identifier-corresponding map entry for each generated identifier. Note that values are added to the

groupWithUsing Source

Arguments

:: Ord b 
=> (a -> c)

Transformer function used to map a value to the resulting type

-> (c -> c -> c)

The combinator used to combine an existing value for a given key with a new value

-> (a -> b)

The function used to map a list value to its key

-> [a]

The list to be grouped

-> Map b c

The resulting key --> transformed value map

Like groupWith, but uses a custom combinator function

groupWithA Source

Arguments

:: (Ord b, Applicative f) 
=> (a -> f b)

The function used to map a list value to its key

-> [a]

The list to be grouped

-> f (MultiMap b a)

The resulting key --> value multimap

Group values in a list by a key, generated by a given applicative function. Applicative version of groupWith. See groupWith for documentation.

groupWithM Source

Arguments

:: (Ord b, Monad m, Applicative m) 
=> (a -> m b)

The function used to map a list value to its key

-> [a]

The list to be grouped

-> m (MultiMap b a)

The resulting key --> value multimap

Alias for groupWithA, with additional monad constraint

groupWithMultipleM Source

Arguments

:: (Ord b, Monad m, Applicative m) 
=> (a -> m [b])

The function used to map a list value to its keys

-> [a]

The list to be grouped

-> m (MultiMap b a)

The resulting map

Like groupWithM, but the identifier-generating function may generate multiple keys for each value (or none at all). See groupWithMultiple for further behavioural details.

Note that it's impossible to define this for applicatives: See http://stackoverflow.com/a/6032260/2597135

groupWithUsingM Source

Arguments

:: (Ord b, Monad m, Applicative m) 
=> (a -> m c)

Transformer function used to map a value to the resulting type

-> (c -> c -> c)

The combinator used to combine an existing value for a given key with a new value

-> (a -> m b)

The function used to map a list value to its key

-> [a]

The list to be grouped

-> m (Map b c)

The resulting key --> transformed value map

Like groupWithM, but uses a custom combinator function