From 25ce43bc647bf1e7b992ed956f0d67fa3afdd4b6 Mon Sep 17 00:00:00 2001 From: CryptoPascal31 Date: Fri, 13 Oct 2023 15:08:56 +0200 Subject: [PATCH] Add the contains* function --- README.md | 1 + docs/source/util-lists.rst | 18 ++++++++++++++++++ pact/contracts/util-lists.pact | 5 +++++ pact/tests_repl/util-lists-test.repl | 4 ++++ 4 files changed, 28 insertions(+) diff --git a/README.md b/README.md index 750a0a0..253f360 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ Contains functions to handle Pact lists. * ```(defun enforce-list-bounds:bool (x:list idx:integer)``` : Verify and enforce that *idx* is in list bounds * ```(defun chain:list (in:list)``` : Chain list of lists * ```(defun enumerate-list:[object] (in:list)``` : Returns a list of objects {'i:idx, 'v:value} where i is the index, and v the value +* ```(defun contains*:bool (in:list item)``` : Starred version of contains for list => arguments inverted * ```(defun first (in:list)``` : Returns the first item of a list * ```(defun last (in:list)``` : Returns the last item of the list * ```(defun at* (in:list idx:integer default)```: Returns the element at *idx*, but returns *default* if the list is too short diff --git a/docs/source/util-lists.rst b/docs/source/util-lists.rst index c52f4bb..2a668b2 100644 --- a/docs/source/util-lists.rst +++ b/docs/source/util-lists.rst @@ -72,6 +72,24 @@ Return a list of objects ``{'i:idx, 'v:value}`` where *'i* is the index, and *'v pact> (enumerate-list ["a", "b", "c"]) [{"i": 0,"v": "a"} {"i": 1,"v": "b"} {"i": 2,"v": "c"}] + +contains* +~~~~~~~~~ +*in* ``[]`` *item* ```` *→* ``bool`` + +Starred version of contains for list => arguments inverted. + +Useful for mapping and filtering + +.. code:: lisp + + pact> (contains* ["a", "b", "c"] "c") + true + + pact> (contains* ["a", "b", "c"] "d") + false + + first ~~~~~ *in* ``[]`` *→* ```` diff --git a/pact/contracts/util-lists.pact b/pact/contracts/util-lists.pact index c3b36f2..9c46189 100644 --- a/pact/contracts/util-lists.pact +++ b/pact/contracts/util-lists.pact @@ -44,6 +44,11 @@ (zip (lambda (idx:integer x) {'i:idx, 'v:x}) indexes in)) ) + (defun contains*:bool (in:list item) + "Starred version of contains for list => arguments inverted" + (contains item in) + ) + ;; Getter Functions (defun first (in:list) "Returns the first item of a list" diff --git a/pact/tests_repl/util-lists-test.repl b/pact/tests_repl/util-lists-test.repl index ec78d70..f2a7140 100644 --- a/pact/tests_repl/util-lists-test.repl +++ b/pact/tests_repl/util-lists-test.repl @@ -21,6 +21,10 @@ (expect-that "Enumerated single element list" (= [{'i:0,'v:"a"}]) (enumerate-list ["a"])) (expect-that "Enumerated empty list" (= []) (enumerate-list [])) +;contains* +(expect "List contains the element" true (contains* ["a", "b", "c"] "b")) +(expect "List does not contain the element" false (contains* ["a", "b", "c"] "d")) + ; first (expect-failure "Empty list must fail" "empty" (first [])) (expect-that "First item is a " (= "a") (first ["a", "b", "c", "d"]))