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.List

Description

Various helpers for list-like structures. This module is intended to be imported qualified.

Synopsis

Construction

preplicate :: forall (ell :: (S -> Type) -> S -> Type) (a :: S -> Type) (s :: S). (PElemConstraint ell a, PListLike ell) => Term s (PInteger :--> (a :--> ell a)) Source #

Given a count n and a value x, produces a list-like structure containing n copies of x, or an empty structure if n is non-positive.

Note

You will likely need to specify which list-like structure you want; the type arguments for this function are optimized for use with TypeApplications to do exactly this task.

Since: 3.6.0

pfromList :: forall (list :: (S -> Type) -> S -> Type) (a :: S -> Type) (s :: S). PIsListLike list a => [Term s a] -> Term s (list a) Source #

Turn a Haskell-level list of Terms into a PListLike @since 3.9.0

Transformation

pmapMaybe :: forall (listO :: (S -> Type) -> S -> Type) (b :: S -> Type) (listI :: (S -> Type) -> S -> Type) (a :: S -> Type) (s :: S). (PIsListLike listI a, PIsListLike listO b) => Term s ((a :--> PMaybe b) :--> (listI a :--> listO b)) Source #

Similar to pmap, but allows elements to be thrown out. More precisely, for elements where the function argument returns PNothing, the corresponding element is removed, while for elements where the function argument returns PJust, the value is used in the result.

Since: 3.14.1

pdeleteFirstBy :: forall (a :: S -> Type) (list :: (S -> Type) -> S -> Type) (s :: S). PIsListLike list a => Term s ((a :--> PBool) :--> (list a :--> PMaybe (list a))) Source #

O(n) . Remove the first occurance of a value that satisfies the predicate from the given list. Return nothing if said value is not in the list.

Since: 3.14.1

ptryDeleteFirstBy :: forall (a :: S -> Type) (list :: (S -> Type) -> S -> Type) (s :: S). PIsListLike list a => Term s ((a :--> PBool) :--> (list a :--> list a)) Source #

Partial version of pdeleteBy.

Since: 3.14.1

pdeleteFirst :: forall (a :: S -> Type) (list :: (S -> Type) -> S -> Type) (s :: S). (PEq a, PIsListLike list a) => Term s (a :--> (list a :--> PMaybe (list a))) Source #

Special version of pdeleteBy, for types with PEq instance.

Since: 3.14.1

Search

pfindJust :: forall (b :: S -> Type) (ell :: (S -> Type) -> S -> Type) (a :: S -> Type) (s :: S). (PElemConstraint ell a, PListLike ell) => Term s ((a :--> PMaybe b) :--> (ell a :--> PMaybe b)) Source #

A combination of pmap and pfind, but without needing an intermediate structure. More precisely, searched for the first element in a list-like structure that produces a PJust argument, returning it if found; otherwise, produces PNothing.

Since: 3.6.0

plookupAssoc :: forall (k :: S -> Type) (v :: S -> Type) (kv :: S -> Type) (ell :: (S -> Type) -> S -> Type) (s :: S). (PElemConstraint ell kv, PListLike ell, PEq k) => Term s ((kv :--> k) :--> ((kv :--> v) :--> (k :--> (ell kv :--> PMaybe v)))) Source #

Treats a list-like structure as an assoc list. More precisely, given a list-like structure of key-value pairs, a method of extracting the key and the value, and a 'target' key, returns the corresponding value, or PNothing if there isn't one.

Note

There may be multiple mappings for a specific key; in such a situation, only the first match is returned. In general, this requires time proportional to the length of the list-like structure, as we may have to check every entry.

Since: 3.6.0

Elimination

phandleList :: forall (a :: S -> Type) (r :: S -> Type) (ell :: (S -> Type) -> S -> Type) (s :: S). (PElemConstraint ell a, PListLike ell) => Term s (ell a) -> Term s r -> (Term s a -> Term s (ell a) -> Term s r) -> Term s r Source #

pelimList with re-ordered arguments. Useful for cases when the 'nil case' is simple, but the 'cons case' is complex.

Since: 3.9.0

precListLookahead Source #

Arguments

:: forall (a :: S -> Type) (r :: S -> Type) (ell :: (S -> Type) -> S -> Type) (s :: S). (PElemConstraint ell a, PListLike ell) 
=> (Term s (a :--> (ell a :--> r)) -> Term s a -> Term s a -> Term s (ell a) -> Term s r)

The 'two or more' case. First Term s a is the 'head', second is a 'peek-ahead', while the Term s (ell a) is what remains after the 'peek-ahead'.

-> (Term s a -> Term s r)

The 'singleton' case, used both for true singletons and also for the end of a non-empty list-like.

-> Term s r

The 'nil' case.

-> Term s (ell a :--> r) 

ptryElimSingle :: forall (ell :: (S -> Type) -> S -> Type) (a :: S -> Type) (r :: S -> Type) (s :: S). (PElemConstraint ell a, PListLike ell) => (Term s a -> Term s r) -> Term s (ell a) -> Term s r Source #

Similar to pelimList, but assumes the argument list-like is a singleton, erroring otherwise.

Since: 3.9.0

Comparison

plistEqualsBy :: forall (list1 :: (S -> Type) -> S -> Type) (list2 :: (S -> Type) -> S -> Type) (a :: S -> Type) (b :: S -> Type) (s :: S). (PIsListLike list1 a, PIsListLike list2 b) => Term s ((a :--> (b :--> PBool)) :--> (list1 a :--> (list2 b :--> PBool))) Source #

Check if two lists are equal. The two list types can be different. The first argument is used for equality checks.

Since: 3.14.1

Singleton

pisSingleton :: forall (a :: S -> Type) (list :: (S -> Type) -> S -> Type) (s :: S). PIsListLike list a => Term s (list a :--> PBool) Source #

O(1) .Return true if the given list has exactly one element.

Since: 3.14.1

pfromSingleton :: forall (a :: S -> Type) (list :: (S -> Type) -> S -> Type) (s :: S). PIsListLike list a => Term s (list a :--> PMaybe a) Source #

Extract a singeton value from the given list. Return nothing if the list consists of zero or more than one elements.

Since: 3.14.1

ptryFromSingleton :: forall (a :: S -> Type) (list :: (S -> Type) -> S -> Type) (s :: S). PIsListLike list a => Term s (list a :--> a) Source #

Partial version of pfromSingleton.

Since: 3.14.1