application/octet-stream
•
2.93 KB
•
111 lines
; Some nujel string λs
[defun println [str]
"Print STR on a single line"
[print [cat str "\n"]]
]
[defun display [value]
"Display VALUE"
[print value]
]
[defun newline []
"Print a single line feed character"
[display "\n"]
]
[defun br [num]
"Return NUM=1 linebreaks"
[if [or [nil? num] [<= [int num] 1]]
"\n"
["\n" [br [-- num]]]]
]
[defun path/ext?! [ext]
"Return a predicate that checks if a path ends on EXT"
[λ [path]
[eq? ext [lowercase [path/extension path]]]
]
]
[defun path/extension [path]
"Return the extension of PATH"
[def last-period [last-index-of path "."]]
[if [>= last-period 0]
[substr path [+ 1 last-period] [string/length path]]
path]
]
[defun path/without-extension [path]
"Return PATH, but without the extension part"
[def last-period [last-index-of path "."]]
[if [>= last-period 0]
[substr path 0 last-period]
path]
]
[defun int->string/binary [α]
"Turn α into a its **binary** string representation"
[def ret ""]
[when-not α [def α 0]]
[when [zero? α] [set! ret "0"]]
[while [not-zero? α]
[set! ret [cat [from-char-code [+ #\0 [logand α #b1]]] ret]]
[set! α [ash α -1]]
]
ret
]
[defun int->string/octal [α]
"Turn α into a its **octal** string representation"
[def ret ""]
[when-not α [def α 0]]
[when [zero? α] [set! ret "0"]]
[while [not-zero? α]
[set! ret [cat [from-char-code [+ #\0 [logand α #b111]]] ret]]
[set! α [ash α -3]]
]
ret
]
[def int->string/hex [let*
"Turn α into a its **hexadecimal** string representation"
[def conversion-arr #["0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "A" "B" "C" "D" "E" "F"]]
[λ [α]
[def ret ""]
[when-not α [def α 0]]
[when [zero? α] [set! ret "0"]]
[while [not-zero? α]
[set! ret [cat [conversion-arr [logand α #b1111]] ret]]
[set! α [ash α -4]]
]
ret
]]]
[defun int->string/decimal [α]
"Turn α into a its **decimal** string representation"
[string α]
]
[def int->string int->string/decimal]
[defun string/pad-start [text goal-length char]
[when-not char [set! char " "]]
[while [< [string/length text] goal-length]
[set! text [cat char text]]
]
[if [> [string/length text] goal-length]
[substr text [- [string/length text] goal-length] [string/length text]]
text]
]
[defun string/pad-end [text goal-length char]
[when-not char [set! char " "]]
[while [< [string/length text] goal-length]
[set! text [cat text char]]
]
[if [> [string/length text] goal-length]
[substr text 0 goal-length]
text]
]