add mod and exp functions, tests, and also some snippets for common

patterns in the readme
This commit is contained in:
Ava Hahn 2023-03-07 21:27:45 -08:00
parent 7fd47c1812
commit 0c5d14ed8e
Signed by untrusted user who does not match committer: affine
GPG key ID: 3A4645B8CF806069
4 changed files with 323 additions and 13 deletions

View file

@ -37,17 +37,67 @@ https://matrix.to/#/#vomitorium:matrix.sunnypup.io
**** TODO Anatomy
**** TODO Naming conventions
**** TODO Undefining variables and functions
*** TODO Easy patterns
**** TODO while-let combo
**** TODO main loop application
**** TODO let destructuring
**** TODO if-set?
*** TODO Builtin functions
*** TODO Documentation
**** TODO Tests
**** TODO Help function
**** TODO Env function
**** TODO Snippets directory
** Easy patterns
This section can serve as a sort of cookbook for a user who is new to leveraging LISP languages or unsure of where to start with ~relish~.
More ideas may be explored in the file:snippets directory of this project.
The author encourages any users to contribute their own personal favorites not already in this section either by adding them to the file:snippets folder, or to extend the documentation here.
*** while-let combo
#+BEGIN_SRC lisp
;; myiter = (1 (2 3 4 5 6))
(def myiter 'iterator over a list' (head (1 2 3 4 5 6)))
;; iterate over each element in mylist
(while (gt? (len (cdr myiter)) 0) ;; while there are more elements to consume
(let ((elem (car myiter)) ;; elem = consumed element from myiter
(remaining (cdr myiter))) ;; remaining = rest of elements
(echo elem) ;; do a thing with the element, could be any operation
(def myiter (head remaining)))) ;; consume next element, loop
#+END_SRC
The while-let pattern can be used for many purposes. Above it is used to iterate over elements in a list. It can also be used to receive connections to a socket and write data to them.
*** TODO main loop application
- state switch (while-toggle)
- state calculation
*** TODO short-circuit guard
- circuit example
- while-not-circuit-do-more-work
*** let destructuring
~let~ is very useful for destructuring complex return types. If you have a function that may return a whole list of values you can then call it from ~let~ to consume the result data.
In this example a let form is used to destructure a call to ~head~. ~head~ returns a list consisting of ~(first-element rest-of-list)~ (for more information see ~(help head)~).
The ~let~ form starts with the output of ~head~ stored in ~head-struct~ (short for head-structured). The next variables defined are ~first~ and ~rest~ which contain individual elements from the return of the call to ~head~.
Finally, the bodies evaluated in the ~let~ form are able to operate on the head and the rest.
#+BEGIN_SRC lisp
;; individually access the top of a list
(let ((head-struct (head (1 2 3))
(first (car head-struct))
(rest (cdr head-struct)))
(echo "this is 1: " first)
(echo "this is 2, 3: " rest))
#+END_SRC
*** if-set?
One common pattern seen in bash scripts and makefiles is the set-variable-if-not-set pattern.
#+BEGIN_SRC shell
MYVAR ?= MY_SPECIAL_VALUE
#+END_SRC
Translated, can be seen below
#+BEGIN_SRC lisp
(if (set? myvar)
() ;; no need to do anything... or add a call here
(def myvar "MY_SPECIAL_VALUE"))
#+END_SRC
Alternatively this combination can be used to process flags in a script or application:
#+BEGIN_SRC lisp
(if (set? myflag)
(process-flag myflag)
())
#+END_SRC
** TODO Builtin functions
*** TODO Env function
** TODO Documentation
*** TODO Tests
*** TODO Help function
*** TODO Snippets directory
** Configuration
By default Relish will read from ~/.relishrc for configuration, but the default shell will also accept a filename from the RELISH_CFG_FILE environment variable.
@ -158,8 +208,8 @@ Will need a concatenate function for tables
**** DONE sub
**** DONE div
**** DONE mul
**** TODO exp
**** TODO mod
**** DONE exp
**** DONE mod
***** TODO Test for Let Destructuring
**** TODO inc
**** TODO dec