mirror of
https://github.com/teo3300/rust-mal.git
synced 2026-01-12 01:05:32 +01:00
88 lines
2.8 KiB
Plaintext
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 tt r]
|
|
(if (empty? l)
|
|
(cons tt r)
|
|
(do (def! cc (car l))
|
|
(if (= cc c)
|
|
(split-r (cdr l) "" (cons tt r))
|
|
(split-r (cdr l) (str tt 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 tt]
|
|
(if (empty? l)
|
|
tt
|
|
(join-r (cdr l) (str tt 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))))
|