discrimination-0.4.1: Fast generic linear-time sorting, joins and container construction.
Safe HaskellNone
LanguageHaskell2010

Data.Discrimination

Synopsis

Discrimination

class Decidable f => Discriminating f where Source #

Methods

disc :: f a -> [(a, b)] -> [[b]] Source #

Instances

Instances details
Discriminating Group Source # 
Instance details

Defined in Data.Discrimination.Class

Methods

disc :: Group a -> [(a, b)] -> [[b]] Source #

Discriminating Sort Source # 
Instance details

Defined in Data.Discrimination.Class

Methods

disc :: Sort a -> [(a, b)] -> [[b]] Source #

Unordered

newtype Group a Source #

Productive Stable Unordered Discriminator

Constructors

Group 

Fields

Instances

Instances details
Contravariant Group Source # 
Instance details

Defined in Data.Discrimination.Grouping

Methods

contramap :: (a -> b) -> Group b -> Group a #

(>$) :: b -> Group b -> Group a #

Divisible Group Source # 
Instance details

Defined in Data.Discrimination.Grouping

Methods

divide :: (a -> (b, c)) -> Group b -> Group c -> Group a #

conquer :: Group a #

Decidable Group Source # 
Instance details

Defined in Data.Discrimination.Grouping

Methods

lose :: (a -> Void) -> Group a #

choose :: (a -> Either b c) -> Group b -> Group c -> Group a #

Discriminating Group Source # 
Instance details

Defined in Data.Discrimination.Class

Methods

disc :: Group a -> [(a, b)] -> [[b]] Source #

Semigroup (Group a) Source # 
Instance details

Defined in Data.Discrimination.Grouping

Methods

(<>) :: Group a -> Group a -> Group a #

sconcat :: NonEmpty (Group a) -> Group a #

stimes :: Integral b => b -> Group a -> Group a #

Monoid (Group a) Source # 
Instance details

Defined in Data.Discrimination.Grouping

Methods

mempty :: Group a #

mappend :: Group a -> Group a -> Group a #

mconcat :: [Group a] -> Group a #

class Grouping a where Source #

Eq equipped with a compatible stable unordered discriminator.

Law:

groupingEq x y ≡ (x == y)

Note: Eq is a moral super class of Grouping. It isn't because of some missing instances.

Minimal complete definition

Nothing

Methods

grouping :: Group a Source #

For every surjection f,

contramap f groupinggrouping

Instances

Instances details
Grouping Bool Source # 
Instance details

Defined in Data.Discrimination.Grouping

Grouping Char Source # 
Instance details

Defined in Data.Discrimination.Grouping

Grouping Int Source # 
Instance details

Defined in Data.Discrimination.Grouping

Grouping Int8 Source # 
Instance details

Defined in Data.Discrimination.Grouping

Grouping Int16 Source # 
Instance details

Defined in Data.Discrimination.Grouping

Grouping Int32 Source # 
Instance details

Defined in Data.Discrimination.Grouping

Grouping Int64 Source # 
Instance details

Defined in Data.Discrimination.Grouping

Grouping Integer Source # 
Instance details

Defined in Data.Discrimination.Grouping

Grouping Natural Source # 
Instance details

Defined in Data.Discrimination.Grouping

Grouping Ordering Source # 
Instance details

Defined in Data.Discrimination.Grouping

Grouping Word Source # 
Instance details

Defined in Data.Discrimination.Grouping

Grouping Word8 Source # 
Instance details

Defined in Data.Discrimination.Grouping

Grouping Word16 Source # 
Instance details

Defined in Data.Discrimination.Grouping

Grouping Word32 Source # 
Instance details

Defined in Data.Discrimination.Grouping

Grouping Word64 Source # 
Instance details

Defined in Data.Discrimination.Grouping

Grouping () Source # 
Instance details

Defined in Data.Discrimination.Grouping

Methods

grouping :: Group () Source #

Grouping Void Source # 
Instance details

Defined in Data.Discrimination.Grouping

Grouping a => Grouping [a] Source # 
Instance details

Defined in Data.Discrimination.Grouping

Methods

grouping :: Group [a] Source #

Grouping a => Grouping (Maybe a) Source # 
Instance details

Defined in Data.Discrimination.Grouping

Methods

grouping :: Group (Maybe a) Source #

Grouping a => Grouping (Ratio a) Source # 
Instance details

Defined in Data.Discrimination.Grouping

Methods

grouping :: Group (Ratio a) Source #

Grouping a => Grouping (Complex a) Source # 
Instance details

Defined in Data.Discrimination.Grouping

Methods

grouping :: Group (Complex a) Source #

Grouping a => Grouping (NonEmpty a) Source # 
Instance details

Defined in Data.Discrimination.Grouping

(Grouping a, Grouping b) => Grouping (Either a b) Source # 
Instance details

Defined in Data.Discrimination.Grouping

Methods

grouping :: Group (Either a b) Source #

(Grouping a, Grouping b) => Grouping (a, b) Source # 
Instance details

Defined in Data.Discrimination.Grouping

Methods

grouping :: Group (a, b) Source #

(Grouping a, Grouping b, Grouping c) => Grouping (a, b, c) Source # 
Instance details

Defined in Data.Discrimination.Grouping

Methods

grouping :: Group (a, b, c) Source #

(Grouping a, Grouping b, Grouping c, Grouping d) => Grouping (a, b, c, d) Source # 
Instance details

Defined in Data.Discrimination.Grouping

Methods

grouping :: Group (a, b, c, d) Source #

(Grouping1 f, Grouping1 g, Grouping a) => Grouping (Compose f g a) Source # 
Instance details

Defined in Data.Discrimination.Grouping

Methods

grouping :: Group (Compose f g a) Source #

class Grouping1 f where Source #

Minimal complete definition

Nothing

Methods

grouping1 :: Group a -> Group (f a) Source #

default grouping1 :: Deciding1 Grouping f => Group a -> Group (f a) Source #

Instances

Instances details
Grouping1 [] Source # 
Instance details

Defined in Data.Discrimination.Grouping

Methods

grouping1 :: Group a -> Group [a] Source #

Grouping1 Maybe Source # 
Instance details

Defined in Data.Discrimination.Grouping

Methods

grouping1 :: Group a -> Group (Maybe a) Source #

Grouping1 Complex Source # 
Instance details

Defined in Data.Discrimination.Grouping

Methods

grouping1 :: Group a -> Group (Complex a) Source #

Grouping1 NonEmpty Source # 
Instance details

Defined in Data.Discrimination.Grouping

Methods

grouping1 :: Group a -> Group (NonEmpty a) Source #

Grouping a => Grouping1 (Either a) Source # 
Instance details

Defined in Data.Discrimination.Grouping

Methods

grouping1 :: Group a0 -> Group (Either a a0) Source #

Grouping a => Grouping1 ((,) a) Source # 
Instance details

Defined in Data.Discrimination.Grouping

Methods

grouping1 :: Group a0 -> Group (a, a0) Source #

(Grouping a, Grouping b) => Grouping1 ((,,) a b) Source # 
Instance details

Defined in Data.Discrimination.Grouping

Methods

grouping1 :: Group a0 -> Group (a, b, a0) Source #

(Grouping a, Grouping b, Grouping c) => Grouping1 ((,,,) a b c) Source # 
Instance details

Defined in Data.Discrimination.Grouping

Methods

grouping1 :: Group a0 -> Group (a, b, c, a0) Source #

(Grouping1 f, Grouping1 g) => Grouping1 (Compose f g) Source # 
Instance details

Defined in Data.Discrimination.Grouping

Methods

grouping1 :: Group a -> Group (Compose f g a) Source #

nub :: Grouping a => [a] -> [a] Source #

O(n). This upgrades nub from Data.List from O(n^2) to O(n) by using productive unordered discrimination.

nub = nubWith id
nub as = head <$> group as

nubWith :: Grouping b => (a -> b) -> [a] -> [a] Source #

O(n). Online nub with a Schwartzian transform.

nubWith f as = head <$> groupWith f as

group :: Grouping a => [a] -> [[a]] Source #

O(n). Similar to group, except we do not require groups to be clustered.

This combinator still operates in linear time, at the expense of storing history.

The result equivalence classes are not sorted, but the grouping is stable.

group = groupWith id

groupWith :: Grouping b => (a -> b) -> [a] -> [[a]] Source #

O(n). This is a replacement for groupWith using discrimination.

The result equivalence classes are not sorted, but the grouping is stable.

runGroup :: Group a -> [(a, b)] -> [[b]] Source #

groupingEq :: Grouping a => a -> a -> Bool Source #

Valid definition for (==) in terms of Grouping.

Ordered

newtype Sort a Source #

Stable Ordered Discriminator

Constructors

Sort 

Fields

  • runSort :: forall b. [(a, b)] -> [[b]]
     

Instances

Instances details
Contravariant Sort Source # 
Instance details

Defined in Data.Discrimination.Sorting

Methods

contramap :: (a -> b) -> Sort b -> Sort a #

(>$) :: b -> Sort b -> Sort a #

Divisible Sort Source # 
Instance details

Defined in Data.Discrimination.Sorting

Methods

divide :: (a -> (b, c)) -> Sort b -> Sort c -> Sort a #

conquer :: Sort a #

Decidable Sort Source # 
Instance details

Defined in Data.Discrimination.Sorting

Methods

lose :: (a -> Void) -> Sort a #

choose :: (a -> Either b c) -> Sort b -> Sort c -> Sort a #

Discriminating Sort Source # 
Instance details

Defined in Data.Discrimination.Class

Methods

disc :: Sort a -> [(a, b)] -> [[b]] Source #

Semigroup (Sort a) Source # 
Instance details

Defined in Data.Discrimination.Sorting

Methods

(<>) :: Sort a -> Sort a -> Sort a #

sconcat :: NonEmpty (Sort a) -> Sort a #

stimes :: Integral b => b -> Sort a -> Sort a #

Monoid (Sort a) Source # 
Instance details

Defined in Data.Discrimination.Sorting

Methods

mempty :: Sort a #

mappend :: Sort a -> Sort a -> Sort a #

mconcat :: [Sort a] -> Sort a #

class Grouping a => Sorting a where Source #

Ord equipped with a compatible stable, ordered discriminator.

Law:

sortingCompare x y ≡ compare x y

Minimal complete definition

Nothing

Methods

sorting :: Sort a Source #

For every strictly monotone-increasing function f:

contramap f sortingsorting

default sorting :: Deciding Sorting a => Sort a Source #

Instances

Instances details
Sorting Bool Source # 
Instance details

Defined in Data.Discrimination.Sorting

Sorting Char Source # 
Instance details

Defined in Data.Discrimination.Sorting

Sorting Int Source # 
Instance details

Defined in Data.Discrimination.Sorting

Methods

sorting :: Sort Int Source #

Sorting Int8 Source # 
Instance details

Defined in Data.Discrimination.Sorting

Sorting Int16 Source # 
Instance details

Defined in Data.Discrimination.Sorting

Sorting Int32 Source # 
Instance details

Defined in Data.Discrimination.Sorting

Sorting Int64 Source # 
Instance details

Defined in Data.Discrimination.Sorting

Sorting Integer Source # 
Instance details

Defined in Data.Discrimination.Sorting

Sorting Natural Source # 
Instance details

Defined in Data.Discrimination.Sorting

Sorting Ordering Source # 
Instance details

Defined in Data.Discrimination.Sorting

Sorting Word Source # 
Instance details

Defined in Data.Discrimination.Sorting

Sorting Word8 Source # 
Instance details

Defined in Data.Discrimination.Sorting

Sorting Word16 Source # 
Instance details

Defined in Data.Discrimination.Sorting

Sorting Word32 Source # 
Instance details

Defined in Data.Discrimination.Sorting

Sorting Word64 Source # 
Instance details

Defined in Data.Discrimination.Sorting

Sorting () Source # 
Instance details

Defined in Data.Discrimination.Sorting

Methods

sorting :: Sort () Source #

Sorting Void Source # 
Instance details

Defined in Data.Discrimination.Sorting

Sorting a => Sorting [a] Source # 
Instance details

Defined in Data.Discrimination.Sorting

Methods

sorting :: Sort [a] Source #

Sorting a => Sorting (Maybe a) Source # 
Instance details

Defined in Data.Discrimination.Sorting

Methods

sorting :: Sort (Maybe a) Source #

Sorting a => Sorting (NonEmpty a) Source # 
Instance details

Defined in Data.Discrimination.Sorting

Methods

sorting :: Sort (NonEmpty a) Source #

(Sorting a, Sorting b) => Sorting (Either a b) Source # 
Instance details

Defined in Data.Discrimination.Sorting

Methods

sorting :: Sort (Either a b) Source #

(Sorting a, Sorting b) => Sorting (a, b) Source # 
Instance details

Defined in Data.Discrimination.Sorting

Methods

sorting :: Sort (a, b) Source #

(Sorting a, Sorting b, Sorting c) => Sorting (a, b, c) Source # 
Instance details

Defined in Data.Discrimination.Sorting

Methods

sorting :: Sort (a, b, c) Source #

(Sorting a, Sorting b, Sorting c, Sorting d) => Sorting (a, b, c, d) Source # 
Instance details

Defined in Data.Discrimination.Sorting

Methods

sorting :: Sort (a, b, c, d) Source #

(Sorting1 f, Sorting1 g, Sorting a) => Sorting (Compose f g a) Source # 
Instance details

Defined in Data.Discrimination.Sorting

Methods

sorting :: Sort (Compose f g a) Source #

class Grouping1 f => Sorting1 f where Source #

Minimal complete definition

Nothing

Methods

sorting1 :: Sort a -> Sort (f a) Source #

default sorting1 :: Deciding1 Sorting f => Sort a -> Sort (f a) Source #

Instances

Instances details
Sorting1 [] Source # 
Instance details

Defined in Data.Discrimination.Sorting

Methods

sorting1 :: Sort a -> Sort [a] Source #

Sorting1 Maybe Source # 
Instance details

Defined in Data.Discrimination.Sorting

Methods

sorting1 :: Sort a -> Sort (Maybe a) Source #

Sorting1 NonEmpty Source # 
Instance details

Defined in Data.Discrimination.Sorting

Methods

sorting1 :: Sort a -> Sort (NonEmpty a) Source #

Sorting a => Sorting1 (Either a) Source # 
Instance details

Defined in Data.Discrimination.Sorting

Methods

sorting1 :: Sort a0 -> Sort (Either a a0) Source #

(Sorting1 f, Sorting1 g) => Sorting1 (Compose f g) Source # 
Instance details

Defined in Data.Discrimination.Sorting

Methods

sorting1 :: Sort a -> Sort (Compose f g a) Source #

desc :: Sort a -> Sort a Source #

sort :: Sorting a => [a] -> [a] Source #

O(n). Sort a list using discrimination.

sort = sortWith id

sortWith :: Sorting b => (a -> b) -> [a] -> [a] Source #

O(n). Sort a list with a Schwartzian transformation by using discrimination.

This linear time replacement for sortWith and sortOn uses discrimination.

sortingBag :: Foldable f => Sort k -> Sort (f k) Source #

Construct a stable ordered discriminator that sorts a list as multisets of elements from another stable ordered discriminator.

The resulting discriminator only cares about the set of keys and their multiplicity, and is sorted as if we'd sorted each key in turn before comparing.

sortingSet :: Foldable f => Sort k -> Sort (f k) Source #

Construct a stable ordered discriminator that sorts a list as sets of elements from another stable ordered discriminator.

The resulting discriminator only cares about the set of keys, and is sorted as if we'd sorted each key in turn before comparing.

sortingCompare :: Sorting a => a -> a -> Ordering Source #

Valid definition for compare in terms of Sorting.

Container Construction

toMap :: Sorting k => [(k, v)] -> Map k v Source #

O(n). Construct a Map.

This is an asymptotically faster version of fromList, which exploits ordered discrimination.

>>> toMap []
fromList []
>>> toMap [(5,"a"), (3 :: Int,"b"), (5, "c")]
fromList [(3,"b"),(5,"c")]
>>> Map.fromList [(5,"a"), (3 :: Int,"b"), (5, "c")]
fromList [(3,"b"),(5,"c")]
>>> toMap [(5,"c"), (3,"b"), (5 :: Int, "a")]
fromList [(3,"b"),(5,"a")]
>>> Map.fromList [(5,"c"), (3,"b"), (5 :: Int, "a")]
fromList [(3,"b"),(5,"a")]

toMapWith :: Sorting k => (v -> v -> v) -> [(k, v)] -> Map k v Source #

O(n). Construct a Map, combining values.

This is an asymptotically faster version of fromListWith, which exploits ordered discrimination.

(Note: values combine in anti-stable order for compatibility with fromListWith)

>>> toMapWith (++) [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5 :: Int,"c")]
fromList [(3,"ab"),(5,"cba")]
>>> Map.fromListWith (++) [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5 :: Int,"c")]
fromList [(3,"ab"),(5,"cba")]
>>> toMapWith (++) []
fromList []

toMapWithKey :: Sorting k => (k -> v -> v -> v) -> [(k, v)] -> Map k v Source #

O(n). Construct a Map, combining values with access to the key.

This is an asymptotically faster version of fromListWithKey, which exploits ordered discrimination.

(Note: the values combine in anti-stable order for compatibility with fromListWithKey)

>>> let f key new_value old_value = show key ++ ":" ++ new_value ++ "|" ++ old_value
>>> toMapWithKey f [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5 :: Int,"c")]
fromList [(3,"3:a|b"),(5,"5:c|5:b|a")]
>>> toMapWithKey f []
fromList []

toIntMap :: [(Int, v)] -> IntMap v Source #

O(n). Construct an IntMap.

>>> toIntMap []
fromList []
>>> toIntMap [(5,"a"), (3,"b"), (5, "c")]
fromList [(3,"b"),(5,"c")]
>>> IntMap.fromList [(5,"a"), (3,"b"), (5, "c")]
fromList [(3,"b"),(5,"c")]
>>> toIntMap [(5,"c"), (3,"b"), (5, "a")]
fromList [(3,"b"),(5,"a")]
>>> IntMap.fromList [(5,"c"), (3,"b"), (5, "a")]
fromList [(3,"b"),(5,"a")]

toIntMapWith :: (v -> v -> v) -> [(Int, v)] -> IntMap v Source #

O(n). Construct an IntMap, combining values.

This is an asymptotically faster version of fromListWith, which exploits ordered discrimination.

(Note: values combine in anti-stable order for compatibility with fromListWith)

>>> toIntMapWith (++) [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"c")]
fromList [(3,"ab"),(5,"cba")]
>>> IntMap.fromListWith (++) [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"c")]
fromList [(3,"ab"),(5,"cba")]
>>> toIntMapWith (++) []
fromList []

toIntMapWithKey :: (Int -> v -> v -> v) -> [(Int, v)] -> IntMap v Source #

O(n). Construct a Map, combining values with access to the key.

This is an asymptotically faster version of fromListWithKey, which exploits ordered discrimination.

(Note: the values combine in anti-stable order for compatibility with fromListWithKey)

>>> let f key new_value old_value = show key ++ ":" ++ new_value ++ "|" ++ old_value
>>> toIntMapWithKey f [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"c")]
fromList [(3,"3:a|b"),(5,"5:c|5:b|a")]
>>> IntMap.fromListWithKey f [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"c")]
fromList [(3,"3:a|b"),(5,"5:c|5:b|a")]
>>> toIntMapWithKey f []
fromList []

toSet :: Sorting k => [k] -> Set k Source #

O(n). Construct a Set in linear time.

This is an asymptotically faster version of fromList, which exploits ordered discrimination.

toIntSet :: [Int] -> IntSet Source #

O(n). Construct an IntSet in linear time.

This is an asymptotically faster version of fromList, which exploits ordered discrimination.

Joins

joining Source #

Arguments

:: Discriminating f 
=> f d

the discriminator to use

-> ([a] -> [b] -> c)

how to join two tables

-> (a -> d)

selector for the left table

-> (b -> d)

selector for the right table

-> [a]

left table

-> [b]

right table

-> [c] 

O(n). Perform a full outer join while explicit merging of the two result tables a table at a time.

The results are grouped by the discriminator.

inner Source #

Arguments

:: Discriminating f 
=> f d

the discriminator to use

-> (a -> b -> c)

how to join two rows

-> (a -> d)

selector for the left table

-> (b -> d)

selector for the right table

-> [a]

left table

-> [b]

right table

-> [[c]] 

O(n). Perform an inner join, with operations defined one row at a time.

The results are grouped by the discriminator.

This takes operation time linear in both the input and result sets.

outer Source #

Arguments

:: Discriminating f 
=> f d

the discriminator to use

-> (a -> b -> c)

how to join two rows

-> (a -> c)

row present on the left, missing on the right

-> (b -> c)

row present on the right, missing on the left

-> (a -> d)

selector for the left table

-> (b -> d)

selector for the right table

-> [a]

left table

-> [b]

right table

-> [[c]] 

O(n). Perform a full outer join with operations defined one row at a time.

The results are grouped by the discriminator.

This takes operation time linear in both the input and result sets.

leftOuter Source #

Arguments

:: Discriminating f 
=> f d

the discriminator to use

-> (a -> b -> c)

how to join two rows

-> (a -> c)

row present on the left, missing on the right

-> (a -> d)

selector for the left table

-> (b -> d)

selector for the right table

-> [a]

left table

-> [b]

right table

-> [[c]] 

O(n). Perform a left outer join with operations defined one row at a time.

The results are grouped by the discriminator.

This takes operation time linear in both the input and result sets.

rightOuter Source #

Arguments

:: Discriminating f 
=> f d

the discriminator to use

-> (a -> b -> c)

how to join two rows

-> (b -> c)

row present on the right, missing on the left

-> (a -> d)

selector for the left table

-> (b -> d)

selector for the right table

-> [a]

left table

-> [b]

right table

-> [[c]] 

O(n). Perform a right outer join with operations defined one row at a time.

The results are grouped by the discriminator.

This takes operation time linear in both the input and result sets.