-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathFUNC_PROG.lsp
137 lines (109 loc) · 4.58 KB
/
FUNC_PROG.lsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
;;;;;;;[ Functional Programming ];;;;;;;;;;;;;;;
;; ;;
;; Functions for functional programming. ;;
;; ;;
;;::::::::::::::::::::::::::::::::::::::::::::::;;
;; ;;
;; Author: J.D. Sandifer (Copyright 2016) ;;
;; Written: 07/26/2016 ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; 08/29/2016 ;;
;; - Added Range and unit test for it. ;;
;; ;;
;; 07/26/2016 ;;
;; - Started with Filter. ;;
;; ;;
;; Todo: ;;
;; - Make functions more efficient? ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Runs all unit tests for this file
; Output: List of test results and summary
(defun C:TestFuncProg ( / testList)
;; Setup for tests
(princ "\n")
(princ "Testing FUNC_PROG\n")
(princ "--------------------\n")
;; Actual tests
;(princ "Map\n")
;(princ "\n")
(princ "Range\n")
(Assert 'Range '(1 1 1) '(1))
(Assert 'Range '(1 2 1) '(1 2))
(Assert 'Range '(1 5 1) '(1 2 3 4 5))
(Assert 'Range '(1 10 2) '(1 3 5 7 9))
(Assert 'Range '(2 10 2) '(2 4 6 8 10))
(Assert 'Range '(5 1 -1) '(5 4 3 2 1))
(Assert 'Range '(10 2 -2) '(10 8 6 4 2))
;; Displaying the results of the tests
(JD:PrintTestResults (JD:CountBooleans testList)))
;|===={ Filter }============================|;
;| Returns a sublist of the supplied list |;
;| including only values that return true |;
;| when input to the supplied function. |;
;|------------------------------------------|;
;| Author: J.D. Sandifer Rev: 07/26/2016 |;
;|==========================================|;
(defun Filter (comparisonFunction listToFilter / listToReturn)
(foreach item listToFilter
(if (apply comparisonFunction (list item))
(setq listToReturn
(append listToReturn (list item)))))
listToReturn)
;|===={ Map }===============================|;
;| Simple wrapper for the "mapcar" function |;
;| to allow it to be accessed by the more |;
;| common functional programming name. |;
;|------------------------------------------|;
;| Author: J.D. Sandifer Rev: 08/01/2016 |;
;|==========================================|;
(defun Map (theFunction theList / )
(mapcar theFunction theList))
;|===={ Range }=============================|;
;| Creates a list of sequential numbers |;
;| based on an initial value, increment, |;
;| and a final value to reach or not |;
;| exceed at the least. Useful for turning |;
;| foreach into a typical for loop. |;
;|------------------------------------------|;
;| Author: J.D. Sandifer Rev: 08/29/2016 |;
;|==========================================|;
(defun Range (value finalValue increment / rangeList comparator)
(if (>= finalValue value)
(setq comparator '<=)
(setq comparator '>=))
(while ((eval comparator) value finalValue)
(setq rangeList (append rangeList (list value)))
(setq value (+ increment value)))
rangeList)
;|===={ Reduce }============================|;
;| Applies a supplied function to the |;
;| first two values of the supplied list. |;
;| The result is then used as the first |;
;| value for the function and the next |;
;| value from the list is the second. This |;
;| continues until the list is *reduced* |;
;| to one value which is returned. The |;
;| given function must expect two inputs. |;
;|------------------------------------------|;
;| Author: J.D. Sandifer Rev: 07/26/2016 |;
;|==========================================|;
(defun Reduce (reductionFunction listToReduce / valueToReturn)
(setq valueToReturn (car listToReduce))
(setq listToReduce (cdr listToReduce))
(foreach item listToReduce
(setq valueToReturn
(apply reductionFunction (list valueToReturn item))))
valueToReturn)
;;----------------------------------------------------------------------;;
(princ
(strcat
"\n:: FUNC_PROG.lsp loaded. | \\U+00A9 J.D. Sandifer "
(menucmd "m=$(edtime,0,yyyy)")
" ::"))
(princ)
;;----------------------------------------------------------------------;;
;; End of File ;;
;;----------------------------------------------------------------------;;