Files
rust-mal/libs/string.mal
teo3300 abb23a8b48 Making all parameters optionals
If a parameter is not provided, it's set to nil
2024-06-25 18:27:23 +09:00

88 lines
2.8 KiB
Plaintext

(def! char (fn* [l]
(car (boom l))))
(def! #n (char "\n"))
(def! #s (char " "))
(def! #t (char "\t"))
(def! char? (fn* [a]
(= (type a) :char)))
(def! string? (fn* [a]
(= (type a) :string)))
(def! strlen (fn* [s] (count (boom s))))
(def! strc (fn* [l]
(def! strc-r (fn* [l s]
(if (empty? l)
s
(strc-r (cdr l) (str s (car l))))))
(strc-r l "")))
(def! _split-ch (fn* [s c]
"Split the string at every occurrence of character sc"
(def! s (boom s))
(def! split-r (fn* [l t r]
(if (empty? l)
(cons t r)
(do (def! cc (car l))
(if (= cc c)
(split-r (cdr l) "" (cons t r))
(split-r (cdr l) (str t cc) r))))))
(reverse (split-r s "" '()))))
(def! split (fn* [string delimiter]
"Split the string at every occurrence of substring delimiter"
"An empty delimiter is splitting every character"
(if (= (strlen delimiter) 1)
(_split-ch string (char delimiter))
(do (def! delimiter (boom delimiter))
(def! split-r (fn* [string tklist matches chunk chunks]
(if (empty? string)
(cons chunk chunks)
(do (def! curr (car string))
(def! string (cdr string))
(if (empty? tklist)
(split-r string delimiter "" (str curr) (cons chunk chunks))
(if (= curr (car tklist))
(split-r string (cdr tklist) (str matches curr) chunk chunks)
(split-r string delimiter "" (str chunk matches curr) chunks)))))))
(reverse (split-r (boom string) delimiter "" "" '()))))))
(def! join (fn* [l s]
"Join element of list l to a stiring, using s as separator"
(def! s (or s ""))
(def! join-r (fn* [l t]
(if (empty? l)
t
(join-r (cdr l) (str t s (car l))))))
(join-r (cdr l) (car l))))
(def! chsub (fn* [s c1 c2]
(strc (map-if (fn* [x] (= x c1)) (fn* [x] c2) (boom s)))))
(def! parse-csv (fn* [filename row-s col-s]
(def! col-s (or col-s "\n"))
(def! row-s (or row-s ","))
(map (fn* [x] (split x row-s)) (filter (fn* [x] (not (= x "")))(split (slurp filename) col-s)))))
(def! _toint {"0" 0 "1" 1
"2" 2 "3" 3
"4" 4 "5" 5
"6" 6 "7" 7
"8" 8 "9" 9})
(def! parseint (fn* [string]
(def! string (boom string))
(def! sign (car string))
(def! parseint-r (fn* [string val]
(if (empty? string)
val
(if (not (def! chint (_toint (str (car string)))))
(raise "Failed to convert string to number")
(parseint-r (cdr string) (+ (* val 10) chint))))))
(if (= sign (char "-"))
(-(parseint-r (cdr string) 0))
(parseint-r string 0))))