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
*
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."
22 (setf (aref arr idx
) (fn el
))
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
32 (setf (aref result idx
) (fn el
))
36 (defun member (item arr
)
37 "Check if ITEM is a member of ARR."
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."
48 (unless (member el arr-to-sub
)
49 (setf (aref result idx
) el
)
53 (defun reduce (func list
&optional init
) ;; the use of init here is actually a bit broken wrt null
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
)))))
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
))))