Skip to content

Commit

Permalink
Add the contains* function
Browse files Browse the repository at this point in the history
  • Loading branch information
CryptoPascal31 committed Oct 13, 2023
1 parent d7e0964 commit 25ce43b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions docs/source/util-lists.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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* ``[<a>]`` *item* ``<a>`` ** ``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* ``[<a>]`` ** ``<a>``
Expand Down
5 changes: 5 additions & 0 deletions pact/contracts/util-lists.pact
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions pact/tests_repl/util-lists-test.repl
Original file line number Diff line number Diff line change
Expand Up @@ -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"]))
Expand Down

0 comments on commit 25ce43b

Please sign in to comment.