From b1c1eb5cf1005a95becac4c30776436bac12593d Mon Sep 17 00:00:00 2001 From: Pekka Laiho Date: Tue, 22 Dec 2020 10:16:41 +0700 Subject: [PATCH] add util.mad with some utility functions --- mad/util.mad | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 mad/util.mad diff --git a/mad/util.mad b/mad/util.mad new file mode 100644 index 0000000..c80f99b --- /dev/null +++ b/mad/util.mad @@ -0,0 +1,16 @@ + +;; +;; Generic utility functions. +;; + +;; Return first item of sequence, or null if it is empty. +(defn first-or-null (seq) (if (empty? seq) null (first seq))) + +;; Split string by whitespace, removing empty strings. +(defn split-ws (s) (re-split "/[\\s]+/" s)) + +;; Split string by whitespace into first and rest. +;; Useful for parsing a string for command name and arguments. +(defn str-first-rest (s) + (let (words (split-ws s)) + [(first-or-null words) (tail words)]))