Safe Haskell | None |
---|---|

Language | Haskell2010 |

Functions on association lists that involve `Predicate`

s on the keys.

## Synopsis

- lookupFirst :: forall l a b. AssocList l a b => Predicate a -> l -> Maybe b
- lookupAll :: forall l a b. AssocList l a b => Predicate a -> l -> [b]
- removeFirst :: forall l a b. AssocList l a b => Predicate a -> l -> l
- removeAll :: forall l a b. AssocList l a b => Predicate a -> l -> l
- mapFirst :: forall l a b. AssocList l a b => Predicate a -> (b -> b) -> l -> l
- mapAll :: forall l a b. AssocList l a b => Predicate a -> (b -> b) -> l -> l
- partition :: forall l a b. AssocList l a b => Predicate a -> l -> ([b], l)
- break :: forall l a b. AssocList l a b => Predicate a -> l -> (l, l)
- breakPartition :: forall l a b. AssocList l a b => Predicate a -> l -> (l, [b], l)

# Related modules

Some other modules that are a lot like this one:

- Data.AssocList.ListLike.Eq - Functions on association lists that
make use of an
`Eq`

constraint on the type of the keys - Data.AssocList.ListLike.Equivalence - Functions on association
lists that involve
`Equivalence`

s on the keys

# Lookup

lookupFirst :: forall l a b. AssocList l a b => Predicate a -> l -> Maybe b Source #

Obtain the first value associated with a key that satisfies a predicate, if such a mapping is present.

`>>>`

Just 2`lookupFirst (Predicate (== 'B')) [('A',1), ('B',2), ('B',3), ('C',4)]`

The result is `Nothing`

if no key in the list satisfies the predicate.

`>>>`

Nothing`lookupFirst (Predicate (== 'D')) [('A',1), ('B',2), ('B',3), ('C',4)]`

lookupAll :: forall l a b. AssocList l a b => Predicate a -> l -> [b] Source #

Obtain all values associated with keys that satisfy the predicate, in the order in which the mappings appear in the list.

`>>>`

[2,3,3]`lookupAll (Predicate (== 'B')) [('A',1), ('B',2), ('B',3), ('C',4), ('B',3)]`

# Removal

removeFirst :: forall l a b. AssocList l a b => Predicate a -> l -> l Source #

Produce a modified version of the association list in which the first occurrence of a key that satisfied the predicate has been removed.

`>>>`

[('A',1),('B',3),('C',4)]`removeFirst (Predicate (== 'B')) [('A',1), ('B',2), ('B',3), ('C',4)]`

If no key in the list satisfies the predicate, then the original list is returned.

`>>>`

[('A',1),('B',2),('B',3)]`removeFirst (Predicate (== 'C')) [('A',1), ('B',2), ('B',3)]`

removeAll :: forall l a b. AssocList l a b => Predicate a -> l -> l Source #

Produce a modified version of the association list in which all occurrences of keys that satisfy the predicate have been removed.

`>>>`

[('A',1),('C',4)]`removeAll (Predicate (== 'B')) [('A',1), ('B',2), ('B',3), ('C',4)]`

If the key is not present in the mapping, then the original list is returned.

`>>>`

[('A',1),('B',2),('B',3)]`removeAll (Predicate (== 'C')) [('A',1), ('B',2), ('B',3)]`

# Mapping

The "map" functions modify values while preserving the structure of the assocative list. The resulting list has the same size and order as the original.

mapFirst :: forall l a b. AssocList l a b => Predicate a -> (b -> b) -> l -> l Source #

At the position where a key satisfying the predicate first appears in the list, apply a function to the corresponding value.

`>>>`

[('A',1),('B',-4),('C',2),('B',6)]`mapFirst (Predicate (== 'B')) negate [('A', 1), ('B', 4), ('C', 2), ('B', 6)]`

If no key in the list satisfies the predicate, then the original list is returned without modification.

`>>>`

[('A',1),('B',4),('C',2),('B',6)]`mapFirst (Predicate (== 'D')) negate [('A', 1), ('B', 4), ('C', 2), ('B', 6)]`

mapAll :: forall l a b. AssocList l a b => Predicate a -> (b -> b) -> l -> l Source #

At each position in the list where the key satisfies the predicate, apply a function to the corresponding value.

`>>>`

[('A',1),('B',-4),('C',2),('B',-6)]`mapAll (Predicate (== 'B')) negate [('A', 1), ('B', 4), ('C', 2), ('B', 6)]`

If no key in the list satisfies the predicate, then the original list is returned without modification.

`>>>`

[('A',1),('B',4),('C',2),('B',6)]`mapAll (Predicate (== 'D')) negate [('A', 1), ('B', 4), ('C', 2), ('B', 6)]`

# Grouping

break :: forall l a b. AssocList l a b => Predicate a -> l -> (l, l) Source #

Produces a tuple of two results:

- The longest prefix of the association list that does
*not*contain a key satisfying the predict - The remainder of the list

`>>>`

([('A',1)],[('B',2),('B',3),('C',4)])`break (Predicate (== 'B')) [('A',1), ('B',2), ('B',3), ('C',4)]`

If the key of the first mapping in the list satisfies the predicate, then the first part of the resulting tuple is empty, and the second part of the result is the entire list.

`>>>`

([],[('A',1),('B',2),('B',3),('C',4)])`break (Predicate (== 'A')) [('A',1), ('B',2), ('B',3), ('C',4)]`

If no key in the list satisfies the predicate, then the first part of the resulting tuple is the entire list, and the second part of the result is empty.

`>>>`

([('A',1),('B',2),('B',3),('C',4)],[])`break (Predicate (== 'D')) [('A',1), ('B',2), ('B',3), ('C',4)]`

breakPartition :: forall l a b. AssocList l a b => Predicate a -> l -> (l, [b], l) Source #

`break`

on a predicate, then `partition`

the remainder.

separates `breakPartition`

p l`l`

into three parts:

- The key-value pairs for which the predicate is
*not*satisfied that occur in the list*before*the first occurrence of a key that satisfies the predicate (`fst (`

)`break`

p l) - All values associated with keys that satisfy the predicate
(

)`lookupAll`

p l - The key-value pairs for which the predicate is
*not*satisfied that occur in the list*after*the first occurrence of a key that satisfies the predicate (

)`removeAll`

p (snd (`break`

p l))

`>>>`

([('A',1)],[2,4],[('C',3)])`breakPartition (Predicate (== 'B')) [('A',1),('B',2),('C',3),('B',4)]`

If the predicate is not satisfied by any key in the list, then the first part of the result is the entire list, and the other parts are empty.

`>>>`

([('A',1),('B',2),('C',3),('B',4)],[],[])`breakPartition (Predicate (== 'D')) [('A',1),('B',2),('C',3),('B',4)]`