Added docstring output for var and defun when *js-print-pretty* is t.
[parenscript.git] / runtime / ps-runtime-lib.lisp
blobdc6f23cf2c2fe84479ed9f50c992f52d3bd12314
1 (in-package #:parenscript)
3 ;;; Script of library functions you can include with your own code to
4 ;;; provide standard Lisp functionality.
6 (defparameter *ps-lisp-library*
7 '(progn
8 (defun mapcar (fun &rest arrs)
9 (let ((result-array (make-array)))
10 (if (= 1 (length arrs))
11 (dolist (element (aref arrs 0))
12 ((@ result-array push) (fun element)))
13 (dotimes (i (length (aref arrs 0)))
14 (let ((args-array (mapcar (lambda (a) (return (aref a i))) arrs)))
15 ((@ result-array push) ((@ fun apply) fun args-array)))))
16 (return result-array)))
18 (defun map-into (fn arr)
19 "Call FN on each element in ARR, replace element with the return value."
20 (let ((idx 0))
21 (dolist (el arr)
22 (setf (aref arr idx) (fn el))
23 (setf idx (1+ idx))))
24 (return arr))
26 (defun map (fn arr)
27 "Call FN on each element in ARR and return the returned values in a new array."
28 ;; In newer versions of ECMAScript, this may call Array.map, too
29 (let ((idx 0)
30 (result (array)))
31 (dolist (el arr)
32 (setf (aref result idx) (fn el))
33 (setf idx (1+ idx)))
34 (return result)))
36 (defun member (item arr)
37 "Check if ITEM is a member of ARR."
38 (dolist (el arr)
39 (if (= el item)
40 (return true)))
41 (return false))
43 (defun set-difference (arr arr-to-sub)
44 "Return a new array with only those elements in ARR that are not in ARR-TO-SUB."
45 (let ((idx 0)
46 (result (array)))
47 (dolist (el arr)
48 (unless (member el arr-to-sub)
49 (setf (aref result idx) el)
50 (setf idx (1+ idx))))
51 (return result)))
53 (defun reduce (func list &optional init) ;; the use of init here is actually a bit broken wrt null
54 (let* ((acc))
55 (do* ((i (if init -1 0) (1+ i))
56 (acc (if init init (elt list 0)) (func acc (elt list i))))
57 ((>= i (1- (length list)))))
58 (return acc)))
60 (defun nconc (arr &rest arrs)
61 (when (and arr (> (length arr) 0))
62 (loop :for other :in arrs :when (and other (> (length other) 0)) :do
63 ((@ arr :splice :apply) arr
64 (append (list (length arr) (length other)) other))))
65 arr)))