Skip to content

Instantly share code, notes, and snippets.

@osfameron
Created November 16, 2020 09:25
Show Gist options
  • Save osfameron/203318c643e9d1dc380767edb402fdd8 to your computer and use it in GitHub Desktop.
Save osfameron/203318c643e9d1dc380767edb402fdd8 to your computer and use it in GitHub Desktop.
testing Carp
https://blog.veitheller.de/Carp.html
(def x 1)
(+ x 1) ;; ???
x
(IO.println &(Int.str (+ x 10)))
(defn id [a] a)
(id x)
(deftype Point [x Int, y Int])
Point
(Point.init 1 2)
(Point 1 2) ;; ???
(def p (Point.init 1 2))
p
(id p) ;; ???
(id @p) ;; ???
(id &p)
&p
(Point.x &p)
(Point.set-x p 3)
(Point.set-x @p 3)
(Point.set-x &p 3)
(Point.set-x &@p 3)
(Point.set-x @&p 3) ;; !!!!
&p
(Point.update-x @&p &dec)
(=> p
(Point.update-x &dec)) ;; huh. macro expansion
(Point.update-x p (ref dec)) ;; nope
(=> p (ref) (copy) (Point.update-x &dec))
(Point.update-x (copy (ref p)) (ref dec))
(id (=> p (ref) (copy) (Point.update-x &dec)))
(id (=> p)
(ref) (copy)
(Point.update-x &dec)
(ref) (copy)
(Point.update-y &dec))
;; presumably could write a macro that automatically does this...
(Point.prn &(=> p
(ref) (copy)
(Point.update-x &dec)
(ref) (copy)
(Point.update-y &dec)))
(Point.set-x! &p 3)
&p ;; !!!!
(do (Point.set-x! &p 3) &p)
&p
;;;; special forms
(let [x 1]
(+ x 1))
(if (= 1 2)
(IO.println "Maths is broken")
(IO.println "Maths is fine"))
(foreach [x &[1 2 3]]
(println* "x: " x))
(let [i 0] (while (< i 10) (do (IO.println "print me tenfold!") (set! i (+ i 1)))))
;; macros
(defmacro foo [a] (list '+ 1 a))
(foo 1)
(id (foo 1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment