2023-03-20 16:22:51 -07:00
|
|
|
#!/bin/relish
|
|
|
|
|
|
2023-03-17 13:06:27 -07:00
|
|
|
;; relish: versatile lisp shell
|
|
|
|
|
;; Copyright (C) 2021 Aidan Hahn
|
|
|
|
|
;;
|
|
|
|
|
;; This program is free software: you can redistribute it and/or modify
|
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
;;
|
|
|
|
|
;; This program is distributed in the hope that it will be useful,
|
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
;;
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
|
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2023-03-17 13:24:31 -07:00
|
|
|
|
2023-03-17 13:06:27 -07:00
|
|
|
;; USERLIB
|
2023-03-20 16:22:51 -07:00
|
|
|
;; This file contains useful features that are not shipped in the STL
|
2023-03-17 13:06:27 -07:00
|
|
|
|
2023-03-20 16:22:51 -07:00
|
|
|
;; this would be way faster as code in stl
|
|
|
|
|
;; but stl already suffers scope creep
|
2023-03-17 13:06:27 -07:00
|
|
|
(def prepend
|
|
|
|
|
'takes a list and appends an element to the back of it.
|
|
|
|
|
returns prepended list'
|
|
|
|
|
(elem list)
|
2023-03-20 16:22:51 -07:00
|
|
|
(reverse (cons (reverse list) elem)))
|
2023-03-17 13:06:27 -07:00
|
|
|
|
2023-03-20 16:22:51 -07:00
|
|
|
;; please dont misuse this tool
|
2023-03-17 13:06:27 -07:00
|
|
|
(def set
|
2023-03-20 16:22:51 -07:00
|
|
|
'sets an existing variable without touching its docstring.
|
|
|
|
|
|
|
|
|
|
WARNING: abandon hope all ye who declare and then modify global variables!
|
|
|
|
|
If you find yourself struggling to debug a complex error in state access,
|
|
|
|
|
or you are having issues re-running commands in the shell consider the
|
|
|
|
|
following advice:
|
|
|
|
|
|
|
|
|
|
It is very much an anti pattern to mutate global variable that contain state
|
|
|
|
|
refactor your program: put iterators, counters, procedurally generated code,
|
|
|
|
|
and all other mutable state into a let loop.
|
|
|
|
|
|
|
|
|
|
A zen script in relish is one where each root level form (or eval at repl)
|
|
|
|
|
is self contained, and does not permanently modify any other one.
|
|
|
|
|
|
|
|
|
|
See the userlib tests for an easy to follow example of this.'
|
|
|
|
|
|
2023-03-17 13:06:27 -07:00
|
|
|
(var val)
|
2023-03-20 16:22:51 -07:00
|
|
|
|
2023-03-17 13:06:27 -07:00
|
|
|
(let ((doc (get-doc var)))
|
2023-03-20 16:22:51 -07:00
|
|
|
(def (eval var) doc val)))
|
|
|
|
|
|
|
|
|
|
(def map
|
|
|
|
|
'Takes two arguments: a function and a list.
|
|
|
|
|
for each element in the list, the function is applied and the
|
|
|
|
|
result is added to a new list. Returns the new list.'
|
|
|
|
|
(func list)
|
|
|
|
|
(let ((list-iter (pop list))
|
|
|
|
|
(result ()))
|
|
|
|
|
(while (gt? (len list-iter) 1)
|
|
|
|
|
(let ((current (car list-iter))
|
|
|
|
|
(remaining (cdr list-iter))
|
|
|
|
|
(current-res (func current)))
|
|
|
|
|
(set (q result) (cons result current-res))
|
|
|
|
|
(set (q list-iter) (pop remaining))))
|
|
|
|
|
result))
|