diff options
author | Kimplul <kimi.h.kuparinen@gmail.com> | 2024-12-06 20:00:42 +0200 |
---|---|---|
committer | Kimplul <kimi.h.kuparinen@gmail.com> | 2024-12-06 20:00:42 +0200 |
commit | 0cef986e6958a0a6a7e94e8256b0039709aed56b (patch) | |
tree | 54d161ff8c2028a2b7819d11b1d74f6b99d01ca3 /examples | |
parent | 1466d2d1032db1e08a4105312f000a1c284d2889 (diff) | |
download | fwd-0cef986e6958a0a6a7e94e8256b0039709aed56b.tar.gz fwd-0cef986e6958a0a6a7e94e8256b0039709aed56b.zip |
add trailing closures
+ Useful for guard statements, not entirely sure about the final syntax
but at least they're possible
Diffstat (limited to 'examples')
-rw-r--r-- | examples/fib.fwd | 3 | ||||
-rw-r--r-- | examples/guard.fwd | 33 | ||||
-rw-r--r-- | examples/uniq.fwd | 15 |
3 files changed, 51 insertions, 0 deletions
diff --git a/examples/fib.fwd b/examples/fib.fwd index 44ced4a..be2b63f 100644 --- a/examples/fib.fwd +++ b/examples/fib.fwd @@ -14,6 +14,9 @@ fib(int n, (int) res) } } +/* 'extern' println */ +fwd_println(int n); + main() { fib(6) => int n; diff --git a/examples/guard.fwd b/examples/guard.fwd new file mode 100644 index 0000000..6af570d --- /dev/null +++ b/examples/guard.fwd @@ -0,0 +1,33 @@ +/* 'any' doesn't actually exist but at the moment I'm not checking types so as + * long as there's some type identifier here the compiler will not complain, + * will eventually have to fix */ +fwd_println(any a); + +guard(bool cond, () err, () ok) +{ + if cond { + err(); + } else { + ok(); + } +} + +try_print_one(int a) +{ + guard(a < 1) => { + fwd_println("smaller than 1"); + } => ; + + guard(a > 1) => { + fwd_println("larger than 1"); + } => ; + + fwd_println(a); +} + +main() +{ + try_print_one(0); + try_print_one(1); + try_print_one(2); +} diff --git a/examples/uniq.fwd b/examples/uniq.fwd index fa7504a..5945d65 100644 --- a/examples/uniq.fwd +++ b/examples/uniq.fwd @@ -1,5 +1,15 @@ /* not entirely sure about the final syntax just yet but something like this */ +/* 'extern' functions (though some should probably be generic?) */ +fwd_getline( + (optional![string]) next); + +fwd_some(optional![string] o, + (string) next); + +fwd_insert(unordered_set![string] set, string line, + (unordered_set![string]) next); + /* at some point I'll probably add in a type system as well, but for now let's * pretend we're static-dynamic (or dynamic at compiletime? dunno) */ readlines(unordered_set![string] set, (unordered_set![string]) next) @@ -19,6 +29,11 @@ readlines(unordered_set![string] set, (unordered_set![string]) next) } } +fwd_foreach(unordered_set![string] set, + (string) callback); + +fwd_println(string s); + main() { /* fwdlib.hpp uses namespace std, not good practice but allows us to do |