Carp/examples/lambdas.carp
2018-08-24 15:20:05 +02:00

56 lines
944 B
Plaintext

;;(defn f [s] (str* s "!"))
(defn example1 [suffix]
(Array.copy-map (fn [s] (str* s suffix)) ;; Can use 'suffix' (of type &String) as long as we're in the example1-scope
&[@"a" @"b" @"c"]))
;; (defn example2 []
;; (let [x @"hello"
;; 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]
(Array.endo-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))