diff --git a/examples/closures.c b/examples/closures.c index abf83349..6e8e22df 100644 --- a/examples/closures.c +++ b/examples/closures.c @@ -17,14 +17,12 @@ typedef struct Closure__Unit_Int { void *env; } Closure__Unit_Int; +// The body of the lambda, but lifted to its own function. Takes the environment as a first argument. int lifted_lambda_main_0(Env_main_0 *env) { - return 1 + env->x; -} - -int CALL_CLOSURE__Unit_Int__Int(Closure__Unit_Int *closure) { - return closure->callback(closure->env); + return 1 + env->x; // simplified for readability } +// Deleter for this particular closure void delete_Closure__Unit_Int__Int(Closure__Unit_Int closure) { free(closure.env); } @@ -40,7 +38,7 @@ int main() { .env = env_0 }; // call f - int _1 = CALL_CLOSURE__Unit_Int__Int(&f); + int _1 = f->callback(f->env); // delete f delete_Closure__Unit_Int__Int(f); return _1; diff --git a/examples/lambdas.carp b/examples/lambdas.carp index 54b4f559..8c6593ae 100644 --- a/examples/lambdas.carp +++ b/examples/lambdas.carp @@ -9,6 +9,47 @@ ;; f (fn [] x)] ; Lambda takes ownership of the string ;; f)) + +;; g : (λ [a] Int) +(defn g [f] + (f 5)) + +(defn h1 [] + (g inc)) + +(defn h2 [y] + (g (fn [x] (+ x y)))) + + (defn-do main [] (Debug.assert-balanced (println* &(example1 "!")))) + + +(defn blaha [f] + (Int.+ 1 (f 5))) + +(defn main [x] + (map blaha [inc dec (fn [_] x)])) + +;; /* void blaha(void *f) { */ +;; /* int _0; */ +;; /* if is_closure(f) { */ +;; /* _0 = call(f, 5); */ +;; /* f.delete(f); */ +;; /* } else { */ +;; /* _0 = f(5); */ +;; /* } */ +;; /* return (1 + _0); */ +;; /* } */ + + +(defn call-on-me [f] + (f)) + +(def a (fn [] 123)) +(defn b [] 666) + +(defn-do main [] + (a) + (b))