From 15d132bdd07e16a9ff3ce0fa4aefc82871d4f755 Mon Sep 17 00:00:00 2001 From: Steve Kemp Date: Thu, 2 Jul 2026 21:46:28 +0300 Subject: [PATCH] Added split-all --- PRIMITIVES.md | 3 +++ stdlib.slisp | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/PRIMITIVES.md b/PRIMITIVES.md index 6897714..b86bc33 100644 --- a/PRIMITIVES.md +++ b/PRIMITIVES.md @@ -130,6 +130,9 @@ Note that functions have their names mangled a little bit ("-" is converted to " * The list is updated in-place. * `split` * Split a string by the given character, and return a list of "(before after)". Return nil if the character isn't found. + * `split-all` + * Return a list of all parts of string, split by the character. + * e.g. `(split-all (getenv "PATH") #\:)` to find all directories on the PATH. * `strcat` * Join two strings together and return them. * `strcmp` diff --git a/stdlib.slisp b/stdlib.slisp index 9a7d550..4d0fec9 100644 --- a/stdlib.slisp +++ b/stdlib.slisp @@ -255,6 +255,24 @@ Incrementing by the given step each time." "Reverse the contents of the specified list." (reverse-inner xs nil)) +(defun split-all (str char) + "Return a list of splitting the string by the given character. + + Empty parts are ignored." + (if (or (nil? str) (= str "")) + nil + (filter (split-all-helper str char) (lambda (x) (> (length x) 0))))) + +(defun split-all-helper (str char) + "Recursive helper for splitting a list by character." + (let ((parts (split str char))) + (if (nil? parts) + (list str) + (cons (nth parts 0) + (split-all-helper (nth parts 1) char))))) + + + ;;; system functions