3c61c70e4bae35bb3f7d3288d9e15d5bc450a1cb
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 as
)
9 (let ((result-array (make-array)))
11 (dolist (element (aref as
0))
12 (result-array.push
(fun element
)))
13 (dotimes (i (length (aref as
0)))
14 (let ((args-array (mapcar (lambda (a) (return (aref a i
))) as
)))
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 map-until (fn arr
)
37 "Call FN on each element in ARR until it returns something. If so return that value."
41 (unless (= result undefined
)
44 (defun member (item arr
)
45 "Check if ITEM is a member of ARR."
51 (defun append (arr1 arr2
)
52 "Return a new array with the contents of ARR1 and ARR2. If ARR2 is not an array
53 then append it as a member."
54 (let ((result (array))
57 (setf (aref result idx
) el
)
59 (unless (= arr2 undefined
)
60 (if (instanceof arr2
*array
)
62 (setf (aref result idx
) el
)
64 (setf (aref result idx
) arr2
))))
67 (defun set-difference (arr arr-to-sub
)
68 "Return a new array with only those elements in ARR that are not in ARR-TO-SUB."
72 (unless (member el arr-to-sub
)
73 (setf (aref result idx
) el
)
77 (defun reduce (func list
&optional init
) ;; the use of init here is actually a bit broken wrt null
79 (do* ((i (if init -
1 0) (1+ i
))
80 (acc (if init init
(elt list
0)) (func acc
(elt list i
))))
81 ((>= i
(1- (length list
)))))