Skip to content

Instantly share code, notes, and snippets.

@allison-casey
Created January 27, 2020 01:17
Show Gist options
  • Save allison-casey/17279ed797256113c5a6e97aaadd933b to your computer and use it in GitHub Desktop.
Save allison-casey/17279ed797256113c5a6e97aaadd933b to your computer and use it in GitHub Desktop.
;; stdlib
(defglobal odd? [x] (!= (% x 2) 0))
(defglobal even? [x] (= (% x 2) 0))
(defglobal list? [x] (= (typeName x) "ARRAY"))
(defglobal string? [x] (= (typeName x) "STRING"))
(defglobal number? [x] (= (typeName x) "SCALAR"))
(defglobal bool? [x] (= (typeName x) "BOOL"))
(defglobal int? [x] (= (floor x) x))
(defglobal pos? [x] (> x 0))
(defglobal neg? [x] (< x 0))
(defglobal zero? [x] (= x 0))
(defglobal true? [x] (= x true))
(defglobal false? [x] (= x false))
(defglobal inc [x] (+ x 1))
(defglobal dec [x] (- x 1))
(defglobal first [seq] (select seq 0))
(defglobal last [seq] (select seq (dec (count seq))))
(defglobal print [x] (hint (str x)))
(defglobal rand-int [n] (floor (random n)))
(defglobal reduce [f initial seq]
(setv accum initial)
(for [curr seq]
(reset! accum (f accum curr)))
accum)
(defglobal map [f seq]
(setv ret [])
(for [x seq]
(pushBack ret (f x)))
ret)
(defglobal filter [pred seq]
(setv ret [])
(for [x seq]
(if (pred x) (pushBack ret x)))
ret)
(defglobal rest [seq]
(setv ret [])
(for [x 1 (count seq)]
(pushBack ret (select seq x)))
ret)
(defglobal dropn [seq n]
(setv ret [])
(for [i n (count seq)]
(pushBack ret (select seq i)))
ret)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment