Carp/examples/basics.carp

194 lines
4.5 KiB
Plaintext
Raw Normal View History

2017-09-06 11:05:19 +03:00
(use Int)
(use Double)
(use Float)
(use Array)
(use IO)
2017-06-26 12:15:03 +03:00
(defn fib [n]
(if (< n 2)
1
(+ (fib (dec (dec n)))
(fib (dec n)))))
(defmodule Things
(defn inside [s]
(let [msg (String.append s (String.copy "!"))]
2017-06-26 12:15:03 +03:00
(println (ref msg))))
(defn call []
(inside (String.copy "Hello"))))
2017-06-26 12:15:03 +03:00
(defn use-doubles []
(println (ref (str (Double.toInt (Double.+ 2.0 3.0))))))
(deftype Person
[name String
age Int])
(defn use-person []
(let [me (Person.init (String.copy "Erik") 30)]
2017-06-26 12:15:03 +03:00
(println (Person.name (ref me)))))
(defn heap-allocations []
(let [friend (Person.new (String.copy "Oscar") 30)]
2017-06-26 12:15:03 +03:00
()))
(defn refer-up []
(let [a 10
b (+ a 5)
c (* a b)]
c))
(deftype Thing
[val Int])
(defn set-stuff-in-array []
(let [xs [0 1 2 3 4 5 6 7 8 9]]
2017-06-26 12:15:03 +03:00
(do
(aset! &xs 4 666)
2017-06-26 12:15:03 +03:00
(println (ref (str (ref xs)))))))
(defn more-array []
(let [xs [1 2 3 4]
xs2 (pop-back xs)]
(do
(println (refstr &(push-back xs2 500))))))
2017-06-26 12:15:03 +03:00
(defn using-the-form-to-specialize [x y]
(+ x (the Double y)))
(defn flip []
(random-between 0 2))
(defn macrooo []
(let [msg "Print!"]
(cond
(< 10 1) (println "Don't print!")
(> 10 1) (println msg)
(println "Don't print!"))))
2017-06-26 12:15:03 +03:00
(defn macrooo2 []
(for [x 1 3]
(for [y 10 100 20]
(println (ref (str (* x y)))))))
2017-06-26 12:15:03 +03:00
(deftype A [s String])
2017-09-06 11:05:19 +03:00
(use A)
2017-06-26 12:15:03 +03:00
(deftype Peep [x Int
y String
z A])
2017-09-06 11:05:19 +03:00
(use Peep)
2017-06-26 12:15:03 +03:00
(defn calling-delete []
(let [plupp (Peep.init 10 (String.copy "PLUPP") (A.init (String.copy "w00t")))
poop [(Peep.init 10 (String.copy "hej") (A.init (String.copy "aha")))]
strings [(String.copy "a") (String.copy "b") (String.copy "c")]]
2017-06-26 12:15:03 +03:00
(do
(delete plupp)
(delete poop)
(Array.delete strings))))
2017-06-26 12:15:03 +03:00
(defn updating []
(let [p1 (Peep.init 9999 (String.copy "jaha") (A.init (String.copy "mmm")))
2017-06-26 12:15:03 +03:00
p2 (Peep.update-x p1 inc)]
(println (ref (str (Peep.x (ref p2)))))))
(defn character []
(println (ref (Char.str \#))))
(defn negative-numbers []
(let [x -10.0 y -20.0f z -30]
(* (*(toInt x) (toInt y)) z)))
2017-06-29 17:58:27 +03:00
(defn square [x]
(Int.* x x))
(defn endofunctor-mapping []
(let [stuff [1 2 3 4 5]
2017-06-30 15:21:31 +03:00
after (map square stuff)]
2017-06-29 18:05:34 +03:00
(println (refstr &after))))
2017-06-29 17:58:27 +03:00
(defn square [x]
(Int.* x x))
(defn even? [x]
(= (Int.mod x 2) 0))
(defn map-filter-reduce-stuff []
(let [stuff [3 5 8 9 10]
after (reduce + 0 (transform square (filter even? stuff)))]
(println (refstr after))))
2017-07-06 14:29:13 +03:00
(defn get-last-string [xs] ;; Should be generic preferably...
(let [i (dec (Array.count &xs))]
(String.copy (Array.nth &xs i))))
(defn print-last-string []
(println &(get-last-string [(String.copy "NO") (String.copy "NO") (String.copy "YES")])))
2017-10-10 14:20:59 +03:00
(defn exclaim [x] (String.append x @"!"))
(deftype Simple [])
(deftype Complex [x Int f Float d Double s String c Char])
(defn print-structs []
(let [s (Simple.init)
c (Complex.init 12345 3.14f 99.99 @"yo" \x)]
(do
(println (ref (Simple.str &s)))
(println (ref (Complex.str &c))))))
(deftype Thing [x Int y Bool])
(defn turn-any-array-into-string []
(let [a [(Thing.init 10 false) (Thing.init 20 true)]
b [1 2 3 4 5]
c (the (Array Bool) [])
d [[4.3f 2.1f] [0.1f 0.2f 0.3f]]]
(do
(println &(Array.str &a))
(println &(Array.str &b))
(println &(Array.str &c))
(println &(Array.str &d))
)))
(deftype Inner [])
(deftype Outer [as (Array Inner)])
(defn convert-struct-with-array-member-to-string []
(let [b (Outer.init [(Inner.init) (Inner.init) (Inner.init)])]
(println (ref (Outer.str &b)))))
2017-06-26 12:15:03 +03:00
(defn main []
(do (Things.call)
(use-doubles)
(println (ref (str (fib 10))))
(use-person)
(heap-allocations)
(println (ref (str (refer-up))))
(println (ref (str (ref [10 20 30 40 50]))))
;;(println (ref (str (ref (map Thing.val [(Thing.init 100) (Thing.init 200)])))))
(println (ref (str (Int.mod 30 7))))
(set-stuff-in-array)
(more-array)
(macrooo)
(macrooo2)
(seed (System.time))
(println (ref (str (ref (repeat 10 flip)))))
(calling-delete)
(updating)
(character)
2017-06-29 17:58:27 +03:00
(println (ref (str (negative-numbers))))
(endofunctor-mapping)
2017-07-06 14:29:13 +03:00
(map-filter-reduce-stuff)
(print-last-string)
2017-10-10 14:20:59 +03:00
(let [stuff (replicate 3 "Yo")]
(println &(Array.str &(map exclaim stuff))))
(print-structs)
(turn-any-array-into-string)
(convert-struct-with-array-member-to-string)
2017-07-06 14:29:13 +03:00
))
2017-06-26 12:15:03 +03:00
(build)
(run)
2017-09-08 13:24:57 +03:00
(quit)