Copyright | © 2022–2024 Jonathan Knowles |
---|---|
License | Apache-2.0 |
Safe Haskell | None |
Language | Haskell2010 |
Synopsis
- data MonoidMap k v
- empty :: MonoidMap k v
- fromList :: (Ord k, MonoidNull v) => [(k, v)] -> MonoidMap k v
- fromListWith :: (Ord k, MonoidNull v) => (v -> v -> v) -> [(k, v)] -> MonoidMap k v
- fromMap :: MonoidNull v => Map k v -> MonoidMap k v
- singleton :: (Ord k, MonoidNull v) => k -> v -> MonoidMap k v
- toList :: MonoidMap k v -> [(k, v)]
- toMap :: MonoidMap k v -> Map k v
- get :: (Ord k, Monoid v) => k -> MonoidMap k v -> v
- set :: (Ord k, MonoidNull v) => k -> v -> MonoidMap k v -> MonoidMap k v
- adjust :: (Ord k, MonoidNull v) => (v -> v) -> k -> MonoidMap k v -> MonoidMap k v
- nullify :: Ord k => k -> MonoidMap k v -> MonoidMap k v
- null :: MonoidMap k v -> Bool
- nullKey :: Ord k => k -> MonoidMap k v -> Bool
- nonNull :: MonoidMap k v -> Bool
- nonNullCount :: MonoidMap k v -> Int
- nonNullKey :: Ord k => k -> MonoidMap k v -> Bool
- nonNullKeys :: MonoidMap k v -> Set k
- take :: Int -> MonoidMap k v -> MonoidMap k v
- drop :: Int -> MonoidMap k v -> MonoidMap k v
- splitAt :: Int -> MonoidMap k a -> (MonoidMap k a, MonoidMap k a)
- filter :: (v -> Bool) -> MonoidMap k v -> MonoidMap k v
- filterKeys :: (k -> Bool) -> MonoidMap k v -> MonoidMap k v
- filterWithKey :: (k -> v -> Bool) -> MonoidMap k v -> MonoidMap k v
- partition :: (v -> Bool) -> MonoidMap k v -> (MonoidMap k v, MonoidMap k v)
- partitionKeys :: (k -> Bool) -> MonoidMap k v -> (MonoidMap k v, MonoidMap k v)
- partitionWithKey :: (k -> v -> Bool) -> MonoidMap k v -> (MonoidMap k v, MonoidMap k v)
- map :: MonoidNull v2 => (v1 -> v2) -> MonoidMap k v1 -> MonoidMap k v2
- mapKeys :: (Ord k2, MonoidNull v) => (k1 -> k2) -> MonoidMap k1 v -> MonoidMap k2 v
- mapKeysWith :: (Ord k2, MonoidNull v) => (v -> v -> v) -> (k1 -> k2) -> MonoidMap k1 v -> MonoidMap k2 v
- append :: (Ord k, MonoidNull v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v
- minus :: (Ord k, MonoidNull v, Group v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v
- minusMaybe :: (Ord k, MonoidNull v, Reductive v) => MonoidMap k v -> MonoidMap k v -> Maybe (MonoidMap k v)
- monus :: (Ord k, MonoidNull v, Monus v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v
- invert :: (MonoidNull v, Group v) => MonoidMap k v -> MonoidMap k v
- power :: (Integral i, MonoidNull v, Group v) => MonoidMap k v -> i -> MonoidMap k v
- isSubmapOf :: (Ord k, Monoid v, Reductive v) => MonoidMap k v -> MonoidMap k v -> Bool
- isSubmapOfBy :: (Ord k, Monoid v1, Monoid v2) => (v1 -> v2 -> Bool) -> MonoidMap k v1 -> MonoidMap k v2 -> Bool
- disjoint :: (Ord k, GCDMonoid v, MonoidNull v) => MonoidMap k v -> MonoidMap k v -> Bool
- disjointBy :: (Ord k, Monoid v1, Monoid v2) => (v1 -> v2 -> Bool) -> MonoidMap k v1 -> MonoidMap k v2 -> Bool
- intersection :: (Ord k, MonoidNull v, GCDMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v
- intersectionWith :: (Ord k, MonoidNull v3) => (v1 -> v2 -> v3) -> MonoidMap k v1 -> MonoidMap k v2 -> MonoidMap k v3
- intersectionWithA :: (Applicative f, Ord k, MonoidNull v3) => (v1 -> v2 -> f v3) -> MonoidMap k v1 -> MonoidMap k v2 -> f (MonoidMap k v3)
- union :: (Ord k, MonoidNull v, LCMMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v
- unionWith :: (Ord k, Monoid v1, Monoid v2, MonoidNull v3) => (v1 -> v2 -> v3) -> MonoidMap k v1 -> MonoidMap k v2 -> MonoidMap k v3
- unionWithA :: (Applicative f, Ord k, Monoid v1, Monoid v2, MonoidNull v3) => (v1 -> v2 -> f v3) -> MonoidMap k v1 -> MonoidMap k v2 -> f (MonoidMap k v3)
- isPrefixOf :: (Ord k, Monoid v, LeftReductive v) => MonoidMap k v -> MonoidMap k v -> Bool
- stripPrefix :: (Ord k, MonoidNull v, LeftReductive v) => MonoidMap k v -> MonoidMap k v -> Maybe (MonoidMap k v)
- commonPrefix :: (Ord k, MonoidNull v, LeftGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v
- stripCommonPrefix :: (Ord k, MonoidNull v, LeftGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> (MonoidMap k v, MonoidMap k v, MonoidMap k v)
- isSuffixOf :: (Ord k, Monoid v, RightReductive v) => MonoidMap k v -> MonoidMap k v -> Bool
- stripSuffix :: (Ord k, MonoidNull v, RightReductive v) => MonoidMap k v -> MonoidMap k v -> Maybe (MonoidMap k v)
- commonSuffix :: (Ord k, MonoidNull v, RightGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v
- stripCommonSuffix :: (Ord k, MonoidNull v, RightGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> (MonoidMap k v, MonoidMap k v, MonoidMap k v)
- overlap :: (Ord k, MonoidNull v, OverlappingGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v
- stripPrefixOverlap :: (Ord k, MonoidNull v, OverlappingGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v
- stripSuffixOverlap :: (Ord k, MonoidNull v, OverlappingGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v
- stripOverlap :: (Ord k, MonoidNull v, OverlappingGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> (MonoidMap k v, MonoidMap k v, MonoidMap k v)
Introduction
This module provides the MonoidMap
type, which:
- models a total function with
+
Data.MonoidMap Copyright © 2022–2024 Jonathan Knowles License Apache-2.0 Safe Haskell None Language Haskell2010 Synopsis
- data MonoidMap k v
- empty :: MonoidMap k v
- fromList :: (Ord k, MonoidNull v) => [(k, v)] -> MonoidMap k v
- fromListWith :: (Ord k, MonoidNull v) => (v -> v -> v) -> [(k, v)] -> MonoidMap k v
- fromMap :: MonoidNull v => Map k v -> MonoidMap k v
- singleton :: (Ord k, MonoidNull v) => k -> v -> MonoidMap k v
- toList :: MonoidMap k v -> [(k, v)]
- toMap :: MonoidMap k v -> Map k v
- get :: (Ord k, Monoid v) => k -> MonoidMap k v -> v
- set :: (Ord k, MonoidNull v) => k -> v -> MonoidMap k v -> MonoidMap k v
- adjust :: (Ord k, MonoidNull v) => (v -> v) -> k -> MonoidMap k v -> MonoidMap k v
- nullify :: Ord k => k -> MonoidMap k v -> MonoidMap k v
- null :: MonoidMap k v -> Bool
- nullKey :: Ord k => k -> MonoidMap k v -> Bool
- nonNull :: MonoidMap k v -> Bool
- nonNullCount :: MonoidMap k v -> Int
- nonNullKey :: Ord k => k -> MonoidMap k v -> Bool
- nonNullKeys :: MonoidMap k v -> Set k
- take :: Int -> MonoidMap k v -> MonoidMap k v
- drop :: Int -> MonoidMap k v -> MonoidMap k v
- splitAt :: Int -> MonoidMap k a -> (MonoidMap k a, MonoidMap k a)
- filter :: (v -> Bool) -> MonoidMap k v -> MonoidMap k v
- filterKeys :: (k -> Bool) -> MonoidMap k v -> MonoidMap k v
- filterWithKey :: (k -> v -> Bool) -> MonoidMap k v -> MonoidMap k v
- partition :: (v -> Bool) -> MonoidMap k v -> (MonoidMap k v, MonoidMap k v)
- partitionKeys :: (k -> Bool) -> MonoidMap k v -> (MonoidMap k v, MonoidMap k v)
- partitionWithKey :: (k -> v -> Bool) -> MonoidMap k v -> (MonoidMap k v, MonoidMap k v)
- map :: MonoidNull v2 => (v1 -> v2) -> MonoidMap k v1 -> MonoidMap k v2
- mapKeys :: (Ord k2, MonoidNull v) => (k1 -> k2) -> MonoidMap k1 v -> MonoidMap k2 v
- mapKeysWith :: (Ord k2, MonoidNull v) => (v -> v -> v) -> (k1 -> k2) -> MonoidMap k1 v -> MonoidMap k2 v
- append :: (Ord k, MonoidNull v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v
- minus :: (Ord k, MonoidNull v, Group v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v
- minusMaybe :: (Ord k, MonoidNull v, Reductive v) => MonoidMap k v -> MonoidMap k v -> Maybe (MonoidMap k v)
- monus :: (Ord k, MonoidNull v, Monus v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v
- invert :: (MonoidNull v, Group v) => MonoidMap k v -> MonoidMap k v
- power :: (Integral i, MonoidNull v, Group v) => MonoidMap k v -> i -> MonoidMap k v
- isSubmapOf :: (Ord k, Monoid v, Reductive v) => MonoidMap k v -> MonoidMap k v -> Bool
- isSubmapOfBy :: (Ord k, Monoid v1, Monoid v2) => (v1 -> v2 -> Bool) -> MonoidMap k v1 -> MonoidMap k v2 -> Bool
- disjoint :: (Ord k, GCDMonoid v, MonoidNull v) => MonoidMap k v -> MonoidMap k v -> Bool
- disjointBy :: (Ord k, Monoid v1, Monoid v2) => (v1 -> v2 -> Bool) -> MonoidMap k v1 -> MonoidMap k v2 -> Bool
- intersection :: (Ord k, MonoidNull v, GCDMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v
- intersectionWith :: (Ord k, MonoidNull v3) => (v1 -> v2 -> v3) -> MonoidMap k v1 -> MonoidMap k v2 -> MonoidMap k v3
- intersectionWithA :: (Applicative f, Ord k, MonoidNull v3) => (v1 -> v2 -> f v3) -> MonoidMap k v1 -> MonoidMap k v2 -> f (MonoidMap k v3)
- union :: (Ord k, MonoidNull v, LCMMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v
- unionWith :: (Ord k, Monoid v1, Monoid v2, MonoidNull v3) => (v1 -> v2 -> v3) -> MonoidMap k v1 -> MonoidMap k v2 -> MonoidMap k v3
- unionWithA :: (Applicative f, Ord k, Monoid v1, Monoid v2, MonoidNull v3) => (v1 -> v2 -> f v3) -> MonoidMap k v1 -> MonoidMap k v2 -> f (MonoidMap k v3)
- isPrefixOf :: (Ord k, Monoid v, LeftReductive v) => MonoidMap k v -> MonoidMap k v -> Bool
- stripPrefix :: (Ord k, MonoidNull v, LeftReductive v) => MonoidMap k v -> MonoidMap k v -> Maybe (MonoidMap k v)
- commonPrefix :: (Ord k, MonoidNull v, LeftGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v
- stripCommonPrefix :: (Ord k, MonoidNull v, LeftGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> (MonoidMap k v, MonoidMap k v, MonoidMap k v)
- isSuffixOf :: (Ord k, Monoid v, RightReductive v) => MonoidMap k v -> MonoidMap k v -> Bool
- stripSuffix :: (Ord k, MonoidNull v, RightReductive v) => MonoidMap k v -> MonoidMap k v -> Maybe (MonoidMap k v)
- commonSuffix :: (Ord k, MonoidNull v, RightGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v
- stripCommonSuffix :: (Ord k, MonoidNull v, RightGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> (MonoidMap k v, MonoidMap k v, MonoidMap k v)
- overlap :: (Ord k, MonoidNull v, OverlappingGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v
- stripPrefixOverlap :: (Ord k, MonoidNull v, OverlappingGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v
- stripSuffixOverlap :: (Ord k, MonoidNull v, OverlappingGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v
- stripOverlap :: (Ord k, MonoidNull v, OverlappingGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> (MonoidMap k v, MonoidMap k v, MonoidMap k v)
Introduction
This module provides the
MonoidMap
type, which:- models a total function with
finite support
- from keys to monoidal values, with a default value
- of
mempty
. - encodes key-value mappings with a minimal encoding that
- only includes values not equal to
mempty
. - provides a comprehensive set of
+ from keys to monoidal values, with a default value
+ of
mempty
. - encodes key-value mappings with a minimal encoding that
+ only includes values not equal to
mempty
. - provides a comprehensive set of monoidal operations for transforming, combining, and comparing maps.
The documentation in this module serves as reference guide to the
MonoidMap
type and its operations.See the
README
file for:Relationship between keys and values
A
MonoidMap
of key typek
and value typev
associates every - possible key of typek
with a value of typev
:get
:: (Ord
k,Monoid
v) => k ->MonoidMap
k v -> v + possible key of typek
with a value of typev
:get
:: (Ord
k,Monoid
v) => k ->MonoidMap
k v -> vThe
empty
map associates every keyk
with a default value of -mempty
:∀ k.
get
kempty
==
mempty
+mempty
:∀ k.
get
kempty
==
mempty
Comparison with standard
Map
typeThe
MonoidMap
type differs from the standardMap
type in how it relates keys to values:Type Models a total function with finite support Map
k v
from keys of type k
-to values of type
.Maybe
vMonoidMap
k v
+to values of type
.Maybe
vMonoidMap
k v
from keys of type k
to values of typev
.This difference can be illustrated by comparing the type signatures of - operations to query a key for its value, for both types:
Map
.lookup
:: k ->Map
k v ->Maybe
v -MonoidMap
.get
::Monoid
v => k ->MonoidMap
k v -> v -Whereas a standard
Map
has a default value ofNothing
, aMonoidMap
has - a default value ofmempty
:∀ k.
Map
.lookup
kMap
.empty
==
Nothing
-∀ k.MonoidMap
.get
kMonoidMap
.empty
==
mempty
-In practice, the standard
Map
type usesMaybe
to indicate the presence + operations to query a key for its value, for both types:Map
.lookup
:: k ->Map
k v ->Maybe
v +MonoidMap
.get
::Monoid
v => k ->MonoidMap
k v -> v +Whereas a standard
Map
has a default value ofNothing
, aMonoidMap
has + a default value ofmempty
:∀ k.
Map
.lookup
kMap
.empty
==
Nothing
+∀ k.MonoidMap
.get
kMonoidMap
.empty
==
mempty
+In practice, the standard
Map
type usesMaybe
to indicate the presence or absence of a value for a particular key. This representation is necessary because theMap
type imposes no constraints on value types.However, monoidal types already have a natural way to represent null or - empty values: the
mempty
constant, which represents the neutral or - identity element of aMonoid
.Consequently, using a standard
Map
with a monoidal value type gives rise + empty values: themempty
constant, which represents the neutral or + identity element of aMonoid
.Consequently, using a standard
Map
with a monoidal value type gives rise to two distinct representations for null or empty values:Map
.lookup
k mInterpretation -
Nothing
+Nothing
Map
m
has no entry for keyk
.-
Just
mempty
+Just
mempty
Map
m
has an entry for keyk
, but the value is empty.In constrast, the
MonoidMap
type provides a single, canonical @@ -49,694 +49,694 @@MonoidMap
.get
k m-
Nothing
+Nothing
⟼ -
mempty
+mempty
-
Just
v +Just
v-v
==
mempty
+v==
mempty
⟼ -
mempty
+mempty
-
Just
v +Just
v-v
/=
mempty
+v/=
mempty
⟼ v
Encoding
A
MonoidMap
only encodes mappings from keys to values that are not equal - tomempty
.The total function \(T\) modelled by a
MonoidMap
is encoded as a + tomempty
.The total function \(T\) modelled by a
MonoidMap
is encoded as a support map \(S\), where \(S\) is the finite subset of key-value - mappings in \(T\) for which values are not equal tomempty
(denoted by + mappings in \(T\) for which values are not equal tomempty
(denoted by \(\varnothing\)):\( \quad S = \{ (k, v) \in T \ | \ v \ne \varnothing \} \)
Automatic minimisation
All
MonoidMap
operations perform automatic minimisation of the support - map, so thatmempty
values do not appear in:Constraints on values
MonoidMap
operations require the monoidal value type to be an instance of + map, so thatmempty
values do not appear in:Constraints on values
MonoidMap
operations require the monoidal value type to be an instance ofMonoidNull
.Instances of
MonoidNull
must provide anull
indicator function that - satisfies the following law:∀ v.
MonoidNull
.null
v==
(v==
mempty
) + satisfies the following law:∀ v.
MonoidNull
.null
v==
(v==
mempty
)MonoidMap
operations use thenull
indicator function to detect and - excludemempty
values from the support map.Note that it is not generally necessary for the value type to be an - instance of
Eq
.Monoidal operations
The
MonoidMap
type provides a comprehensive set of monoidal operations - for transforming, combining, and comparing maps.Instances for several subclasses of
Semigroup
andMonoid
are - provided, including classes from the following libraries:At the root of this hierarchy of subclasses is the
Semigroup
class, whose + excludemempty
values from the support map.Note that it is not generally necessary for the value type to be an + instance of
Eq
.Monoidal operations
The
MonoidMap
type provides a comprehensive set of monoidal operations + for transforming, combining, and comparing maps.Instances for several subclasses of
Semigroup
andMonoid
are + provided, including classes from the following libraries:At the root of this hierarchy of subclasses is the
Semigroup
class, whose instance forMonoidMap
is defined in terms of the underlying value type, - so that applying(<>)
to a pair of maps is equivalent to applying(<>)
- to all pairs of values for matching keys:∀ k.
get
k (m1<>
m2)==
get
k m1<>
get
k m2 -In general, operations for subclasses of
Semigroup
andMonoid
are - defined analogously to theSemigroup
instance, so that:- unary operations on individual maps are defined in terms of their
+ so that applying
(<>)
to a pair of maps is equivalent to applying(<>)
+ to all pairs of values for matching keys:∀ k.
get
k (m1<>
m2)==
get
k m1<>
get
k m2 +In general, operations for subclasses of
Semigroup
andMonoid
are + defined analogously to theSemigroup
instance, so that:- unary operations on individual maps are defined in terms of their distributive application to all values.
- binary operations on pairs of maps are defined in terms of their - distributive application to all pairs of values for matching keys.
Unary monoidal operations typically satisfy a property similar to:
∀ k.
get
k (f m)==
f (get
k m) -Binary monoidal operations typically satisfy a property similar to:
∀ k.
get
k (f m1 m2)==
f (get
k m1) (get
k m2) + distributive application to all pairs of values for matching keys.
Unary monoidal operations typically satisfy a property similar to:
∀ k.
get
k (f m)==
f (get
k m) +Binary monoidal operations typically satisfy a property similar to:
∀ k.
get
k (f m1 m2)==
f (get
k m1) (get
k m2)Defining monoidal operations in this way makes it possible to transform, combine, and compare maps in ways that are consistent with the algebraic - properties of the underlying monoidal value type.
Type
Instances
General operations
Construction
\ No newline at end of file +fromList :: (Ord k, MonoidNull v) => [(k, v)] -> MonoidMap k v #
\(O(n \log n)\). Constructs a
MonoidMap
from a list of key-value pairs.If the list contains more than one value for the same key, values are - combined together in the order that they appear with the
(<>)
operator.Satisfies the following property for all possible keys
k
:get
k (fromList
kvs)==
-foldMap
snd
(filter
((==
k) . fst) kvs) -Satisfies the following round-trip property:
fromList
(toList
m)==
m -Examples
Type
Instances
General operations
Construction
fromList :: (Ord k, MonoidNull v) => [(k, v)] -> MonoidMap k v #
\(O(n \log n)\). Constructs a
MonoidMap
from a list of key-value pairs.If the list contains more than one value for the same key, values are + combined together in the order that they appear with the
(<>)
operator.Satisfies the following property for all possible keys
k
:get
k (fromList
kvs)==
+foldMap
snd
(filter
((==
k) . fst) kvs) +Satisfies the following round-trip property:
fromList
(toList
m)==
m +Examples
:: (Ord k, MonoidNull v) => (v -> v -> v) Function with which to combine values for duplicate keys.
-> [(k, v)] -> MonoidMap k v \(O(n \log n)\). Constructs a
MonoidMap
from a list of key-value pairs, +:: (Ord k, MonoidNull v) => (v -> v -> v) Function with which to combine values for duplicate keys.
-> [(k, v)] -> MonoidMap k v \(O(n \log n)\). Constructs a
MonoidMap
from a list of key-value pairs, with a combining function for values.If the list contains more than one value for the same key, values are combined together in the order that they appear with the given combining - function.
Satisfies the following property for all possible keys
k
:get
k (fromListWith
f kvs)==
-maybe
mempty
(foldl1
f) - (nonEmpty
(snd
<$>
filter
((==
k) . fst) kvs)) -fromMap :: MonoidNull v => Map k v -> MonoidMap k v #
\(O(n)\). Constructs a
MonoidMap
from an ordinaryMap
.Satisfies the following property for all possible keys
k
:get
k (fromMap
m)==
findWithDefault
mempty
k
m + function.Satisfies the following property for all possible keys
k
:get
k (fromListWith
f kvs)==
+maybe
mempty
(foldl1
f) + (nonEmpty
(snd
<$>
filter
((==
k) . fst) kvs)) +fromMap :: MonoidNull v => Map k v -> MonoidMap k v #
singleton :: (Ord k, MonoidNull v) => k -> v -> MonoidMap k v #
singleton :: (Ord k, MonoidNull v) => k -> v -> MonoidMap k v #
Deconstruction
toList :: MonoidMap k v -> [(k, v)] #
\(O(n)\). Converts a
MonoidMap
to a list of key-value pairs, where the - keys are in ascending order.The result only includes entries with values that are not
null
.Satisfies the following round-trip property:
fromList
(toList
m)==
m -The resulting list is sorted in ascending key order:
sortOn
fst
(toList
m)==
toList
m + keys are in ascending order.The result only includes entries with values that are not
null
.Satisfies the following round-trip property:
fromList
(toList
m)==
m +The resulting list is sorted in ascending key order:
sortOn
fst
(toList
m)==
toList
mLookup
Modification
Membership
Lookup
Modification
Membership
nonNull :: MonoidMap k v -> Bool #
\(O(1)\). Returns
True
if (and only if) the map contains at least one - value that is notnull
.Satisfies the following property:
nonNull
m==
(∃ k.nonNullKey
k m) -nonNullCount :: MonoidMap k v -> Int #
\(O(1)\). Returns a count of all values in the map that are not
null
.Satisfies the following property:
nonNullCount
m==
size
(nonNullKeys
m) -nonNullKey :: Ord k => k -> MonoidMap k v -> Bool #
\(O(\log n)\). Returns
True
if (and only if) the given key is associated - with a value that is notnull
.Satisfies the following property:
nonNullKey
k m==
not
(null
(get
k m)) +MonoidNull
.nonNull :: MonoidMap k v -> Bool #
\(O(1)\). Returns
True
if (and only if) the map contains at least one + value that is notnull
.Satisfies the following property:
nonNull
m==
(∃ k.nonNullKey
k m) +nonNullCount :: MonoidMap k v -> Int #
\(O(1)\). Returns a count of all values in the map that are not
null
.Satisfies the following property:
nonNullCount
m==
size
(nonNullKeys
m) +nonNullKey :: Ord k => k -> MonoidMap k v -> Bool #
nonNullKeys :: MonoidMap k v -> Set k #
\(O(n)\). Returns the set of keys associated with values that are not -
null
.Satisfies the following property:
k
`member`
(nonNullKeys
m)==
nonNullKey
k m -Slicing
splitAt :: Int -> MonoidMap k a -> (MonoidMap k a, MonoidMap k a) #
\(O(\log n)\). Splits a map into two slices.
This function is equivalent to a combination of
take
anddrop
:splitAt
n m==
(take
n m,drop
n m) -The resulting maps can be combined to reproduce the original map:
splitAt
n m&
- \(m1, m2) -> m1<>
m2==
m -The resulting maps have disjoint sets of non-
null
entries:splitAt
n m&
+null
.Satisfies the following property:
k
`member`
(nonNullKeys
m)==
nonNullKey
k m +Slicing
splitAt :: Int -> MonoidMap k a -> (MonoidMap k a, MonoidMap k a) #
\(O(\log n)\). Splits a map into two slices.
This function is equivalent to a combination of
take
anddrop
:splitAt
n m==
(take
n m,drop
n m) +The resulting maps can be combined to reproduce the original map:
splitAt
n m&
+ \(m1, m2) -> m1<>
m2==
m +The resulting maps have disjoint sets of non-
null
entries:splitAt
n m&
\(m1, m2) ->disjoint
(nonNullKeys
m1) (nonNullKeys
m2) -Filtering
Filtering
filter :: (v -> Bool) -> MonoidMap k v -> MonoidMap k v #
\(O(n)\). Filters a map according to a predicate on values.
Satisfies the following property for all possible keys
k
:get
k (filter
f m)==
if f (get
k m) thenget
k m - elsemempty
+ elsemempty
The resulting map is identical to that obtained by constructing a map from a - filtered list of key-value pairs:
filter
f m==
fromList
(filter
(f .snd
) (toList
m)) -filterKeys :: (k -> Bool) -> MonoidMap k v -> MonoidMap k v #
filterKeys :: (k -> Bool) -> MonoidMap k v -> MonoidMap k v #
\(O(n)\). Filters a map according to a predicate on keys.
Satisfies the following property for all possible keys
k
:get
k (filterKeys
f m)==
if f k thenget
k m - elsemempty
+ elsemempty
The resulting map is identical to that obtained by constructing a map from a - filtered list of key-value pairs:
filter
f m==
fromList
(filter
(f .fst
) (toList
m)) -filterWithKey :: (k -> v -> Bool) -> MonoidMap k v -> MonoidMap k v #
filterWithKey :: (k -> v -> Bool) -> MonoidMap k v -> MonoidMap k v #
\(O(n)\). Filters a map according to a predicate on keys and values.
Satisfies the following property for all possible keys
k
:get
k (filterWithKey
f m)==
if f k (get
k m) thenget
k m - elsemempty
+ elsemempty
The resulting map is identical to that obtained by constructing a map from a - filtered list of key-value pairs:
filterWithKey
f m==
fromList
(filter
(uncurry
f) (toList
m)) -Partitioning
Partitioning
partition :: (v -> Bool) -> MonoidMap k v -> (MonoidMap k v, MonoidMap k v) #
\(O(n)\). Partitions a map according to a predicate on values.
Satisfies the following property:
partition
f m==
(filter
f m - ,filter
(not
. f) m + ,filter
(not
. f) m ) -The resulting maps can be combined to reproduce the original map:
partition
f m&
\(m1, m2) -> - m1<>
m2==
m -The resulting maps have disjoint sets of non-
null
entries:partition
f m&
\(m1, m2) -> +The resulting maps can be combined to reproduce the original map:
partition
f m&
\(m1, m2) -> + m1<>
m2==
m +The resulting maps have disjoint sets of non-
null
entries:partition
f m&
\(m1, m2) ->disjoint
(nonNullKeys
m1) (nonNullKeys
m2) -partitionKeys :: (k -> Bool) -> MonoidMap k v -> (MonoidMap k v, MonoidMap k v) #
\(O(n)\). Partitions a map according to a predicate on keys.
Satisfies the following property:
partitionKeys
f m==
+partitionKeys :: (k -> Bool) -> MonoidMap k v -> (MonoidMap k v, MonoidMap k v) #
\(O(n)\). Partitions a map according to a predicate on keys.
Satisfies the following property:
partitionKeys
f m==
(filterKeys
f m - ,filterKeys
(not
. f) m + ,filterKeys
(not
. f) m ) -The resulting maps can be combined to reproduce the original map:
partitionKeys
f m&
\(m1, m2) -> - m1<>
m2==
m -The resulting maps have disjoint sets of non-
null
entries:partitionKeys
f m&
\(m1, m2) -> +The resulting maps can be combined to reproduce the original map:
partitionKeys
f m&
\(m1, m2) -> + m1<>
m2==
m +The resulting maps have disjoint sets of non-
null
entries:partitionKeys
f m&
\(m1, m2) ->disjoint
(nonNullKeys
m1) (nonNullKeys
m2) -partitionWithKey :: (k -> v -> Bool) -> MonoidMap k v -> (MonoidMap k v, MonoidMap k v) #
\(O(n)\). Partitions a map according to a predicate on keys and values.
Satisfies the following property:
partitionWithKey
f m==
+partitionWithKey :: (k -> v -> Bool) -> MonoidMap k v -> (MonoidMap k v, MonoidMap k v) #
\(O(n)\). Partitions a map according to a predicate on keys and values.
Satisfies the following property:
partitionWithKey
f m==
(filterWithKey
f m - ,filterWithKey
((fmap
.fmap
)not
f) m + ,filterWithKey
((fmap
.fmap
)not
f) m ) -The resulting maps can be combined to reproduce the original map:
partitionWithKey
f m&
\(m1, m2) -> - m1<>
m2==
m -The resulting maps have disjoint sets of non-
null
entries:partitionWithKey
f m&
\(m1, m2) -> +The resulting maps can be combined to reproduce the original map:
partitionWithKey
f m&
\(m1, m2) -> + m1<>
m2==
m +The resulting maps have disjoint sets of non-
null
entries:partitionWithKey
f m&
\(m1, m2) ->disjoint
(nonNullKeys
m1) (nonNullKeys
m2) -Mapping
map :: MonoidNull v2 => (v1 -> v2) -> MonoidMap k v1 -> MonoidMap k v2 #
\(O(n)\). Applies a function to all non-
null
values of aMonoidMap
.Satisfies the following properties for all functions
f
:(
get
k m==
mempty
) ==> (get
k (map
f m)==
mempty
) -(get
k m/=
mempty
) ==> (get
k (map
f m)==
f (get
k m)) -Conditional properties
If applying function
f
tomempty
producesmempty
, then the - following additional properties hold:(f
mempty
==
mempty
) +Mapping
map :: MonoidNull v2 => (v1 -> v2) -> MonoidMap k v1 -> MonoidMap k v2 #
\(O(n)\). Applies a function to all non-
null
values of aMonoidMap
.Satisfies the following properties for all functions
f
:(
get
k m==
mempty
) ==> (get
k (map
f m)==
mempty
) +(get
k m/=
mempty
) ==> (get
k (map
f m)==
f (get
k m)) +Conditional properties
If applying function
f
tomempty
producesmempty
, then the + following additional properties hold:(f
mempty
==
mempty
) ==> - (∀ k.get
k (map
f m)==
f (get
k m)) -(f
mempty
==
mempty
) + (∀ k.get
k (map
f m)==
f (get
k m)) +(f
mempty
==
mempty
) ==> - (∀ g.map
(f . g) m==
map
f (map
g m)) -mapKeys :: (Ord k2, MonoidNull v) => (k1 -> k2) -> MonoidMap k1 v -> MonoidMap k2 v #
\(O(n \log n)\). Applies a function to all the keys of a
MonoidMap
that are associated with non-null
values.If the resultant map would contain more than one value for the same key, - values are combined together in ascending key order with the
(<>)
- operator.Satisfies the following property for all possible keys
k
:get
k (mapKeys
f m)==
+ values are combined together in ascending key order with the(<>)
+ operator.Satisfies the following property for all possible keys
k
:get
k (mapKeys
f m)==
foldMap
(`get`
m) - (filter
((==
) k . f) (nonNullKeys
m)) -:: (Ord k2, MonoidNull v) => (v -> v -> v) Function with which to combine values for duplicate keys.
-> (k1 -> k2) -> MonoidMap k1 v -> MonoidMap k2 v \(O(n \log n)\). Applies a function to all the keys of a
MonoidMap
that + (filter
((==
) k . f) (nonNullKeys
m)) +:: (Ord k2, MonoidNull v) => (v -> v -> v) Function with which to combine values for duplicate keys.
-> (k1 -> k2) -> MonoidMap k1 v -> MonoidMap k2 v \(O(n \log n)\). Applies a function to all the keys of a
MonoidMap
that are associated with non-null
values, with a combining function for values.If the resultant map would contain more than one value for the same key, values are combined together in ascending key order with the given - combining function.
Satisfies the following property:
mapKeysWith
c f==
fromListWith
c .fmap
(first
f) .toList
+ combining function.Satisfies the following property:
mapKeysWith
c f==
fromListWith
c .fmap
(first
f) .toList
Monoidal operations
See the section on monoidal operations within - the introduction.
Association
append :: (Ord k, MonoidNull v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v #
Appends a pair of maps together.
Uses the
Semigroup
operator(<>)
to append each value in the first map - to its matching value in the second map.Satisfies the following property for all possible keys
k
:get
k (append
m1 m2)==
get
k m1<>
get
k m2 -This function provides the definition of
(<>)
for theMonoidMap
instance - ofSemigroup
.Examples
With
String
values:>>> m1 =
fromList
[(1, "abc"), (2, "ij" ), (3, "p" ) ] + the introduction.Association
append :: (Ord k, MonoidNull v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v #
Appends a pair of maps together.
Uses the
Semigroup
operator(<>)
to append each value in the first map + to its matching value in the second map.Satisfies the following property for all possible keys
k
:get
k (append
m1 m2)==
get
k m1<>
get
k m2 +This function provides the definition of
(<>)
for theMonoidMap
instance + ofSemigroup
.Examples
With
String
values:>>> m1 =
fromList
[(1, "abc"), (2, "ij" ), (3, "p" ) ] >>> m2 =fromList
[ (2, " k"), (3, "qr"), (4, "xyz")] >>> m3 =fromList
[(1, "abc"), (2, "ijk"), (3, "pqr"), (4, "xyz")] ->>>
append
m1 m2==
m3 -True
->>> m1 =
fromList
[("a", 4), ("b", 2), ("c", 1) ] +>>>
append
m1 m2==
m3 +True
+>>> m1 =
fromList
[("a", 4), ("b", 2), ("c", 1) ] >>> m2 =fromList
[ ("b", 1), ("c", 2), ("d", 4)] >>> m3 =fromList
[("a", 4), ("b", 3), ("c", 3), ("d", 4)] ->>>
append
m1 m2==
m3 -True
-Subtraction
minus :: (Ord k, MonoidNull v, Group v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v #
Performs group subtraction of the second map from the first.
Uses the
Group
subtraction operator(~~)
to subtract each value in the - second map from its matching value in the first map.Satisfies the following property for all possible keys
k
:get
k (m1`minus`
m2)==
get
k m1~~
get
k m2 +>>>
append
m1 m2==
m3 +True
+Subtraction
minus :: (Ord k, MonoidNull v, Group v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v #
Performs group subtraction of the second map from the first.
Uses the
Group
subtraction operator(~~)
to subtract each value in the + second map from its matching value in the first map.Satisfies the following property for all possible keys
k
:get
k (m1`minus`
m2)==
get
k m1~~
get
k m2This function provides the definition of
(~~)
for theMonoidMap
- instance ofGroup
.Examples
With
Sum
Integer
values, this function performs normal + instance ofGroup
.Examples
With
Sum
Integer
values, this function performs normal integer subtraction of matching values:>>> m1 =
fromList
[("a", (-1)), ("b", 0 ), ("c", 1)] >>> m2 =fromList
[("a", 1 ), ("b", 1 ), ("c", 1)] >>> m3 =fromList
[("a", (-2)), ("b", (-1)), ("c", 0)] ->>> m1
`minus`
m2==
m3 -True
+>>> m1
`minus`
m2==
m3 +True
>>> m1 =
fromList
[("a", (-1)), ("b", 0 ), ("c", 1 )] >>> m2 =fromList
[("a", (-1)), ("b", (-1)), ("c", (-1))] >>> m3 =fromList
[("a", 0 ), ("b", 1 ), ("c", 2 )] ->>> m1
`minus`
m2==
m3 -True
-minusMaybe :: (Ord k, MonoidNull v, Reductive v) => MonoidMap k v -> MonoidMap k v -> Maybe (MonoidMap k v) #
minusMaybe :: (Ord k, MonoidNull v, Reductive v) => MonoidMap k v -> MonoidMap k v -> Maybe (MonoidMap k v) #
Performs reductive subtraction of the second map from the first.
Uses the
Reductive
subtraction operator(</>)
to subtract each value in the second map from its matching value in the first map.This function produces a result if (and only if) for all possible keys
k
, it is possible to subtract the value fork
in the second map - from the value fork
in the first map:isJust
(m1`minusMaybe`
m2) -==
(∀ k.isJust
(get
k m1</>
get
k m2)) -Otherwise, this function returns
Nothing
.This function satisfies the following property:
all
- (\r ->Just
(get
k r)==
get
k m1</>
get
k m2) + from the value fork
in the first map:isJust
(m1`minusMaybe`
m2) +==
(∀ k.isJust
(get
k m1</>
get
k m2)) +Otherwise, this function returns
Nothing
.This function satisfies the following property:
all
+ (\r ->Just
(get
k r)==
get
k m1</>
get
k m2) (m1`minusMaybe`
m2)This function provides the definition of
(</>)
for theMonoidMap
- instance ofReductive
.Examples
With
Set
Natural
values, this function performs set + instance ofReductive
.Examples
With
Set
Natural
values, this function performs set subtraction of matching values, succeeding if (and only if) each value - from the second map is a subset of its matching value from the first map:f xs =
fromList
(fromList
<$>
xs) + from the second map is a subset of its matching value from the first map:f xs =
fromList
(fromList
<$>
xs)>>> m1 = f [("a", [0,1,2]), ("b", [0,1,2])] >>> m2 = f [("a", [ ]), ("b", [0,1,2])] >>> m3 = f [("a", [0,1,2]), ("b", [ ])] -
>>> m1
`minusMaybe`
m2==
Just
m3 -True
+>>> m1
`minusMaybe`
m2==
Just
m3 +True
>>> m1 = f [("a", [0,1,2]), ("b", [0,1,2]), ("c", [0,1,2])] >>> m2 = f [("a", [0 ]), ("b", [ 1 ]), ("c", [ 2])] >>> m3 = f [("a", [ 1,2]), ("b", [0, 2]), ("c", [0,1 ])] -
>>> m1
`minusMaybe`
m2==
Just
m3 -True
+>>> m1
`minusMaybe`
m2==
Just
m3 +True
>>> m1 = f [("a", [0,1,2 ]), ("b", [0,1,2 ]), ("c", [0,1,2 ])] >>> m2 = f [("a", [ 2,3,4]), ("b", [ 1,2,3,4]), ("c", [0,1,2,3,4])] -
>>> m1
`minusMaybe`
m2==
Nothing
-True
->>> m1
`minusMaybe`
m2==
Nothing
+True
+With
Sum
Natural
values, this function performs ordinary subtraction of matching values, succeeding if (and only if) each value from the second map is less than or equal to its matching value from the first map:>>> m1 =
fromList
[("a", 2), ("b", 3), ("c", 5), ("d", 8)] >>> m2 =fromList
[("a", 0), ("b", 0), ("c", 0), ("d", 0)] >>> m3 =fromList
[("a", 2), ("b", 3), ("c", 5), ("d", 8)] ->>> m1
`minusMaybe`
m2==
Just
m3 -True
+>>> m1
`minusMaybe`
m2==
Just
m3 +True
>>> m1 =
fromList
[("a", 2), ("b", 3), ("c", 5), ("d", 8)] >>> m2 =fromList
[("a", 1), ("b", 2), ("c", 3), ("d", 5)] >>> m3 =fromList
[("a", 1), ("b", 1), ("c", 2), ("d", 3)] ->>> m1
`minusMaybe`
m2==
Just
m3 -True
+>>> m1
`minusMaybe`
m2==
Just
m3 +True
>>> m1 =
fromList
[("a", 2), ("b", 3), ("c", 5), ("d", 8)] >>> m2 =fromList
[("a", 2), ("b", 3), ("c", 5), ("d", 8)] >>> m3 =fromList
[("a", 0), ("b", 0), ("c", 0), ("d", 0)] ->>> m1
`minusMaybe`
m2==
Just
m3 -True
+>>> m1
`minusMaybe`
m2==
Just
m3 +True
>>> m1 =
fromList
[("a", 2), ("b", 3), ("c", 5), ("d", 8)] >>> m2 =fromList
[("a", 3), ("b", 3), ("c", 5), ("d", 8)] ->>> m1
`minusMaybe`
m2==
Nothing
-True
-monus :: (Ord k, MonoidNull v, Monus v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v #
Performs monus subtraction of the second map from the first.
Uses the
Monus
subtraction operator(<\>)
to subtract each value in - the second map from its matching value in the first map.Satisfies the following property for all possible keys
k
:get
k (m1`monus`
m2)==
get
k m1<\>
get
k m2 +>>> m1
`minusMaybe`
m2==
Nothing
+True
+monus :: (Ord k, MonoidNull v, Monus v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v #
Performs monus subtraction of the second map from the first.
Uses the
Monus
subtraction operator(<\>)
to subtract each value in + the second map from its matching value in the first map.Satisfies the following property for all possible keys
k
:get
k (m1`monus`
m2)==
get
k m1<\>
get
k m2This function provides the definition of
(<\>)
for theMonoidMap
- instance ofMonus
.Examples
With
Set
Natural
values, this function performs set - subtraction of matching values:f xs =
fromList
(fromList
<$>
xs) + instance ofMonus
.Examples
With
Set
Natural
values, this function performs set + subtraction of matching values:f xs =
fromList
(fromList
<$>
xs)>>> m1 = f [("a", [0,1,2]), ("b", [0,1,2])] >>> m2 = f [("a", [ ]), ("b", [0,1,2])] >>> m3 = f [("a", [0,1,2]), ("b", [ ])] -
>>> m1
`monus`
m2==
m3 -True
+>>> m1
`monus`
m2==
m3 +True
>>> m1 = f [("a", [0,1,2]), ("b", [0,1,2]), ("c", [0,1,2])] >>> m2 = f [("a", [0 ]), ("b", [ 1 ]), ("c", [ 2])] >>> m3 = f [("a", [ 1,2]), ("b", [0, 2]), ("c", [0,1 ])] -
>>> m1
`monus`
m2==
m3 -True
+>>> m1
`monus`
m2==
m3 +True
>>> m1 = f [("a", [0,1,2 ]), ("b", [0,1,2 ]), ("c", [0,1,2 ])] >>> m2 = f [("a", [ 2,3,4]), ("b", [ 1,2,3,4]), ("c", [0,1,2,3,4])] >>> m3 = f [("a", [0,1 ]), ("b", [0 ]), ("c", [ ])] -
>>> m1
`monus`
m2==
m3 -True
->>> m1
`monus`
m2==
m3 +True
+With
Sum
Natural
values, this function performs truncated subtraction of matching values:>>> m1 =
fromList
[("a", 0), ("b", 1), ("c", 2), ("d", 3)] >>> m2 =fromList
[("a", 0), ("b", 0), ("c", 0), ("d", 0)] >>> m3 =fromList
[("a", 0), ("b", 1), ("c", 2), ("d", 3)] ->>> m1
`monus`
m2==
m3 -True
+>>> m1
`monus`
m2==
m3 +True
>>> m1 =
fromList
[("a", 0), ("b", 1), ("c", 2), ("d", 3)] >>> m2 =fromList
[("a", 1), ("b", 1), ("c", 1), ("d", 1)] >>> m3 =fromList
[("a", 0), ("b", 0), ("c", 1), ("d", 2)] ->>> m1
`monus`
m2==
m3 -True
+>>> m1
`monus`
m2==
m3 +True
>>> m1 =
fromList
[("a", 0), ("b", 1), ("c", 2), ("d", 3)] >>> m2 =fromList
[("a", 2), ("b", 2), ("c", 2), ("d", 2)] >>> m3 =fromList
[("a", 0), ("b", 0), ("c", 0), ("d", 1)] ->>> m1
`monus`
m2==
m3 -True
+>>> m1
`monus`
m2==
m3 +True
>>> m1 =
fromList
[("a", 0), ("b", 1), ("c", 2), ("d", 3)] >>> m2 =fromList
[("a", 4), ("b", 4), ("c", 4), ("d", 4)] >>> m3 =fromList
[("a", 0), ("b", 0), ("c", 0), ("d", 0)] ->>> m1
`monus`
m2==
m3 -True
-Inversion
Inversion
invert :: (MonoidNull v, Group v) => MonoidMap k v -> MonoidMap k v #
Exponentiation
Exponentiation
power :: (Integral i, MonoidNull v, Group v) => MonoidMap k v -> i -> MonoidMap k v #
Performs exponentiation of every value in a map.
Uses the
Group
exponentiation methodpow
to raise every value in a map + to the power of the given exponent.Satisfies the following property for all possible keys
k
:get
k (m`power`
i)==
get
k m`pow`
iThis function provides the definition of
pow
for theMonoidMap
- instance ofGroup
.Examples
With
Sum
Natural
values, this function + instance ofGroup
.Examples
With
Sum
Natural
values, this function performs ordinary multiplication of all values by the given exponent:>>> m1 =
fromList
[("a", 0), ("b", 1), ("c", 2), ("d", 3)] >>> m2 =fromList
[("a", 0), ("b", 2), ("c", 4), ("d", 6)] ->>> m1
`power`
2==
m2 -True
+>>> m1
`power`
2==
m2 +True
>>> m1 =
fromList
[("a", 0), ("b", 1 ), ("c", 2 ), ("d", 3 )] >>> m2 =fromList
[("a", 0), ("b", (-1)), ("c", (-2)), ("d", (-3))] ->>> m1
`power`
(-1)==
m2 -True
-Comparison
isSubmapOf :: (Ord k, Monoid v, Reductive v) => MonoidMap k v -> MonoidMap k v -> Bool #
Indicates whether or not the first map is a submap of the second.
Map
m1
is a submap of mapm2
if (and only if)m1
can be - subtracted fromm2
with theminusMaybe
operation:m1
`isSubmapOf`
m2==
isJust
(m2`minusMaybe`
m1) +>>> m1
`power`
(-1)==
m2 +True
+Comparison
isSubmapOf :: (Ord k, Monoid v, Reductive v) => MonoidMap k v -> MonoidMap k v -> Bool #
Indicates whether or not the first map is a submap of the second.
Map
m1
is a submap of mapm2
if (and only if)m1
can be + subtracted fromm2
with theminusMaybe
operation:m1
`isSubmapOf`
m2==
isJust
(m2`minusMaybe`
m1)Equivalently, map
m1
is a submap of mapm2
if (and only if) for all possible keysk
, the value fork
inm1
can be - subtracted from the value fork
inm2
with the(</>)
operator:m1
`isSubmapOf`
m2==
(∀ k.isJust
(get
k m2</>
get
k m1)) -:: (Ord k, Monoid v1, Monoid v2) => (v1 -> v2 -> Bool) Function with which to compare values for matching keys.
-> MonoidMap k v1 -> MonoidMap k v2 -> Bool Indicates whether or not the first map is a submap of the second, using - the given function to compare values for matching keys.
Satisfies the following property:
isSubmapOfBy
f m1 m2==
-all
(\k -> f (get
k m1) (get
k m2)) (nonNullKeys
m1) -Conditional totality
If the given comparison function
f
always evaluates toTrue
- when its first argument ismempty
:∀ v. f
mempty
v -Then the following property holds:
isSubmapOfBy
f m1 m2==
(∀ k. f (get
k m1) (get
k m2)) -disjoint :: (Ord k, GCDMonoid v, MonoidNull v) => MonoidMap k v -> MonoidMap k v -> Bool #
Indicates whether or not a pair of maps are disjoint.
Maps
m1
andm2
are disjoint if (and only if) their intersection - is empty:disjoint
m1 m2==
(intersection
m1 m2==
mempty
) + subtracted from the value fork
inm2
with the(</>)
operator:m1
`isSubmapOf`
m2==
(∀ k.isJust
(get
k m2</>
get
k m1)) +:: (Ord k, Monoid v1, Monoid v2) => (v1 -> v2 -> Bool) Function with which to compare values for matching keys.
-> MonoidMap k v1 -> MonoidMap k v2 -> Bool Indicates whether or not the first map is a submap of the second, using + the given function to compare values for matching keys.
Satisfies the following property:
isSubmapOfBy
f m1 m2==
+all
(\k -> f (get
k m1) (get
k m2)) (nonNullKeys
m1) +Conditional totality
If the given comparison function
f
always evaluates toTrue
+ when its first argument ismempty
:∀ v. f
mempty
v +Then the following property holds:
isSubmapOfBy
f m1 m2==
(∀ k. f (get
k m1) (get
k m2)) +disjoint :: (Ord k, GCDMonoid v, MonoidNull v) => MonoidMap k v -> MonoidMap k v -> Bool #
Indicates whether or not a pair of maps are disjoint.
Maps
m1
andm2
are disjoint if (and only if) their intersection + is empty:disjoint
m1 m2==
(intersection
m1 m2==
mempty
)Equivalently, maps
m1
andm2
are disjoint if (and only if) for all possible keysk
, the values fork
inm1
andm2
- have agcd
that isnull
:disjoint
m1 m2==
(∀ k.null
(gcd
(get
k m1) (get
k m2))) -:: (Ord k, Monoid v1, Monoid v2) => (v1 -> v2 -> Bool) Function with which to test pairs of values for matching keys.
-> MonoidMap k v1 -> MonoidMap k v2 -> Bool Indicates whether or not a pair of maps are disjoint using the given + indicator function to test pairs of values for matching keys.
Satisfies the following property:
disjointBy
f m1 m2==
+all
(\k -> f (get
k m1) (get
k m2)) (intersection
(nonNullKeys
m1) (nonNullKeys
m2)) -Conditional totality
If the given indicator function
f
always evaluates toTrue
- when either or both of its arguments aremempty
:∀ v. (f v
mempty
)&&
(fmempty
v) -Then the following property holds:
disjointBy
f m1 m2==
(∀ k. f (get
k m1) (get
k m2)) -Intersection
intersection :: (Ord k, MonoidNull v, GCDMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v #
Finds the intersection of two maps.
The intersection of maps
m1
andm2
is the greatest single map +Conditional totality
If the given indicator function
f
always evaluates toTrue
+ when either or both of its arguments aremempty
:∀ v. (f v
mempty
)&&
(fmempty
v) +Then the following property holds:
disjointBy
f m1 m2==
(∀ k. f (get
k m1) (get
k m2)) +Intersection
intersection :: (Ord k, MonoidNull v, GCDMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v #
Finds the intersection of two maps.
The intersection of maps
m1
andm2
is the greatest single mapm
that is a submap of bothm1
andm2
:intersection
m1 m2`isSubmapOf`
m1intersection
m1 m2`isSubmapOf`
m2 -The intersection is unique:
and
+The intersection is unique:
and
[intersection
m1 m2`isSubmapOf`
m , m`isSubmapOf`
m1 , m`isSubmapOf`
m2 ] ==> - (m==
intersection
m1 m2) -The following property holds for all possible keys
k
:get
k (intersection
m1 m2)==
gcd
(get
k m1) (get
k m2) + (m==
intersection
m1 m2) +The following property holds for all possible keys
k
:get
k (intersection
m1 m2)==
gcd
(get
k m1) (get
k m2)This function provides the definition of
gcd
for theMonoidMap
- instance ofGCDMonoid
.Examples
With
Product
Natural
values, this function + instance ofGCDMonoid
.Examples
With
Product
Natural
values, this function computes the greatest common divisor of each pair of matching values:>>> m1 =
fromList
[("a", 2), ("b", 6), ("c", 15), ("d", 35)] >>> m2 =fromList
[("a", 6), ("b", 15), ("c", 35), ("d", 77)] >>> m3 =fromList
[("a", 2), ("b", 3), ("c", 5), ("d", 7)] ->>>
intersection
m1 m2==
m3 -True
-With
Sum
Natural
values, this function +>>>
intersection
m1 m2==
m3 +True
+With
Sum
Natural
values, this function computes the minimum of each pair of matching values:>>> m1 =
fromList
[("a", 0), ("b", 1), ("c", 2), ("d", 3)] >>> m2 =fromList
[("a", 3), ("b", 2), ("c", 1), ("d", 0)] >>> m3 =fromList
[("a", 0), ("b", 1), ("c", 1), ("d", 0)] ->>>
intersection
m1 m2==
m3 -True
-With
Set
Natural
values, this function computes the - set intersection of each pair of matching values:f xs =
fromList
(fromList
<$>
xs) +>>>
intersection
m1 m2==
m3 +True
+With
Set
Natural
values, this function computes the + set intersection of each pair of matching values:f xs =
fromList
(fromList
<$>
xs)>>> m1 = f [("a", [0,1,2]), ("b", [0,1,2 ]), ("c", [0,1,2 ])] >>> m2 = f [("a", [0,1,2]), ("b", [ 1,2,3]), ("c", [ 2,3,4])] >>> m3 = f [("a", [0,1,2]), ("b", [ 1,2 ]), ("c", [ 2 ])] -
>>>
intersection
m1 m2==
m3 -True
-:: (Ord k, MonoidNull v3) => (v1 -> v2 -> v3) Function with which to combine values for matching keys.
-> MonoidMap k v1 -> MonoidMap k v2 -> MonoidMap k v3 Computes the intersection of a pair of maps using the given function - to combine values for matching keys.
Satisfies the following property for all possible keys
k
:get
k (intersectionWith
f m1 m2)==
+>>>
intersection
m1 m2==
m3 +True
+:: (Ord k, MonoidNull v3) => (v1 -> v2 -> v3) Function with which to combine values for matching keys.
-> MonoidMap k v1 -> MonoidMap k v2 -> MonoidMap k v3 Computes the intersection of a pair of maps using the given function + to combine values for matching keys.
Satisfies the following property for all possible keys
k
:get
k (intersectionWith
f m1 m2)==
if k`member`
intersection
(nonNullKeys
m1) (nonNullKeys
m2) then f (get
k m1) (get
k m2) - elsemempty
-Conditional totality
If the given combining function
f
always producesmempty
when - either or both of its arguments aremempty
:(f v
mempty
==
mempty
)&&
-(fmempty
v==
mempty
) -Then the following property holds for all possible keys
k
:get
k (intersectionWith
f m1 m2)==
f (get
k m1) (get
k m2) -Examples
With the
min
function applied toSum
-Natural
values:>>> m1 =
fromList
[("a", 4), ("b", 3), ("c", 2), ("d", 1) ] + elsemempty
+Conditional totality
If the given combining function
f
always producesmempty
when + either or both of its arguments aremempty
:(f v
mempty
==
mempty
)&&
+(fmempty
v==
mempty
) +Then the following property holds for all possible keys
k
:get
k (intersectionWith
f m1 m2)==
f (get
k m1) (get
k m2) +Examples
:: (Applicative f, Ord k, MonoidNull v3) => (v1 -> v2 -> f v3) Function with which to combine values for matching keys.
-> MonoidMap k v1 -> MonoidMap k v2 -> f (MonoidMap k v3) An applicative version of
intersectionWith
.Satisfies the following property:
runIdentity
(intersectionWithA
((fmap
.fmap
)Identity
f) m1 m2) -==
(intersectionWith
f m1 m2) -Union
union :: (Ord k, MonoidNull v, LCMMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v #
Finds the union of two maps.
The union of maps
m1
andm2
is the smallest single mapm
+>>>
intersectionWith
min
m1 m2==
m3 +True
+:: (Applicative f, Ord k, MonoidNull v3) => (v1 -> v2 -> f v3) Function with which to combine values for matching keys.
-> MonoidMap k v1 -> MonoidMap k v2 -> f (MonoidMap k v3) An applicative version of
intersectionWith
.Satisfies the following property:
runIdentity
(intersectionWithA
((fmap
.fmap
)Identity
f) m1 m2) +==
(intersectionWith
f m1 m2) +Union
union :: (Ord k, MonoidNull v, LCMMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v #
Finds the union of two maps.
The union of maps
m1
andm2
is the smallest single mapm
that includes bothm1
andm2
as submaps:m1
`isSubmapOf`
union
m1 m2 m2`isSubmapOf`
union
m1 m2 -The union is unique:
and
+The union is unique:
and
[ m1`isSubmapOf`
m , m2`isSubmapOf`
m , m`isSubmapOf`
union
m1 m2 ] ==> - (m==
union
m1 m2) -The following property holds for all possible keys
k
:get
k (union
m1 m2)==
lcm
(get
k m1) (get
k m2) + (m==
union
m1 m2) +The following property holds for all possible keys
k
:get
k (union
m1 m2)==
lcm
(get
k m1) (get
k m2)This function provides the definition of
lcm
for theMonoidMap
- instance ofLCMMonoid
.Examples
With
Product
Natural
values, this function + instance ofLCMMonoid
.Examples
With
Product
Natural
values, this function computes the least common multiple of each pair of matching values:>>> m1 =
fromList
[("a", 2), ("b", 6), ("c", 15), ("d", 35)] >>> m2 =fromList
[("a", 6), ("b", 15), ("c", 35), ("d", 77)] >>> m3 =fromList
[("a", 6), ("b", 30), ("c", 105), ("d", 385)] ->>>
union
m1 m2==
m3 -True
-With
Sum
Natural
values, this function +>>>
union
m1 m2==
m3 +True
+With
Sum
Natural
values, this function computes the maximum of each pair of matching values:>>> m1 =
fromList
[("a", 0), ("b", 1), ("c", 2), ("d", 3)] >>> m2 =fromList
[("a", 3), ("b", 2), ("c", 1), ("d", 0)] >>> m3 =fromList
[("a", 3), ("b", 2), ("c", 2), ("d", 3)] ->>>
union
m1 m2==
m3 -True
-With
Set
Natural
values, this function computes the - set union of each pair of matching values:f xs =
fromList
(fromList
<$>
xs) +>>>
union
m1 m2==
m3 +True
+With
Set
Natural
values, this function computes the + set union of each pair of matching values:f xs =
fromList
(fromList
<$>
xs)>>> m1 = f [("a", [0,1,2]), ("b", [0,1,2 ]), ("c", [0,1,2 ])] >>> m2 = f [("a", [0,1,2]), ("b", [ 1,2,3]), ("c", [ 2,3,4])] >>> m3 = f [("a", [0,1,2]), ("b", [0,1,2,3]), ("c", [0,1,2,3,4])] -
>>>
union
m1 m2==
m3 -True
-:: (Ord k, Monoid v1, Monoid v2, MonoidNull v3) => (v1 -> v2 -> v3) Function with which to combine values for matching keys.
-> MonoidMap k v1 -> MonoidMap k v2 -> MonoidMap k v3 Computes the union of a pair of maps using the given function to combine + values for matching keys.
Satisfies the following property for all possible keys
k
:get
k (unionWith
f m1 m2)==
if k`member`
union
(nonNullKeys
m1) (nonNullKeys
m2) then f (get
k m1) (get
k m2) - elsemempty
-Conditional totality
If the given combining function
f
always producesmempty
when - both of its arguments aremempty
:f
mempty
mempty
==
mempty
-Then the following property holds for all possible keys
k
:get
k (unionWith
f m1 m2)==
f (get
k m1) (get
k m2) -Examples
With the
max
function applied toSum
-Natural
values:>>> m1 =
fromList
[("a", 4), ("b", 3), ("c", 2), ("d", 1) ] + elsemempty
+Conditional totality
If the given combining function
f
always producesmempty
when + both of its arguments aremempty
:f
mempty
mempty
==
mempty
+Then the following property holds for all possible keys
k
:get
k (unionWith
f m1 m2)==
f (get
k m1) (get
k m2) +Examples
:: (Applicative f, Ord k, Monoid v1, Monoid v2, MonoidNull v3) => (v1 -> v2 -> f v3) Function with which to combine values for matching keys.
-> MonoidMap k v1 -> MonoidMap k v2 -> f (MonoidMap k v3) An applicative version of
unionWith
.Satisfies the following property:
runIdentity
(unionWithA
((fmap
.fmap
)Identity
f) m1 m2) -==
(unionWith
f m1 m2) -Prefixes
isPrefixOf :: (Ord k, Monoid v, LeftReductive v) => MonoidMap k v -> MonoidMap k v -> Bool #
:: (Applicative f, Ord k, Monoid v1, Monoid v2, MonoidNull v3) => (v1 -> v2 -> f v3) Function with which to combine values for matching keys.
-> MonoidMap k v1 -> MonoidMap k v2 -> f (MonoidMap k v3) An applicative version of
unionWith
.Satisfies the following property:
runIdentity
(unionWithA
((fmap
.fmap
)Identity
f) m1 m2) +==
(unionWith
f m1 m2) +Prefixes
isPrefixOf :: (Ord k, Monoid v, LeftReductive v) => MonoidMap k v -> MonoidMap k v -> Bool #
Indicates whether or not the first map is a prefix of the second.
MonoidMap
m1
is a prefix ofMonoidMap
m2
if (and only if) for all possible keysk
, the value fork
inm1
is a - prefix of the value fork
inm2
:m1
`isPrefixOf`
m2==
(∀ k.get
k m1`isPrefixOf`
get
k m2) + prefix of the value fork
inm2
:m1
`isPrefixOf`
m2==
(∀ k.get
k m1`isPrefixOf`
get
k m2)This function provides the definition of
isPrefixOf
for theMonoidMap
- instance ofLeftReductive
.Examples
With
String
values:>>> m1 =
fromList
[(1, "a" ), (2, "p" ), (3, "x" )] + instance ofLeftReductive
.Examples
With
String
values:>>> m1 =
fromList
[(1, "a" ), (2, "p" ), (3, "x" )] >>> m2 =fromList
[(1, "abc"), (2, "pqr"), (3, "xyz")] >>> m1`isPrefixOf`
m2 -True
+True
>>> m1 =
fromList
[ (2, "p" ) ] >>> m2 =fromList
[(1, "abc"), (2, "pqr"), (3, "xyz")] >>> m1`isPrefixOf`
m2 -True
+True
>>> m1 =
fromList
[(1, "abc"), (2, "p" ), (3, "x" )] >>> m2 =fromList
[(1, "a" ), (2, "pqr"), (3, "xyz")] >>> m1`isPrefixOf`
m2 -False
->>> m1 =
fromList
[("a", 1), ("b", 1), ("c", 1)] +False
+>>> m1 =
fromList
[("a", 1), ("b", 1), ("c", 1)] >>> m2 =fromList
[("a", 2), ("b", 4), ("c", 8)] >>> m1`isPrefixOf`
m2 -True
+True
>>> m1 =
fromList
[ ("b", 1) ] >>> m2 =fromList
[("a", 2), ("b", 4), ("c", 8)] >>> m1`isPrefixOf`
m2 -True
+True
>>> m1 =
fromList
[("a", 2), ("b", 1), ("c", 1)] >>> m2 =fromList
[("a", 1), ("b", 4), ("c", 8)] >>> m1`isPrefixOf`
m2 -False
-stripPrefix :: (Ord k, MonoidNull v, LeftReductive v) => MonoidMap k v -> MonoidMap k v -> Maybe (MonoidMap k v) #
Strips a prefix from a
MonoidMap
.If map
m1
is a prefix of mapm2
, thenstripPrefix
m1
+False
+stripPrefix :: (Ord k, MonoidNull v, LeftReductive v) => MonoidMap k v -> MonoidMap k v -> Maybe (MonoidMap k v) #
Strips a prefix from a
MonoidMap
.If map
m1
is a prefix of mapm2
, thenstripPrefix
m1
m2
will produce a reduced map where prefixm1
is stripped fromm2
.Properties
The
stripPrefix
function, when applied to mapsm1
andm2
, - produces a result if (and only if)m1
is a prefix ofm2
:isJust
(stripPrefix
m1 m2)==
m1`isPrefixOf`
m2 + produces a result if (and only if)m1
is a prefix ofm2
:isJust
(stripPrefix
m1 m2)==
m1`isPrefixOf`
m2The value for any key
k
in the result is identical to the result of stripping the value fork
in mapm1
from the value fork
- in mapm2
:all
- (\r ->Just
(get
k r)==
stripPrefix
(get
k m1) (get
k m2)) + in mapm2
:all
+ (\r ->Just
(get
k r)==
stripPrefix
(get
k m1) (get
k m2)) (stripPrefix
m1 m2)If we append prefix
m1
to the left-hand side of the result, we can - always recover the original mapm2
:all
- (\r -> m1<>
r==
m2) + always recover the original mapm2
:all
+ (\r -> m1<>
r==
m2) (stripPrefix
m1 m2)This function provides the definition of
stripPrefix
for theMonoidMap
- instance ofLeftReductive
.Examples
With
String
values:>>> m1 =
fromList
[(1, "" ), (2, "i" ), (3, "pq" ), (4, "xyz")] + instance ofLeftReductive
.Examples
With
String
values:>>> m1 =
fromList
[(1, "" ), (2, "i" ), (3, "pq" ), (4, "xyz")] >>> m2 =fromList
[(1, "abc"), (2, "ijk"), (3, "pqr"), (4, "xyz")] >>> m3 =fromList
[(1, "abc"), (2, "jk"), (3, "r"), (4, "")] ->>>
stripPrefix
m1 m2==
Just
m3 -True
->>>
stripPrefix
m2 m1==
Nothing
-True
->>> m1 =
fromList
[("a", 0), ("b", 1), ("c", 2), ("d", 3)] +>>>
stripPrefix
m1 m2==
Just
m3 +True
+>>>
stripPrefix
m2 m1==
Nothing
+True
+>>> m1 =
fromList
[("a", 0), ("b", 1), ("c", 2), ("d", 3)] >>> m2 =fromList
[("a", 3), ("b", 3), ("c", 3), ("d", 3)] >>> m3 =fromList
[("a", 3), ("b", 2), ("c", 1), ("d", 0)] ->>>
stripPrefix
m1 m2==
Just
m3 -True
->>>
stripPrefix
m2 m1==
Nothing
-True
-commonPrefix :: (Ord k, MonoidNull v, LeftGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v #
Finds the greatest common prefix of two maps.
Satisfies the following property for all possible keys
k
:get
k (commonPrefix
m1 m2) -==
commonPrefix
(get
k m1) (get
k m2) +>>>
stripPrefix
m1 m2==
Just
m3 +True
+>>>
stripPrefix
m2 m1==
Nothing
+True
+commonPrefix :: (Ord k, MonoidNull v, LeftGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v #
Finds the greatest common prefix of two maps.
Satisfies the following property for all possible keys
k
:get
k (commonPrefix
m1 m2) +==
commonPrefix
(get
k m1) (get
k m2)This function provides the definition of
commonPrefix
for the -MonoidMap
instance ofLeftGCDMonoid
.Examples
With
String
values:>>> m1 =
fromList
[(1, "+++"), (2, "b++"), (3, "cc+"), (4, "ddd")] +MonoidMap
instance ofLeftGCDMonoid
.Examples
With
String
values:>>> m1 =
fromList
[(1, "+++"), (2, "b++"), (3, "cc+"), (4, "ddd")] >>> m2 =fromList
[(1, "---"), (2, "b--"), (3, "cc-"), (4, "ddd")] >>> m3 =fromList
[(1, "" ), (2, "b" ), (3, "cc" ), (4, "ddd")] ->>>
commonPrefix
m1 m2==
m3 -True
->>> m1 =
fromList
[("a", 0), ("b", 1), ("c", 2), ("d", 3)] +>>>
commonPrefix
m1 m2==
m3 +True
+>>> m1 =
fromList
[("a", 0), ("b", 1), ("c", 2), ("d", 3)] >>> m2 =fromList
[("a", 2), ("b", 2), ("c", 2), ("d", 2)] >>> m3 =fromList
[("a", 0), ("b", 1), ("c", 2), ("d", 2)] ->>>
commonPrefix
m1 m2==
m3 -True
-stripCommonPrefix :: (Ord k, MonoidNull v, LeftGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> (MonoidMap k v, MonoidMap k v, MonoidMap k v) #
Strips the greatest common prefix from a pair of maps.
Given two maps
m1
andm2
,stripCommonPrefix
produces a +>>>
commonPrefix
m1 m2==
m3 +True
+stripCommonPrefix :: (Ord k, MonoidNull v, LeftGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> (MonoidMap k v, MonoidMap k v, MonoidMap k v) #
Strips the greatest common prefix from a pair of maps.
Given two maps
m1
andm2
,stripCommonPrefix
produces a tuple(p, r1, r2)
, where:p
is the greatest common prefix ofm1
andm2
r1
is the remainder of stripping prefixp
fromm1
r2
is the remainder of stripping prefixp
fromm2
The resulting prefix
p
can be appended to the left-hand side of either remainderr1
orr2
to reproduce either of the original mapsm1
orm2
respectively:stripCommonPrefix
m1 m2 -&
\(p, r1, _) -> p<>
r1==
m1 +&
\(p, r1, _) -> p<>
r1==
m1stripCommonPrefix
m1 m2 -&
\(p, _, r2) -> p<>
r2==
m2 +&
\(p, _, r2) -> p<>
r2==
m2Prefix
p
is identical to the result of applyingcommonPrefix
tom1
andm2
:stripCommonPrefix
m1 m2 -&
\(p, _, _) -> p==
commonPrefix
m1 m2 +&
\(p, _, _) -> p==
commonPrefix
m1 m2Remainders
r1
andr2
are identical to the results of applyingstripPrefix
top
andm1
or top
andm2
respectively:stripCommonPrefix
m1 m2 -&
\(p, r1, _) ->Just
r1==
stripPrefix
p m1 +&
\(p, r1, _) ->Just
r1==
stripPrefix
p m1stripCommonPrefix
m1 m2 -&
\(p, _, r2) ->Just
r2==
stripPrefix
p m2 +&
\(p, _, r2) ->Just
r2==
stripPrefix
p m2This function provides the definition of
stripCommonPrefix
for the -MonoidMap
instance ofLeftGCDMonoid
.Examples
With
String
values:>>> m1 =
fromList
[(1, "+++"), (2, "a++"), (3, "aa+"), (4, "aaa")] +MonoidMap
instance ofLeftGCDMonoid
.Examples
With
String
values:>>> m1 =
fromList
[(1, "+++"), (2, "a++"), (3, "aa+"), (4, "aaa")] >>> m2 =fromList
[(1, "---"), (2, "a--"), (3, "aa-"), (4, "aaa")]>>> p =
fromList
[(1, "" ), (2, "a" ), (3, "aa" ), (4, "aaa")] >>> r1 =fromList
[(1, "+++"), (2, "++"), (3, "+"), (4, "")] >>> r2 =fromList
[(1, "---"), (2, "--"), (3, "-"), (4, "")] ->>>
stripCommonPrefix
m1 m2==
(p, r1, r2) -True
->>> m1 =
fromList
[("a", 0), ("b", 1), ("c", 2), ("d", 3), ("e", 4)] +>>>
stripCommonPrefix
m1 m2==
(p, r1, r2) +True
+>>> m1 =
fromList
[("a", 0), ("b", 1), ("c", 2), ("d", 3), ("e", 4)] >>> m2 =fromList
[("a", 4), ("b", 3), ("c", 2), ("d", 1), ("e", 0)]>>> p =
fromList
[("a", 0), ("b", 1), ("c", 2), ("d", 1), ("e", 0)] >>> r1 =fromList
[("a", 0), ("b", 0), ("c", 0), ("d", 2), ("e", 4)] >>> r2 =fromList
[("a", 4), ("b", 2), ("c", 0), ("d", 0), ("e", 0)] ->>>
stripCommonPrefix
m1 m2==
(p, r1, r2) -True
-Suffixes
isSuffixOf :: (Ord k, Monoid v, RightReductive v) => MonoidMap k v -> MonoidMap k v -> Bool #
Indicates whether or not the first map is a suffix of the second.
MonoidMap
m1
is a suffix ofMonoidMap
m2
if (and only if) +>>>
stripCommonPrefix
m1 m2==
(p, r1, r2) +True
+Suffixes
isSuffixOf :: (Ord k, Monoid v, RightReductive v) => MonoidMap k v -> MonoidMap k v -> Bool #
Indicates whether or not the first map is a suffix of the second.
MonoidMap
m1
is a suffix ofMonoidMap
m2
if (and only if) for all possible keysk
, the value fork
inm1
is a - suffix of the value fork
inm2
:m1
`isSuffixOf`
m2==
(∀ k.get
k m1`isSuffixOf`
get
k m2) + suffix of the value fork
inm2
:m1
`isSuffixOf`
m2==
(∀ k.get
k m1`isSuffixOf`
get
k m2)This function provides the definition of
isSuffixOf
for theMonoidMap
- instance ofRightReductive
.Examples
With
String
values:>>> m1 =
fromList
[(1, "c"), (2, "r"), (3, "z")] + instance ofRightReductive
.Examples
With
String
values:>>> m1 =
fromList
[(1, "c"), (2, "r"), (3, "z")] >>> m2 =fromList
[(1, "abc"), (2, "pqr"), (3, "xyz")] >>> m1`isSuffixOf`
m2 -True
+True
>>> m1 =
fromList
[ (2, "r") ] >>> m2 =fromList
[(1, "abc"), (2, "pqr"), (3, "xyz")] >>> m1`isSuffixOf`
m2 -True
+True
>>> m1 =
fromList
[(1, "abc"), (2, "r"), (3, "z")] >>> m2 =fromList
[(1, "c"), (2, "pqr"), (3, "xyz")] >>> m1`isSuffixOf`
m2 -False
->>> m1 =
fromList
[("a", 1), ("b", 1), ("c", 1)] +False
+>>> m1 =
fromList
[("a", 1), ("b", 1), ("c", 1)] >>> m2 =fromList
[("a", 2), ("b", 4), ("c", 8)] >>> m1`isSuffixOf`
m2 -True
+True
>>> m1 =
fromList
[ ("b", 1) ] >>> m2 =fromList
[("a", 2), ("b", 4), ("c", 8)] >>> m1`isSuffixOf`
m2 -True
+True
>>> m1 =
fromList
[("a", 2), ("b", 1), ("c", 1)] >>> m2 =fromList
[("a", 1), ("b", 4), ("c", 8)] >>> m1`isSuffixOf`
m2 -False
-stripSuffix :: (Ord k, MonoidNull v, RightReductive v) => MonoidMap k v -> MonoidMap k v -> Maybe (MonoidMap k v) #
Strips a suffix from a
MonoidMap
.If map
m1
is a suffix of mapm2
, thenstripSuffix
m1
+False
+stripSuffix :: (Ord k, MonoidNull v, RightReductive v) => MonoidMap k v -> MonoidMap k v -> Maybe (MonoidMap k v) #
Strips a suffix from a
MonoidMap
.If map
m1
is a suffix of mapm2
, thenstripSuffix
m1
m2
will produce a reduced map where suffixm1
is stripped fromm2
.Properties
The
stripSuffix
function, when applied to mapsm1
andm2
, - produces a result if (and only if)m1
is a suffix ofm2
:isJust
(stripSuffix
m1 m2)==
m1`isSuffixOf`
m2 + produces a result if (and only if)m1
is a suffix ofm2
:isJust
(stripSuffix
m1 m2)==
m1`isSuffixOf`
m2The value for any key
k
in the result is identical to the result of stripping the value fork
in mapm1
from the value fork
- in mapm2
:all
- (\r ->Just
(get
k r)==
stripSuffix
(get
k m1) (get
k m2)) + in mapm2
:all
+ (\r ->Just
(get
k r)==
stripSuffix
(get
k m1) (get
k m2)) (stripSuffix
m1 m2)If we append suffix
m1
to the right-hand side of the result, we can - always recover the original mapm2
:all
- (\r -> r<>
m1==
m2) + always recover the original mapm2
:all
+ (\r -> r<>
m1==
m2) (stripSuffix
m1 m2)This function provides the definition of
stripSuffix
for theMonoidMap
- instance ofRightReductive
.Examples
With
String
values:>>> m1 =
fromList
[(1, ""), (2, "k"), (3, "qr"), (4, "xyz")] + instance ofRightReductive
.Examples
With
String
values:>>> m1 =
fromList
[(1, ""), (2, "k"), (3, "qr"), (4, "xyz")] >>> m2 =fromList
[(1, "abc"), (2, "ijk"), (3, "pqr"), (4, "xyz")] >>> m3 =fromList
[(1, "abc"), (2, "ij" ), (3, "p" ), (4, "" )] ->>>
stripSuffix
m1 m2==
Just
m3 -True
->>>
stripSuffix
m2 m1==
Nothing
-True
->>> m1 =
fromList
[("a", 0), ("b", 1), ("c", 2), ("d", 3)] +>>>
stripSuffix
m1 m2==
Just
m3 +True
+>>>
stripSuffix
m2 m1==
Nothing
+True
+>>> m1 =
fromList
[("a", 0), ("b", 1), ("c", 2), ("d", 3)] >>> m2 =fromList
[("a", 3), ("b", 3), ("c", 3), ("d", 3)] >>> m3 =fromList
[("a", 3), ("b", 2), ("c", 1), ("d", 0)] ->>>
stripSuffix
m1 m2==
Just
m3 -True
->>>
stripSuffix
m2 m1==
Nothing
-True
-commonSuffix :: (Ord k, MonoidNull v, RightGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v #
Finds the greatest common suffix of two maps.
Satisfies the following property for all possible keys
k
:get
k (commonSuffix
m1 m2) -==
commonSuffix
(get
k m1) (get
k m2) +>>>
stripSuffix
m1 m2==
Just
m3 +True
+>>>
stripSuffix
m2 m1==
Nothing
+True
+commonSuffix :: (Ord k, MonoidNull v, RightGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v #
Finds the greatest common suffix of two maps.
Satisfies the following property for all possible keys
k
:get
k (commonSuffix
m1 m2) +==
commonSuffix
(get
k m1) (get
k m2)This function provides the definition of
commonSuffix
for the -MonoidMap
instance ofRightGCDMonoid
.Examples
With
String
values:>>> m1 =
fromList
[(1, "+++"), (2, "++b"), (3, "+cc"), (4, "ddd")] +MonoidMap
instance ofRightGCDMonoid
.Examples
With
String
values:>>> m1 =
fromList
[(1, "+++"), (2, "++b"), (3, "+cc"), (4, "ddd")] >>> m2 =fromList
[(1, "---"), (2, "--b"), (3, "-cc"), (4, "ddd")] >>> m3 =fromList
[(1, ""), (2, "b"), (3, "cc"), (4, "ddd")] ->>>
commonSuffix
m1 m2==
m3 -True
->>> m1 =
fromList
[("a", 0), ("b", 1), ("c", 2), ("d", 3)] +>>>
commonSuffix
m1 m2==
m3 +True
+>>> m1 =
fromList
[("a", 0), ("b", 1), ("c", 2), ("d", 3)] >>> m2 =fromList
[("a", 2), ("b", 2), ("c", 2), ("d", 2)] >>> m3 =fromList
[("a", 0), ("b", 1), ("c", 2), ("d", 2)] ->>>
commonSuffix
m1 m2==
m3 -True
-stripCommonSuffix :: (Ord k, MonoidNull v, RightGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> (MonoidMap k v, MonoidMap k v, MonoidMap k v) #
Strips the greatest common suffix from a pair of maps.
Given two maps
m1
andm2
,stripCommonSuffix
produces a +>>>
commonSuffix
m1 m2==
m3 +True
+stripCommonSuffix :: (Ord k, MonoidNull v, RightGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> (MonoidMap k v, MonoidMap k v, MonoidMap k v) #
Strips the greatest common suffix from a pair of maps.
Given two maps
m1
andm2
,stripCommonSuffix
produces a tuple(r1, r2, s)
, where:s
is the greatest common suffix ofm1
andm2
r1
is the remainder of stripping suffixs
fromm1
r2
is the remainder of stripping suffixs
fromm2
The resulting suffix
s
can be appended to the right-hand side of either remainderr1
orr2
to reproduce either of the original mapsm1
orm2
respectively:stripCommonSuffix
m1 m2 -&
\(r1, _, s) -> r1<>
s==
m1 +&
\(r1, _, s) -> r1<>
s==
m1stripCommonSuffix
m1 m2 -&
\(_, r2, s) -> r2<>
s==
m2 +&
\(_, r2, s) -> r2<>
s==
m2Suffix
s
is identical to the result of applyingcommonSuffix
tom1
andm2
:stripCommonSuffix
m1 m2 -&
\(_, _, s) -> s==
commonSuffix
m1 m2 +&
\(_, _, s) -> s==
commonSuffix
m1 m2Remainders
r1
andr2
are identical to the results of applyingstripSuffix
tos
andm1
or tos
andm2
respectively:stripCommonSuffix
m1 m2 -&
\(r1, _, s) ->Just
r1==
stripSuffix
s m1 +&
\(r1, _, s) ->Just
r1==
stripSuffix
s m1stripCommonSuffix
m1 m2 -&
\(_, r2, s) ->Just
r2==
stripSuffix
s m2 +&
\(_, r2, s) ->Just
r2==
stripSuffix
s m2This function provides the definition of
stripCommonSuffix
for the -MonoidMap
instance ofRightGCDMonoid
.Examples
With
String
values:>>> m1 =
fromList
[(1, "+++"), (2, "++a"), (3, "+aa"), (4, "aaa")] +MonoidMap
instance ofRightGCDMonoid
.Examples
With
String
values:>>> m1 =
fromList
[(1, "+++"), (2, "++a"), (3, "+aa"), (4, "aaa")] >>> m2 =fromList
[(1, "---"), (2, "--a"), (3, "-aa"), (4, "aaa")]>>> r1 =
fromList
[(1, "+++"), (2, "++" ), (3, "+" ), (4, "" )] >>> r2 =fromList
[(1, "---"), (2, "--" ), (3, "-" ), (4, "" )] >>> s =fromList
[(1, ""), (2, "a"), (3, "aa"), (4, "aaa")] ->>>
stripCommonSuffix
m1 m2==
(r1, r2, s) -True
->>> m1 =
fromList
[("a", 0), ("b", 1), ("c", 2), ("d", 3), ("e", 4)] +>>>
stripCommonSuffix
m1 m2==
(r1, r2, s) +True
+>>> m1 =
fromList
[("a", 0), ("b", 1), ("c", 2), ("d", 3), ("e", 4)] >>> m2 =fromList
[("a", 4), ("b", 3), ("c", 2), ("d", 1), ("e", 0)]>>> r1 =
fromList
[("a", 0), ("b", 0), ("c", 0), ("d", 2), ("e", 4)] >>> r2 =fromList
[("a", 4), ("b", 2), ("c", 0), ("d", 0), ("e", 0)] >>> s =fromList
[("a", 0), ("b", 1), ("c", 2), ("d", 1), ("e", 0)] ->>>
stripCommonSuffix
m1 m2==
(r1, r2, s) -True
-Overlap
overlap :: (Ord k, MonoidNull v, OverlappingGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v #
Overlap
overlap :: (Ord k, MonoidNull v, OverlappingGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v #
Finds the greatest overlap of two maps.
The greatest overlap
o
of mapsm1
andm2
is the unique + greatest map that is both a suffix ofm1
and a prefix ofm2
:m1
==
r1<>
o +m2==
o<>
r2Where:
r1
is the remainder obtained by stripping suffix overlapo
fromm1
.(see
stripSuffixOverlap
)r2
is the remainder obtained by stripping prefix overlap -o
fromm2
.(see
stripPrefixOverlap
)
This function satisfies the following property:
get
k (overlap
m1 m2)==
overlap
(get
k m1) (get
k m2) +o
fromm2
.(see
stripPrefixOverlap
)This function satisfies the following property:
get
k (overlap
m1 m2)==
overlap
(get
k m1) (get
k m2)This function provides the definition of
overlap
for theMonoidMap
- instance ofOverlappingGCDMonoid
.Examples
With
String
values:>>> m1 =
fromList
[(1,"abc" ), (2,"abcd" ), (3,"abcde "), (4,"abcdef")] + instance ofOverlappingGCDMonoid
.Examples
With
String
values:>>> m1 =
fromList
[(1,"abc" ), (2,"abcd" ), (3,"abcde "), (4,"abcdef")] >>> m2 =fromList
[(1, "def"), (2, "cdef"), (3," bcdef"), (4,"abcdef")] >>> m3 =fromList
[(1, "" ), (2, "cd" ), (3," bcde" ), (4,"abcdef")] ->>>
overlap
m1 m2==
m3 -True
->>> m1 =
fromList
[("a", 0), ("b", 1), ("c", 2), ("d", 3), ("e", 4)] +>>>
overlap
m1 m2==
m3 +True
+>>> m1 =
fromList
[("a", 0), ("b", 1), ("c", 2), ("d", 3), ("e", 4)] >>> m2 =fromList
[("a", 4), ("b", 3), ("c", 2), ("d", 1), ("e", 0)] >>> m3 =fromList
[("a", 0), ("b", 1), ("c", 2), ("d", 1), ("e", 0)] ->>>
overlap
m1 m2==
m3 -True
-stripPrefixOverlap :: (Ord k, MonoidNull v, OverlappingGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v #
stripPrefixOverlap :: (Ord k, MonoidNull v, OverlappingGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v #
Strips from the second map its greatest prefix overlap with suffixes of the first map.
Evaluating
stripPrefixOverlap
m1
m2
produces the remainder -r2
:m1
==
r1<>
o -m2==
o<>
r2 +r2
:m1
==
r1<>
o +m2==
o<>
r2Where
o
is the greatest overlap of mapsm1
andm2
: the unique greatest map that is both a suffix ofm1
and a prefix ofm2
.This function satisfies the following property:
get
k (stripPrefixOverlap
m1 m2) -==
stripPrefixOverlap
(get
k m1) (get
k m2) +==
stripPrefixOverlap
(get
k m1) (get
k m2)This function provides the definition of
stripPrefixOverlap
for the -MonoidMap
instance ofOverlappingGCDMonoid
.Examples
With
String
values:>>> m1 =
fromList
[(1,"abc" ), (2,"abcd" ), (3,"abcde" ), (4,"abcdef")] +MonoidMap
instance ofOverlappingGCDMonoid
.Examples
With
String
values:>>> m1 =
fromList
[(1,"abc" ), (2,"abcd" ), (3,"abcde" ), (4,"abcdef")] >>> m2 =fromList
[(1, "def"), (2, "cdef"), (3, "bcdef"), (4,"abcdef")] >>> m3 =fromList
[(1, "def"), (2, "ef"), (3, "f"), (4, "")] ->>>
stripPrefixOverlap
m1 m2==
m3 -True
->>> m1 =
fromList
[("a", 0), ("b", 1), ("c", 2), ("d", 3), ("e", 4)] +>>>
stripPrefixOverlap
m1 m2==
m3 +True
+>>> m1 =
fromList
[("a", 0), ("b", 1), ("c", 2), ("d", 3), ("e", 4)] >>> m2 =fromList
[("a", 4), ("b", 3), ("c", 2), ("d", 1), ("e", 0)] >>> m3 =fromList
[("a", 4), ("b", 2), ("c", 0), ("d", 0), ("e", 0)] ->>>
stripPrefixOverlap
m1 m2==
m3 -True
-stripSuffixOverlap :: (Ord k, MonoidNull v, OverlappingGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v #
Strips from the second map its greatest suffix overlap with prefixes +
>>>
stripPrefixOverlap
m1 m2==
m3 +True
+stripSuffixOverlap :: (Ord k, MonoidNull v, OverlappingGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v #
Strips from the second map its greatest suffix overlap with prefixes of the first map.
Evaluating
stripSuffixOverlap
m2
m1
produces the remainder -r1
:m1
==
r1<>
o -m2==
o<>
r2 +r1
:m1
==
r1<>
o +m2==
o<>
r2Where
o
is the greatest overlap of mapsm1
andm2
: the unique greatest map that is both a suffix ofm1
and a prefix ofm2
.This function satisfies the following property:
get
k (stripSuffixOverlap
m2 m1) -==
stripSuffixOverlap
(get
k m2) (get
k m1) +==
stripSuffixOverlap
(get
k m2) (get
k m1)This function provides the definition of
stripSuffixOverlap
for the -MonoidMap
instance ofOverlappingGCDMonoid
.Examples
With
String
values:>>> m1 =
fromList
[(1,"abc" ), (2,"abcd" ), (3,"abcde" ), (4,"abcdef")] +MonoidMap
instance ofOverlappingGCDMonoid
.Examples
With
String
values:>>> m1 =
fromList
[(1,"abc" ), (2,"abcd" ), (3,"abcde" ), (4,"abcdef")] >>> m2 =fromList
[(1, "def"), (2, "cdef"), (3, "bcdef"), (4,"abcdef")] >>> m3 =fromList
[(1,"abc" ), (2,"ab" ), (3,"a" ), (4,"" )] ->>>
stripSuffixOverlap
m2 m1==
m3 -True
->>> m1 =
fromList
[("a", 0), ("b", 1), ("c", 2), ("d", 3), ("e", 4)] +>>>
stripSuffixOverlap
m2 m1==
m3 +True
+>>> m1 =
fromList
[("a", 0), ("b", 1), ("c", 2), ("d", 3), ("e", 4)] >>> m2 =fromList
[("a", 4), ("b", 3), ("c", 2), ("d", 1), ("e", 0)] >>> m3 =fromList
[("a", 0), ("b", 0), ("c", 0), ("d", 2), ("e", 4)] ->>>
stripSuffixOverlap
m2 m1==
m3 -True
-stripOverlap :: (Ord k, MonoidNull v, OverlappingGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> (MonoidMap k v, MonoidMap k v, MonoidMap k v) #
Finds the greatest overlap of two maps and strips it from both maps.
Evaluating
stripOverlap
m1
m2
produces the tuple -(r1, o, r2)
, where:m1
==
r1<>
o -m2==
o<>
r2 +>>>
stripSuffixOverlap
m2 m1==
m3 +True
+stripOverlap :: (Ord k, MonoidNull v, OverlappingGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> (MonoidMap k v, MonoidMap k v, MonoidMap k v) #
Finds the greatest overlap of two maps and strips it from both maps.
Evaluating
stripOverlap
m1
m2
produces the tuple +(r1, o, r2)
, where:m1
==
r1<>
o +m2==
o<>
r2Where:
o
is the greatest overlap of mapsm1
andm2
: the unique greatest map that is both a suffix ofm1
and a prefix ofm2
.(see
overlap
)r1
is the remainder obtained by stripping suffix overlapo
fromm1
.(see
stripSuffixOverlap
)r2
is the remainder obtained by stripping prefix overlap -o
fromm2
.(see
stripPrefixOverlap
)
This function satisfies the following property:
stripOverlap
m1 m2==
+o
fromm2
.(see
stripPrefixOverlap
)This function satisfies the following property:
stripOverlap
m1 m2==
(stripSuffixOverlap
m2 m1 ,overlap
m1 m2 ,stripPrefixOverlap
m1 m2 )This function provides the definition of
stripOverlap
for the -MonoidMap
instance ofOverlappingGCDMonoid
.MonoidMap
instance ofOverlappingGCDMonoid
.
monoidmap-0.0.1.3: Monoidal map type
Monoidal map type with support for semigroup and monoid subclasses.
- Data
monoidmap-0.0.1.4: Monoidal map type
Monoidal map type with support for semigroup and monoid subclasses.
- Data