|
68 | 68 |
|
69 | 69 | ### |
70 | 70 | # @meta List |
71 | | -# @brief |
72 | | -# @details |
| 71 | +# @brief Drop the first n elements of a list |
| 72 | +# @details The original list is left unmodified. |
73 | 73 | # --- |
| 74 | +# (let cool-stuff [1 2 3 4 5 6 7 8 9]) |
| 75 | +# (print (list:drop cool-stuff 4)) # [5 6 7 8 9] |
74 | 76 | # --- |
75 | 77 | # @param _L the list to work on |
| 78 | +# @param _n the number of elements to drop |
76 | 79 | # @author https://github.com/rstefanic |
77 | 80 | ### |
78 | 81 | (let list:drop (fun (_L _n) { |
|
87 | 90 |
|
88 | 91 | ### |
89 | 92 | # @meta List |
90 | | -# @brief |
91 | | -# @details |
92 | | -# --- |
93 | | -# --- |
94 | | -# @param _L the list to work on |
95 | | -# @author https://github.com/rstefanic |
96 | | -### |
97 | | -(let list:dropWhile (fun (_L _f) { |
98 | | - (mut _index 0) |
99 | | - (mut _output []) |
100 | | - (mut _continue true) |
101 | | - |
102 | | - (while (and (< _index (len _L)) _continue) { |
103 | | - (if (_f (@ _L _index)) { |
104 | | - (set _index (+ 1 _index)) |
105 | | - (set _output (append _output (@ _L _index))) |
106 | | - } |
107 | | - (set _continue false)) |
108 | | - }) |
109 | | - _output |
110 | | -})) |
111 | | - |
112 | | -### |
113 | | -# @meta List |
114 | | -# @brief |
115 | | -# @details |
| 93 | +# @brief Keep elements in a given list if they follow a predicate |
| 94 | +# @details The original list is left unmodified. |
116 | 95 | # --- |
| 96 | +# (import "Math.ark") |
| 97 | +# (print (list:filter [1 2 3 4 5 6 7 8 9] math:even)) # [2 4 6 8] |
117 | 98 | # --- |
118 | 99 | # @param _L the list to work on |
| 100 | +# @param _f the predicate |
119 | 101 | # @author https://github.com/rstefanic |
120 | 102 | ### |
121 | 103 | (let list:filter (fun (_L _f) { |
|
131 | 113 |
|
132 | 114 | ### |
133 | 115 | # @meta List |
134 | | -# @brief |
135 | | -# @details |
| 116 | +# @brief Applies a given function to each element of a list |
| 117 | +# @details The original list is left unmodified. |
136 | 118 | # --- |
| 119 | +# (print (list:map [1 2 3 4 5 6 7 8 9] (fun (e) (* e e)))) # [1 4 9 25 36 49 64 81] |
137 | 120 | # --- |
138 | 121 | # @param _L the list to work on |
| 122 | +# @param _f the function to apply to each element |
139 | 123 | # @author https://github.com/rstefanic |
140 | 124 | ### |
141 | 125 | (let list:map (fun (_L _f) { |
|
150 | 134 |
|
151 | 135 | ### |
152 | 136 | # @meta List |
153 | | -# @brief |
154 | | -# @details |
| 137 | +# @brief Apply a function to the elements of a list to reduce it |
| 138 | +# @details The original list is left unmodified. |
155 | 139 | # --- |
| 140 | +# (let cool [1 2 3 4 5 6 7 8 9]) |
| 141 | +# (print (list:reduce cool (fun (a b) (+ a b)))) # 45 |
156 | 142 | # --- |
157 | 143 | # @param _L the list to work on |
| 144 | +# @param _f the function to apply |
158 | 145 | # @author https://github.com/FrenchMasterSword |
159 | 146 | ### |
160 | 147 | (let list:reduce (fun (_L _f) { |
|
167 | 154 | _output |
168 | 155 | })) |
169 | 156 |
|
| 157 | +(import "Math.ark") # needed for (math:min a b) |
| 158 | + |
170 | 159 | ### |
171 | 160 | # @meta List |
172 | | -# @brief |
173 | | -# @details |
| 161 | +# @brief Take the first n elements of |
| 162 | +# @details The original list is left unmodified. |
174 | 163 | # --- |
| 164 | +# (print (list:take [1 2 3 4 5 6 7 8 9] 4)) # [1 2 3 4] |
175 | 165 | # --- |
176 | 166 | # @param _L the list to work on |
| 167 | +# @param _n the number of elements to take |
177 | 168 | # @author https://github.com/rstefanic |
178 | 169 | ### |
179 | 170 | (let list:take (fun (_L _n) { |
180 | 171 | (mut _index 0) |
181 | 172 | (mut _output []) |
182 | | - (while (and (< _index _n) (< _index (len _L))) { |
| 173 | + (set _n (math:min _n (len _L))) |
| 174 | + |
| 175 | + (while (< _index _n) { |
183 | 176 | (set _output (append _output (@ _L _index))) |
184 | 177 | (set _index (+ 1 _index)) |
185 | 178 | }) |
|
188 | 181 |
|
189 | 182 | ### |
190 | 183 | # @meta List |
191 | | -# @brief |
192 | | -# @details |
193 | | -# --- |
194 | | -# --- |
195 | | -# @param _L the list to work on |
196 | | -# @author https://github.com/rstefanic |
197 | | -### |
198 | | -(let list:takeWhile (fun (_L _f) { |
199 | | - (mut _index 0) |
200 | | - (mut _output []) |
201 | | - (mut _continue true) |
202 | | - |
203 | | - (while (and (< _index (len _L)) _continue) { |
204 | | - (if (_f (@ _L _index)) { |
205 | | - (set _output (append _output (@ _L _index))) |
206 | | - (set _index (+ 1 _index)) |
207 | | - } |
208 | | - (set _continue false) |
209 | | - ) |
210 | | - }) |
211 | | - _output |
212 | | -})) |
213 | | - |
214 | | -### |
215 | | -# @meta List |
216 | | -# @brief |
217 | | -# @details |
| 184 | +# @brief Unzip a list of [[a b] [c d]...] into [[a c ...] [b d ...]] |
| 185 | +# @details The original list is left unmodified. |
218 | 186 | # --- |
| 187 | +# (let zipped [[1 5] [2 6] [3 7] [4 8]]) |
| 188 | +# (print (list:unzip zipped)) # [[1 2 3 4] [5 6 7 8]] |
219 | 189 | # --- |
220 | 190 | # @param _L the list to work on |
221 | 191 | # @author https://github.com/FrenchMasterSword |
|
234 | 204 | [_list1 _list2] |
235 | 205 | })) |
236 | 206 |
|
237 | | -(import "Math.ark") # needed for (math:min a b) |
238 | | - |
239 | 207 | ### |
240 | 208 | # @meta List |
241 | | -# @brief |
242 | | -# @details |
| 209 | +# @brief Zip two lists into one: [1 2 3 4] and [5 6 7 8] will give [[1 5] [2 6] [3 7] [4 8]] |
| 210 | +# @details The original lists are left unmodified. |
243 | 211 | # --- |
| 212 | +# (let a [1 2 3 4]) |
| 213 | +# (let b [5 6 7 8]) |
| 214 | +# (print (list:zip a b)) # [[1 5] [2 6] [3 7] [4 8]] |
244 | 215 | # --- |
245 | | -# @param _L the list to work on |
| 216 | +# @param _a the first list to work on |
| 217 | +# @param _b the second list to work on |
246 | 218 | # @author https://github.com/FrenchMasterSword |
247 | 219 | ### |
248 | 220 | (let list:zip (fun (_a _b) { |
|
0 commit comments