1 ;;;; -*- encoding:utf-8 -*-
3 ;;; Copyright 2005 Manuel Odendahl
4 ;;; Copyright 2005-2006 Edward Marco Baringer
5 ;;; Copyright 2009-2011, 2018-2019 Vladimir Sedach
6 ;;; Copyright 2019 Philipp Marek
8 ;;; SPDX-License-Identifier: BSD-3-Clause
10 (in-package #:parenscript
)
12 ;;; Script of library functions you can include with your own code to
13 ;;; provide standard Lisp functionality.
15 (defparameter *ps-lisp-library
*
17 (defun mapcar (fun &rest arrs
)
18 (let ((result-array (make-array)))
19 (if (= 1 (length arrs
))
20 (dolist (element (aref arrs
0))
21 ((@ result-array push
) (fun element
)))
22 (dotimes (i (length (aref arrs
0)))
23 (let ((args-array (mapcar (lambda (a) (aref a i
)) arrs
)))
24 ((@ result-array push
) ((@ fun apply
) fun args-array
)))))
27 (defun map-into (fn arr
)
28 "Call FN on each element in ARR, replace element with the return value."
31 (setf (aref arr idx
) (fn el
))
36 "Call FN on each element in ARR and return the returned values in a new array."
37 ;; In newer versions of ECMAScript, this may call Array.map, too
41 (setf (aref result idx
) (fn el
))
46 (string= (typeof v
) "string"))
49 (string= (typeof v
) "string"))
52 "Return whether NUM is zero."
56 "Return whether NUM is positive."
60 "Return whether NUM is negative."
63 (defun member (item arr
)
64 "Check if ITEM is a member of ARR."
67 (return-from member true
)))
70 (defun set-difference (arr arr-to-sub
)
71 "Return a new array with only those elements in ARR that are not in ARR-TO-SUB."
75 (unless (member el arr-to-sub
)
76 (setf (aref result idx
) el
)
80 (defun reduce (func list
&optional init
)
82 (do* ((i (if (= (length arguments
) 3) -
1 0)
84 (acc (if (= (length arguments
) 3) init
(elt list
0))
85 (func acc
(elt list i
))))
86 ((>= i
(1- (length list
)))))
89 (defun nconc (arr &rest arrs
)
90 (when (and arr
(> (length arr
) 0))
91 (loop :for other
:in arrs
:when
(and other
(> (length other
) 0)) :do
92 ((@ arr
:splice
:apply
) arr
93 (append (list (length arr
) (length other
)) other
))))