Commit | Line | Data |
---|---|---|
8b6fe833 AL |
1 | (in-package :js) |
2 | ||
9da682ca RD |
3 | ;;;; this file might be a little dated |
4 | ||
8b6fe833 AL |
5 | ;; This file contains JS code and is meant to be compiled and included |
6 | ;; into the host environment in one way or another | |
7 | ||
8 | (defun map-into (fn arr) | |
9 | "Call FN on each element in ARR, replace element with the return value." | |
10 | (let ((idx 0)) | |
11 | (dolist (el arr) | |
12 | (setf (aref arr idx) (fn el)) | |
13 | (setf idx (1+ idx)))) | |
14 | (return arr)) | |
15 | ||
16 | (defun map (fn arr) | |
17 | "Call FN on each element in ARR and return the returned values in a new array." | |
18 | ;; This may call Array.map, too | |
19 | (let ((idx 0) | |
20 | (result (array))) | |
21 | (dolist (el arr) | |
22 | (setf (aref result idx) (fn el)) | |
23 | (setf idx (1+ idx))) | |
24 | (return result))) | |
25 | ||
26 | (defun map-until (fn arr) | |
27 | "Call FN on each element in ARR until it returns something. If so return that value." | |
28 | (let ((result)) | |
29 | (dolist (el arr) | |
30 | (setf result (fn el)) | |
31 | (unless (= result undefined) | |
32 | (return result))))) | |
33 | ||
34 | (defun member (item arr) | |
35 | "Check if ITEM is a member of ARR." | |
36 | (dolist (el arr) | |
37 | (if (= el item) | |
38 | (return true))) | |
39 | (return false)) | |
40 | ||
41 | (defun append (arr1 arr2) | |
42 | "Return a new array with the contents of ARR1 and ARR2. If ARR2 is not an array | |
43 | then append it as a member." | |
44 | (let ((result (array)) | |
45 | (idx 0)) | |
46 | (dolist (el arr1) | |
47 | (setf (aref result idx) el) | |
48 | (setf idx (1+ idx))) | |
49 | (unless (= arr2 undefined) | |
50 | (if (instanceof arr2 *array) | |
51 | (dolist (el arr2) | |
52 | (setf (aref result idx) el) | |
53 | (setf idx (1+ idx))) | |
54 | (setf (aref result idx) arr2)))) | |
55 | (return result)) | |
56 | ||
57 | (defun set-difference (arr arr-to-sub) | |
58 | "Return a new array with only those elements in ARR that are not in ARR-TO-SUB." | |
59 | (let ((idx 0) | |
60 | (result (array))) | |
61 | (dolist (el arr) | |
62 | (unless (member el arr-to-sub) | |
63 | (setf (aref result idx) el) | |
64 | (setf idx (1+ idx)))) | |
65 | (return result))) | |
66 |