|
48 | 48 | # @author https://github.com/SuperFola
|
49 | 49 | (let fill (fun (_val _count) (builtin__list:fill _val _count)))
|
50 | 50 |
|
| 51 | +# @brief Function to call the `len` operator on a list |
| 52 | +# @param _L list to get the size of |
| 53 | +# =begin |
| 54 | +# (print (list:size [1 2 3 4])) # 4 |
| 55 | +# =end |
| 56 | +# @author https://github.com/SuperFola |
| 57 | +(let size (fun (_L) (len _L))) |
| 58 | + |
51 | 59 | # @brief Modify a given list and return a new one
|
52 | 60 | # @details The original list is not modified
|
53 | 61 | # @param list the list to modify
|
|
62 | 70 | # @brief Iterate over a given list and run a given function on every element.
|
63 | 71 | # @param _L the list to iterate over
|
64 | 72 | # @param _func the function to call on each element
|
65 |
| -# @details The original list is left unmodified. |
| 73 | +# @details The original list is not modified. |
66 | 74 | # =begin
|
67 | 75 | # (import std.List)
|
68 | 76 | # (let collection [1 2 5 12])
|
|
81 | 89 |
|
82 | 90 | # @brief Iterate over a given list and multiply all the elements with the others.
|
83 | 91 | # @param _L the list to iterate over
|
84 |
| -# @details The original list is left unmodified. |
| 92 | +# @details The original list is not modified. |
85 | 93 | # =begin
|
86 | 94 | # (import std.List)
|
87 | 95 | # (let collection [1 2 5 12])
|
|
99 | 107 |
|
100 | 108 | # @brief Iterate over a given list and sum all the elements.
|
101 | 109 | # @param _L the list to iterate over
|
102 |
| -# @details The original list is left unmodified. |
| 110 | +# @details The original list is not modified. |
103 | 111 | # =begin
|
104 | 112 | # (import std.List)
|
105 | 113 | # (let collection [1 2 5 12])
|
|
115 | 123 | (set _index (+ 1 _index)) })
|
116 | 124 | _output }))
|
117 | 125 |
|
| 126 | +# @brief Find the minimum in a list of numbers |
| 127 | +# @param _L list of numbers |
| 128 | +# @details The original list is not modified. |
| 129 | +# =begin |
| 130 | +# (let value (list:min [0 1 2 3 5 8])) # 0 |
| 131 | +# =end |
| 132 | +# @author https://github.com/SuperFola |
| 133 | +(let min (fun (_L) { |
| 134 | + (mut _index 0) |
| 135 | + (mut _output nil) |
| 136 | + |
| 137 | + (while (< _index (len _L)) { |
| 138 | + (if (or (nil? _output) (< (@ _L _index) _output)) |
| 139 | + (set _output (@ _L _index))) |
| 140 | + (set _index (+ 1 _index)) }) |
| 141 | + _output })) |
| 142 | + |
| 143 | +# @brief Find the maximum in a list of numbers |
| 144 | +# @param _L list of numbers |
| 145 | +# @details The original list is not modified. |
| 146 | +# =begin |
| 147 | +# (let value (list:min [0 1 2 3 5 8])) # 8 |
| 148 | +# =end |
| 149 | +# @author https://github.com/SuperFola |
| 150 | +(let max (fun (_L) { |
| 151 | + (mut _index 0) |
| 152 | + (mut _output nil) |
| 153 | + |
| 154 | + (while (< _index (len _L)) { |
| 155 | + (if (or (nil? _output) (> (@ _L _index) _output)) |
| 156 | + (set _output (@ _L _index))) |
| 157 | + (set _index (+ 1 _index)) }) |
| 158 | + _output })) |
| 159 | + |
118 | 160 | (import std.Math :min :max)
|
119 | 161 |
|
120 | 162 | # @brief Drop the first n elements of a list
|
121 | 163 | # @param _L the list to work on
|
122 | 164 | # @param _n the number of elements to drop
|
123 |
| -# @details The original list is left unmodified. |
| 165 | +# @details The original list is not modified. |
124 | 166 | # =begin
|
125 | 167 | # (let cool-stuff [1 2 3 4 5 6 7 8 9])
|
126 | 168 | # (print (drop cool-stuff 4)) # [5 6 7 8 9]
|
|
143 | 185 | # @brief Drop the first elements of a list, while they match a given predicate
|
144 | 186 | # @param _L the list to work on
|
145 | 187 | # @param _f the predicate
|
146 |
| -# @details The original list is left unmodified. |
| 188 | +# @details The original list is not modified. |
147 | 189 | # =begin
|
148 | 190 | # (let cool-stuff [1 2 3 4 5 6 7 8 9])
|
149 | 191 | # (print (dropWhile cool-stuff (fun (a) (< a 4)))) # [4 5 6 7 8 9]
|
|
164 | 206 | # @brief Keep elements in a given list if they follow a predicate
|
165 | 207 | # @param _L the list to work on
|
166 | 208 | # @param _f the predicate
|
167 |
| -# @details The original list is left unmodified. |
| 209 | +# @details The original list is not modified. |
168 | 210 | # =begin
|
169 | 211 | # (import std.Math)
|
170 | 212 | # (print (filter [1 2 3 4 5 6 7 8 9] math:even)) # [2 4 6 8]
|
|
182 | 224 | # @brief Apply a given function to each element of a list
|
183 | 225 | # @param _L the list to work on
|
184 | 226 | # @param _f the function to apply to each element
|
185 |
| -# @details The original list is left unmodified. |
| 227 | +# @details The original list is not modified. |
186 | 228 | # =begin
|
187 | 229 | # (print (map [1 2 3 4 5 6 7 8 9] (fun (e) (* e e)))) # [1 4 9 25 36 49 64 81]
|
188 | 230 | # =end
|
|
199 | 241 | # @brief Apply a function to the elements of a list to reduce it
|
200 | 242 | # @param _L the list to work on
|
201 | 243 | # @param _f the function to apply
|
202 |
| -# @details The original list is left unmodified. |
| 244 | +# @details The original list is not modified. |
203 | 245 | # =begin
|
204 | 246 | # (let cool [1 2 3 4 5 6 7 8 9])
|
205 | 247 | # (print (reduce cool (fun (a b) (+ a b)))) # 45
|
|
216 | 258 |
|
217 | 259 | # @brief Flatten a list
|
218 | 260 | # @param _L the list to work on
|
219 |
| -# @details The original list is left unmodified. |
| 261 | +# @details The original list is not modified. |
220 | 262 | # =begin
|
221 | 263 | # (let cool [[1 2 3] [4] 5 6 [7 8] 9])
|
222 | 264 | # (print (flatten cool)) # [1 2 3 4 5 6 7 8 9]
|
|
238 | 280 | # @brief Apply a given function to each element of a list and then flatten it
|
239 | 281 | # @param _L the list to work on
|
240 | 282 | # @param _f the function to apply to each element
|
241 |
| -# @details The original list is left unmodified. |
| 283 | +# @details The original list is not modified. |
242 | 284 | # =begin
|
243 | 285 | # (let cool [1 2 3 4])
|
244 | 286 | # (print (flatMap cool (fun (a) [a a]))) # [1 1 2 2 3 3 4 4]
|
|
260 | 302 | # @brief Take the first n elements of
|
261 | 303 | # @param _L the list to work on
|
262 | 304 | # @param _n the number of elements to take
|
263 |
| -# @details The original list is left unmodified. |
| 305 | +# @details The original list is not modified. |
264 | 306 | # =begin
|
265 | 307 | # (print (take [1 2 3 4 5 6 7 8 9] 4)) # [1 2 3 4]
|
266 | 308 | # =end
|
|
278 | 320 | # @brief Take the first n elements of a list, given a predicate
|
279 | 321 | # @param _L the list to work on
|
280 | 322 | # @param _f the predicate
|
281 |
| -# @details The original list is left unmodified. |
| 323 | +# @details The original list is not modified. |
282 | 324 | # =begin
|
283 | 325 | # (print (takeWhile [1 2 3 4 5 6 7 8 9 10] (fun (a) (< a 4)))) # [1 2 3]
|
284 | 326 | # =end
|
|
299 | 341 | # @brief Partition a list in two, given a predicate
|
300 | 342 | # @param _L the list to work on
|
301 | 343 | # @param _f the predicate, accepting the value and its index
|
302 |
| -# @details The original list is left unmodified. |
| 344 | +# @details The original list is not modified. |
303 | 345 | # =begin
|
304 | 346 | # (let a [1 2 3])
|
305 | 347 | # (print (list:partition a (fun (c i) (= 0 (mod c 2))))) # [[2] [1 3]]
|
|
321 | 363 |
|
322 | 364 | # @brief Unzip a list of [[a b] [c d]...] into [[a c ...] [b d ...]]
|
323 | 365 | # @param _L the list to work on
|
324 |
| -# @details The original list is left unmodified. |
| 366 | +# @details The original list is not modified. |
325 | 367 | # =begin
|
326 | 368 | # (let zipped [[1 5] [2 6] [3 7] [4 8]])
|
327 | 369 | # (print (unzip zipped)) # [[1 2 3 4] [5 6 7 8]]
|
|
343 | 385 | # @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]]
|
344 | 386 | # @param _a the first list to work on
|
345 | 387 | # @param _b the second list to work on
|
346 |
| -# @details The original lists are left unmodified. |
| 388 | +# @details The original lists are not modified. |
347 | 389 | # =begin
|
348 | 390 | # (let a [1 2 3 4])
|
349 | 391 | # (let b [5 6 7 8])
|
|
362 | 404 |
|
363 | 405 | # @brief Zip a list elements with their index. [5 6 7 8] will give [[0 5] [1 6] [2 7] [3 8]]
|
364 | 406 | # @param _L the list to iterate over
|
365 |
| -# @details The original list is left unmodified. |
| 407 | +# @details The original list is not modified. |
366 | 408 | # =begin
|
367 | 409 | # (let a [5 6 7 8])
|
368 | 410 | # (print (zipWithIndex a)) # [[0 5] [1 6] [2 7] [3 8]]
|
|
381 | 423 | # @param _L the list to work on
|
382 | 424 | # @param _init an init value
|
383 | 425 | # @param _f a function to apply to the list
|
384 |
| -# @details The original list is left unmodified. |
| 426 | +# @details The original list is not modified. |
385 | 427 | # =begin
|
386 | 428 | # (let a [1 2 3 4])
|
387 | 429 | # (print (foldLeft a 0 (fun (a b) (+ a b)))) # 10
|
|
512 | 554 | _output }))
|
513 | 555 |
|
514 | 556 | # @brief Insert an element (or expand a list) at a given position inside a list
|
515 |
| -# @details Original list is left unmodified |
| 557 | +# @details The original list is not modified |
516 | 558 | # @param _L list to insert element(s) in
|
517 | 559 | # @param _index where to insert
|
518 | 560 | # @param _value value to insert
|
|
0 commit comments