liqwid-plutarch-extra-3.21.1: A collection of Plutarch extras from Liqwid Labs
Copyright(C) Liqwid Labs 2022
LicenseApache 2.0
MaintainerKoz Ross <[email protected]>
StabilityExperimental
PortabilityGHC only
Safe HaskellSafe-Inferred
LanguageHaskell2010

Plutarch.Extra.Map

Description

Various helpers for PMap use. This module is intended to be imported qualified.

Synopsis

Lookup

ptryLookup :: forall (k :: S -> Type) (v :: S -> Type) (keys :: KeyGuarantees) (s :: S). (PIsData k, PIsData v) => Term s (k :--> (PMap keys k v :--> v)) Source #

As plookup, but errors when the key is missing.

Since: 3.4.0

plookupGe :: forall (k :: S -> Type) (v :: S -> Type) (s :: S). (PIsData k, POrd k) => Term s (k :--> (PMap 'Sorted k v :--> PMaybe (PPair (PAsData v) (PMap 'Sorted k v)))) Source #

As plookup, but also yields the portion of the PMap whose keys are greater than the target if the search is successful.

Since: 3.9.0

Comparisons

pkeysEqual :: forall (k :: S -> Type) (a :: S -> Type) (b :: S -> Type) (s :: S). (PIsData k, PEq k) => Term s (PMap 'Sorted k a :--> (PMap 'Sorted k b :--> PBool)) Source #

Gives PTrue if both argument PMaps contain mappings for exactly the same set of keys. Requires a number of equality comparisons between keys proportional to the length of the shorter argument.

Since: 1.1.0

pkeysEqualUnsorted :: forall (k :: S -> Type) (a :: S -> Type) (b :: S -> Type) (s :: S). (PIsData k, PIsData a, PIsData b) => Term s (PMap 'Unsorted k a :--> (PMap 'Unsorted k b :--> PBool)) Source #

As pkeysEqual, but requires only PEq constraints for the keys, and works for Unsorted PMaps. This requires a number of equality comparisons between keys proportional to the product of the lengths of both arguments: that is, this function is quadratic.

Since: 3.4.0

Modification

pupdate :: forall (k :: S -> Type) (v :: S -> Type) (s :: S). (PIsData k, PIsData v, POrd k) => Term s ((v :--> PMaybe v) :--> (k :--> (PMap 'Sorted k v :--> PMap 'Sorted k v))) Source #

Given an 'updater' and a key, if the key exists in the PMap, apply the 'updater' to it, otherwise do nothing. If the 'updater' produces PNothing, the value is deleted; otherwise, it is modified to the result.

Performance will be equivalent to a lookup followed by an insert (or delete), as well as the cost of calling the 'updater'.

Since: 3.4.0

padjust :: forall (k :: S -> Type) (v :: S -> Type) (s :: S). (PIsData k, PEq k, PIsData v) => Term s ((v :--> v) :--> (k :--> (PMap 'Unsorted k v :--> PMap 'Unsorted k v))) Source #

If a value exists at the specified key, apply the function argument to it; otherwise, do nothing.

This is necessarily linear in the size of the map performance-wise, as we have to scan the entire map to find the key in the worst case.

Since: 3.4.0

Folds

pfoldMapWithKey :: forall (m :: S -> Type) (k :: S -> Type) (v :: S -> Type) (s :: S). (PIsData k, PIsData v, forall (s' :: S). Monoid (Term s' m)) => Term s ((k :--> (v :--> m)) :--> (PMap 'Sorted k v :--> m)) Source #

Project all key-value pairs into a Monoid, then combine. Keys and values will be presented in key order.

Since: 3.4.0

pfoldlWithKey :: forall (a :: S -> Type) (k :: S -> Type) (v :: S -> Type) (s :: S). (PIsData k, PIsData v) => Term s ((a :--> (k :--> (v :--> a))) :--> (a :--> (PMap 'Sorted k v :--> a))) Source #

Left-associative fold of a PMap with keys. Keys and values will be presented in key order.

Since: 3.4.0

Elimination

phandleMin :: forall (r :: S -> Type) (k :: S -> Type) (v :: S -> Type) (s :: S). (PIsData k, PIsData v) => Term s (PMap 'Sorted k v) -> Term s r -> (Term s k -> Term s v -> Term s (PMap 'Sorted k v) -> Term s r) -> Term s r Source #

Eliminates a sorted PMap, similarly to pelimList. The function argument, if used, will be given the smallest key in the PMap, with its corresponding value, as well as the 'rest' of the PMap.

Since: 3.9.0

Conversion

punsortedMapFromFoldable :: forall (k :: S -> Type) (v :: S -> Type) (f :: Type -> Type) (s :: S). (Foldable f, PIsData k, PIsData v) => f (Term s k, Term s v) -> Term s (PMap 'Unsorted k v) Source #

Given a Foldable of key-value pairs, construct an unsorted PMap. Performs linearly with respect to its argument.

Note

If there are duplicate keys in the input, the last key will 'win' in a lookup.

Since: 3.4.0

psortedMapFromFoldable :: forall (k :: S -> Type) (v :: S -> Type) (f :: Type -> Type) (s :: S). (Foldable f, POrd k, PIsData k, PIsData v) => f (Term s k, Term s v) -> Term s (PMap 'Sorted k v) Source #

Given a Foldable of (not necessarily sorted) key-value pairs, construct a PMap which is guaranteed sorted. Performs a linear number of ordered insertions with respect to the length of its argument.

Note

If there are duplicate keys, only the last key-value pair will remain in the result.

Since: 3.4.0

pkeys :: forall (ell :: (S -> Type) -> S -> Type) (k :: S -> Type) (v :: S -> Type) (keys :: KeyGuarantees) (s :: S). (PListLike ell, PElemConstraint ell (PAsData k)) => Term s (PMap keys k v :--> ell (PAsData k)) Source #

Get a list-like structure full of the keys of the argument PMap. If the PMap is Sorted, the keys will maintain that order, and will be unique; otherwise, the order is unspecified, and duplicates may exist.

Note

You will need to specify what manner of list-like structure you want; we have arranged the type signature to make specifying this easy with TypeApplications.

Since: 3.4.0

Key-value pair manipulation

pkvPairKey :: forall (k :: S -> Type) (v :: S -> Type) (s :: S). PIsData k => Term s (PBuiltinPair (PAsData k) (PAsData v) :--> k) Source #

Get the key of a key-value pair.

Since: 1.1.0

pkvPairValue :: forall (k :: S -> Type) (v :: S -> Type) (s :: S). PIsData v => Term s (PBuiltinPair (PAsData k) (PAsData v) :--> v) Source #

Get the value of a key-value pair.

Since: 3.4.0

pkvPairLt :: forall (k :: S -> Type) (v :: S -> Type) (s :: S). (PIsData k, PPartialOrd k) => Term s (PBuiltinPair (PAsData k) (PAsData v) :--> (PBuiltinPair (PAsData k) (PAsData v) :--> PBool)) Source #

Compare two key-value pairs by their keys. Gives PTrue if the key of the first argument pair is less than the key of the second argument pair.

Since: 3.4.0