Moving examples

This commit is contained in:
Devine Lu Linvega
2019-07-22 15:36:16 +09:00
parent a3ae0177e2
commit 39b2e558bd
28 changed files with 34 additions and 20 deletions

View File

@@ -0,0 +1,26 @@
(echo (map '(add %1 1) (1 2 3)))
(echo (first (1 2 3)))
(echo (rest (1 2 3)))
(echo
(filter
'(eq 0 (mod %1 2))
(1 2 3 4 5))
)
(clear)
(map
'(stroke (rect (mul a 30) 20 50 50)
1 "red")
(range 0 20 5))
(map
'(stroke
(rect
(mul %1 10)
(add 250 (mul (sin %1) 250))
(mul 4 %1)
(add 20 (mul (cos %1) 50))) 1 "red")
(range 0 200 5))

View File

@@ -0,0 +1,13 @@
; basics
; define a variable
(def a 25)
(echo a)
; define a function
(defn add-two (a) (add 2 a))
(echo (add-two 4))
; use a lambda
(times 5
(λ (a) (echo (concat "time:" a))))

View File

@@ -0,0 +1,68 @@
; benchmark
; Basics
(test "add" (add 8 4 2) 14)
(test "sub" (sub 8 4 2) 2)
(test "mul" (mul 8 4 2) 64)
(test "div" (div 8 4 2) 1)
; Others
(test "mod" (mod 6 4) 2)
(test "clamp" (clamp 12 4 8) 8)
(test "step" (step 12 10) 10)
(test "PI" TWO_PI (mul 2 PI))
; Logic
(test "lt" (lt 3 4) true)
(test "gt" (gt 3 4) false)
(test "and - true" (and 1 2 true 4) 4)
(test "and - false" (and 1 false 2) false)
(test "or - true" (or false false 2 false) 2)
(test "if - branch 1" (if (gt 3 2) ("branch 1") ("branch 2")) "branch 1")
(test "if - branch 2" (if (lt 3 2) ("branch 1") ("branch 2")) "branch 2")
(test "if - no else" (if (lt 3 2) ("branch 1")) ())
; Arrays
(test "first" (first ("a" "b" "c")) "a")
(test "rest" (rest ("a" "b" "c")) ("b" "c"))
(test "last" (last ("a" "b" "c")) "c")
(test "range simple" (range 0 4) (0 1 2 3 4))
(test "range with step" (range 0 4 2) (0 2 4))
(test "range with negative step" (range 0 -4 -1) (0 -1 -2 -3 -4))
(test "map" (map (λ (a) (add 1 a)) (1 2 3)) (2 3 4))
(test "filter" (filter (λ (a) (eq 0 (mod a 2))) (2 3 4 5 6)) (2 4 6))
(test "reduce" (reduce (λ (acc val) (add acc val)) (1 2 3) 4) 10)
; Scope
(def aaa 123)
(def addOne (λ (a) (add a 1)))
(test "def - value" aaa 123)
(test "def - func" (addOne 4) 5)
(defn addTwo (a) (add 2 a))
(test "defn" (addTwo 4) 6)
(defn mulTwo "multiplies by two" (a) (mul 2 a))
(test "docstring" (mulTwo 4) 8)
; Generics
(test "concat" (concat 1 4 "-" (add 3 4) ".jpg") "14-7.jpg")
; Interop
(test "interop" ((of (of (js) "Math") "max") 2 4) 4)
(test "recursive key selector" ((of (js) "Math" "max") 2 4) 4)
; fs
; filesystem
(echo (filepath))
(echo (dirpath))
(echo (file))
(echo (dir))

View File

@@ -0,0 +1,5 @@
; crop
(clear)
(open "../static/crystal.jpg")
(crop (rect 100 100 400 400))

View File

@@ -0,0 +1,33 @@
; dejong attractor
(clear)
(defn point (x y color)
(fill (rect x y 1 1) color))
(defn _dejong (x y a b c d)
(rest ((point
(add 300 (mul 100 x))
(add 400 (mul 100 y))
"red")
(add (sin (mul a y)) (mul x (cos (mul b x))))
(add (mul x (sin (mul x c))) (cos (mul d y)))
))
)
(defn dejong (r a b c d)
(reduce
(λ (acc val)
(first (
(_dejong (first acc) (last acc) a b c d)
)))
(range 0 r)
(2 1)
)
)
(benchmark
'(dejong 12800
(random -2 2)
(random -2 2)
(random -2 2)
(random -2 2)
))

View File

@@ -0,0 +1,18 @@
; Glitch
(clear)
(defn glitch
(rec)
(if (gt rec 1)
(
(translate
(rect (random 400) (random 400) (random 10) (random 10))
(pos (random 400) (random 400)))
(glitch (sub rec 1)))))
(import
"../static/crystal.jpg"
(rect 0 0 400 400))
(glitch 500)

View File

@@ -0,0 +1,14 @@
; gradients
(clear)
(fill
(svg "M405,15 L405,15 L150,150 L195,90 L240,135 L120,195 L75,90 L135,165 L120,225 L90,240 L60,210 L90,150 L255,180 L285,180 L285,165 ")
(gradient
(0 -50 600 175)
("red" "orange" "blue" "green")))
(stroke
(svg "M255,60 L255,60 L135,180 L75,60 L195,210 L120,225 L105,225 L165,255 L225,195 L255,135 L285,150") 1
(gradient
(50 0 180 0)
("black" "white" "blue" "green")))

View File

@@ -0,0 +1,56 @@
; guides
(clear)
(stroke
(frame) 1 "red")
(stroke
(line
(pos 0 0)
(pos
(of
(frame) "w")
(of
(frame) "h"))) 1 "red")
(stroke
(line
(pos
(of
(frame) "w") 0)
(pos 0
(of
(frame) "h"))) 1 "red")
(stroke
(line
(pos
(div
(of
(frame) "w") 2) 0)
(pos
(div
(of
(frame) "w") 2)
(of
(frame) "h"))) 1 "red")
(stroke
(line
(pos 0
(div
(of
(frame) "h") 2))
(pos
(div
(of
(frame) "w") 2)
(of
(frame) "h"))) 1 "#72dec2")
(stroke
(line
(pos
(div
(of
(frame) "w") 2) 0)
(pos
(of
(frame) "w")
(div
(of
(frame) "h") 2))) 1 "#72dec2")

View File

@@ -0,0 +1,5 @@
(clear)
(def a (import
"../static/crystal.jpg"
(rect 0 0 400 400)))
(echo a)

View File

@@ -0,0 +1,3 @@
; include
(include "../examples/recursive.lisp")
(echo line-color)

View File

@@ -0,0 +1,17 @@
; objects
(test "symbols" :a "a")
(def ob {:a "fantastic" :b 2})
((of (js) :console :log) ob)
(echo (of ob :a))
(echo (keys ob))
(echo (values ob))
(set ob :a 4)
(echo (of ob :a))
(echo (map '(add %1 2) (4 5 6))

View File

@@ -0,0 +1,7 @@
; saturate image
(open "../static/crystal.jpg")
(pixels
(frame)
saturation
12)

View File

@@ -0,0 +1,7 @@
; pixels
(clear)
(import "../../PREVIEW.jpg"
(frame))
(pixels
(rect 0 0 500 500) saturation 0.5)

View File

@@ -0,0 +1,14 @@
; filesystem
; get files
(def files
(dir
(dirpath)))
; pick a random file
(def random-index
(floor
(random
(len files))))
; print random file name
(echo
(get files random-index))

View File

@@ -0,0 +1,17 @@
; random
(clear)
(defn place
(rec)
(if
(gt rec 0)
(
(import "../static/crystal.jpg"
(rect
(random 200)
(random 200)
(random 200)
(random 200)))
(place
(sub rec 1)))))
(place 30)

View File

@@ -0,0 +1,16 @@
; recursive
(clear)
(defn rec
(v)
(if
(gt v 0)
(
(stroke
(circle
(mul 5 v)
(mul 5 v)
(mul 5 v)) 1 "red")
(rec
(sub v 5)))))
(rec 100)

View File

@@ -0,0 +1,5 @@
; resize
(clear)
(open "../../PREVIEW.jpg")
(resize 0.5 0.5)

View File

@@ -0,0 +1,24 @@
; Shapes
(clear)
; variables
(def center-w (div (of (frame) "w") 2))
(def center-h (div (of (frame) "h") 2))
(def rad (div (of (frame) "h") 4))
; draw circle
(stroke
(circle center-w center-h rad) 2 "white")
; draw rect
(stroke
(rect
(sub center-w rad) (sub center-h rad) center-h center-h) 2 "white")
; draw line
(stroke
(line
(pos (sub center-w rad) center-h)
(pos (add center-w rad) center-h)))
(stroke (text 10 170 200 "HELL") 2 "pink")

View File

@@ -0,0 +1,30 @@
; animated recusive spiral
; by @local_guru
(clear)
(defn rec
(v)
(if
(gt v 0)
(
(stroke
(circle
(add 375
(mul
(cos
(add
(div v 17)
(div
(time) 2000)))
(div v 2)))
(add 300
(mul
(sin
(div v 11))
(div v 2)))
(div v 2)) 1 "rgba(114,222, 194,0.1)")
(rec
(sub v 0.3)))))
; set false to stop
(animate true)
(rec 300)

View File

@@ -0,0 +1,37 @@
; stars
(clear)
;
(defn draw-spoke
(cx cy r a)
(
(stroke
(line
(pos cx cy)
(circle-pos cx cy r a)) 2 "white")))
;
(defn draw-star
(cx cy r c)
(
(times c
(λ
(i)
(
(draw-spoke cx cy r
(deg-rad
(mul i
(div 360 c)))))))))
; main
(times 100
(λ
()
(
(draw-star
(random 100
(of
(frame) :w))
(random 100
(of
(frame) :h))
(random 10 100)
(floor
(random 3 32))))))

View File

@@ -0,0 +1,8 @@
(clear)
; ronin path
(stroke
(svg "M60,60 L195,60 A45,45 0 0,1 240,105 A45,45 0 0,1 195,150 L60,150 M195,150 A45,45 0 0,1 240,195 L240,240 ") 2 "white")
; outline
(stroke
(svg "M15,15 L15,15 L285,15 L285,285 L15,285 Z") 1 "#555")

View File

@@ -0,0 +1,38 @@
; theme
(clear)
(def col
(λ
(i)
(of
(
(theme "f_high")
(theme "f_med")
(theme "f_low")
(theme "f_inv")
(theme "b_high")
(theme "b_med")
(theme "b_low")
(theme "b_inv"))
(mod i 8))))
(def rec
(λ
(v i)
(if
(gt v 0)
(
(fill
(circle
(add
(div
(of
(frame) "w") 1.6)
(mul 1.5 v))
(mul 10 v)
(mul v
(div v 5)))
(col i))
(rec
(sub v 3)
(add i 1))))))
(rec 40 0)

View File

@@ -0,0 +1,25 @@
(resetTransform)
(clear)
(defn branch
(v)
(if
(gt v 0)
(
(scale 0.95)
(stroke
(line (pos 0 0) (pos 100 100))
10 "white")
(move 100 100)
(pushTransform)
(rotate (div v 50))
(branch (sub v 1))
(popTransform)
(pushTransform)
(rotate (div v -50))
(branch (sub v 1))
(popTransform)
)
)
)
(branch 10)