From 9b82db238f9e2e02a76f95c793f8d6ef2387ecfd Mon Sep 17 00:00:00 2001 From: unwox Date: Thu, 26 Sep 2024 17:46:38 +0600 Subject: init --- lib/array.fnl | 13 +++++++++++++ lib/macro.fnl | 18 ++++++++++++++++++ lib/number.fnl | 6 ++++++ lib/string.fnl | 11 +++++++++++ 4 files changed, 48 insertions(+) create mode 100644 lib/array.fnl create mode 100644 lib/macro.fnl create mode 100644 lib/number.fnl create mode 100644 lib/string.fnl (limited to 'lib') diff --git a/lib/array.fnl b/lib/array.fnl new file mode 100644 index 0000000..11e4d2a --- /dev/null +++ b/lib/array.fnl @@ -0,0 +1,13 @@ +(import-macros {: reduce} :lib.macro) + +(fn concat [a b] + (local copy + (fn [a b] + (reduce + (fn [_ v c] + (table.insert c v) + c) + b a))) + (copy a b)) + +{: concat} diff --git a/lib/macro.fnl b/lib/macro.fnl new file mode 100644 index 0000000..f23b1fb --- /dev/null +++ b/lib/macro.fnl @@ -0,0 +1,18 @@ +(fn map [f l] + `(icollect [i# v# (ipairs ,l)] + (,f i# v#))) + +(fn filter [f l] + `(icollect [i# v# (ipairs ,l)] + (if (,f i# v#) + v# + nil))) + +(fn reduce [f l s] + `(accumulate [r# ,s + i# v# (ipairs ,l)] + (,f i# v# r#))) + +{: map + : filter + : reduce} diff --git a/lib/number.fnl b/lib/number.fnl new file mode 100644 index 0000000..61d8cc4 --- /dev/null +++ b/lib/number.fnl @@ -0,0 +1,6 @@ +(fn string->number [str] + (if str + (tonumber (pick-values 1 (str:gsub "[^0-9.]" ""))) + nil)) + +{: string->number} diff --git a/lib/string.fnl b/lib/string.fnl new file mode 100644 index 0000000..466dac9 --- /dev/null +++ b/lib/string.fnl @@ -0,0 +1,11 @@ +(fn split [string] + (accumulate [res [] v _ + (string:gmatch "%S+")] + (do + (table.insert res v) + res))) + +(fn ends-with [string end] + (= (string.sub string (- (# end))) end)) + +{: split : ends-with} -- cgit v1.2.3