From 52ac625d2365dc8d93b6eb1937a02b1b178c47a5 Mon Sep 17 00:00:00 2001 From: Steve Kemp Date: Sat, 15 Oct 2022 08:57:29 +0300 Subject: [PATCH] Added (every) and (repeated) --- README.md | 2 +- stdlib/stdlib.lisp | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 49bc689..8ecc96a 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,7 @@ We have a reasonable number of functions implemented, either in our golang core * `help` will return the supplied help text for any functions which provide it - which includes all of our built-in functions and large parts of our standard-library. * **NOTE**: This does not (yet?) include help for special forms such as `(let* ..)`, `(if ..)`, etc. * List operations: - * `butlast`, `car`, `cdr`, `cons`, `drop`, `first`, `last`, `list`,`sort`, & `take`. + * `butlast`, `car`, `cdr`, `cons`, `drop`, `every`, `first`, `last`, `list`, `repeated`, `sort`, & `take`. * Logical operations: * `and`, & `or`. * Mathematical operations: diff --git a/stdlib/stdlib.lisp b/stdlib/stdlib.lisp index 15eeeb0..f47758e 100644 --- a/stdlib/stdlib.lisp +++ b/stdlib/stdlib.lisp @@ -139,6 +139,21 @@ false)))) +;; every is useful and almost a logical operation +(set! every (fn* (xs:list fun:function) + "Return true if applying every element of the list through the specified function resulted in a true result" + (let* (res (map xs fun)) + (if (and res) + true + false)))) + + +;; Useful for creating a list of numbers +(set! repeated (fn* (n:number x) + "Return a list of length n whose elements are all x" + (when (pos? n) + (cons x (repeated (dec n) x))))) + ;; inc/dec are useful primitives to have (set! inc (fn* (n:number) "inc will add one to the supplied value, and return the result."