|
12 | 12 | x
|
13 | 13 | (__replace_all_placeholders_with replacement ...xs) })) })
|
14 | 14 |
|
| 15 | +# @brief Chain calls on a given value, for a nicer syntax |
| 16 | +# @param arg value to transform |
| 17 | +# @param ...fns series of functions to apply one by one to `arg` |
| 18 | +# =begin |
| 19 | +# (import std.Macros) |
| 20 | +# (-> "f0" f1) # equivalent to (f1 "f0") |
| 21 | +# (-> "f0" f1 f2 f3) # equivalent to (f3 (f2 (f1 "f0"))) |
| 22 | +# (-> "f0" f1 (apply _ f2) (apply _ f3)) # equivalent to (apply (apply (f1 "f0") f2) f3) |
| 23 | +# =end |
| 24 | +# @author https://github.com/SuperFola |
15 | 25 | (macro -> (arg fn1 ...fns) {
|
16 | 26 | ($if (= "Symbol" ($type fn1))
|
17 | 27 | (macro call (fn1 arg))
|
|
25 | 35 | ($if (> x 1) (__suffix-dup sym (- x 1)))
|
26 | 36 | ($symcat sym x) })
|
27 | 37 |
|
| 38 | +# @brief Create a partial function with prefilled arguments |
| 39 | +# @param func function to make partial |
| 40 | +# @param ...defargs predefined arguments |
| 41 | +# =begin |
| 42 | +# (import std.Macros) |
| 43 | +# (let test_func (fun (a b c) (* a b c))) |
| 44 | +# (let test_func1 (partial test_func 1)) |
| 45 | +# (let test_func2 (partial test_func1 2)) |
| 46 | +# (print (test_func1 2 3)) # 6 |
| 47 | +# (print (test_func2 3)) # 6 |
| 48 | +# =end |
| 49 | +# @author https://github.com/SuperFola |
28 | 50 | (macro partial (func ...defargs) {
|
29 | 51 | (macro bloc (__suffix-dup a (- ($argcount func) (len defargs))))
|
30 | 52 | (fun (bloc) (func ...defargs bloc))
|
|
52 | 74 |
|
53 | 75 | (__replace_placeholders replacements ...xs) })) })
|
54 | 76 |
|
| 77 | +# @brief Create a partial function with prefilled arguments, allowing some arguments to be skipped |
| 78 | +# @param call function to make partial |
| 79 | +# @param ...args predefined arguments |
| 80 | +# =begin |
| 81 | +# (import std.Macros) |
| 82 | +# (let test_func (fun (a b c) (* a b c))) |
| 83 | +# (let test_func3 (partial2 test_func 1 _ 3)) |
| 84 | +# (print (test_func3 2)) # 6 |
| 85 | +# =end |
| 86 | +# @author https://github.com/SuperFola |
55 | 87 | (macro partial2 (call ...args) {
|
56 | 88 | (macro length (__count_placeholders 0 ...args))
|
57 | 89 | (macro arg_bloc (__suffix-dup arg length))
|
58 | 90 | (macro all_args (__replace_placeholders [arg_bloc] ...args))
|
59 | 91 | (fun (arg_bloc) (call all_args)) })
|
60 | 92 |
|
| 93 | +# @brief Create a reversed condition |
| 94 | +# @param cond condition |
| 95 | +# @param body then node (or then/else nodes) |
| 96 | +# =begin |
| 97 | +# (import std.Macros) |
| 98 | +# (unless (canCall? dog) |
| 99 | +# (print "dog can't call") |
| 100 | +# (print "dog can actually call us!")) |
| 101 | +# (unless false |
| 102 | +# (print "this will always be executed")) |
| 103 | +# =end |
| 104 | +# @author https://github.com/SuperFola |
61 | 105 | (macro unless (cond ...body)
|
62 | 106 | (if (not cond) ...body))
|
63 | 107 |
|
| 108 | +# @brief Iterate until the condition is truthy |
| 109 | +# @param cond condition |
| 110 | +# @param body loop body |
| 111 | +# =begin |
| 112 | +# (import std.Macros) |
| 113 | +# (mut data [0]) |
| 114 | +# (until (= 10 (len data)) |
| 115 | +# (append! data (+ 1 (@ data -1)))) |
| 116 | +# (print data) # [0 1 2 3 4 5 6 7 8 9] |
| 117 | +# =end |
| 118 | +# @author https://github.com/SuperFola |
64 | 119 | (macro until (cond body)
|
65 | 120 | (while (not cond) body))
|
0 commit comments