From 530264742af4a29c4674b55d67e2b121a409b8c0 Mon Sep 17 00:00:00 2001 From: Quentin Leonetti Date: Sun, 14 Jul 2019 07:40:20 +0200 Subject: [PATCH] adding dejong example and reduce --- desktop/sources/scripts/library.js | 4 +++ examples/dejong.lisp | 39 +++++++++++++++++------------- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/desktop/sources/scripts/library.js b/desktop/sources/scripts/library.js index 29978d0..d75b601 100644 --- a/desktop/sources/scripts/library.js +++ b/desktop/sources/scripts/library.js @@ -70,6 +70,10 @@ function Library (ronin) { return arr.filter(fn) } + this.reduce = (fn, arr, acc = 0) => { + return arr.reduce(fn, acc) + } + this.first = (arr) => { return arr[0] } diff --git a/examples/dejong.lisp b/examples/dejong.lisp index 9b7bdd6..31317d4 100644 --- a/examples/dejong.lisp +++ b/examples/dejong.lisp @@ -1,22 +1,27 @@ ( (clear) (def point (lambda (x y color) (stroke (circle x y 1) 1 color))) - (point 50 50 "red") - (point 50 150 "red") - (def dejong (lambda (i x y a b c d) - (if (gt i 0) - ( - (point - (add 200 (mul 50 x)) - (add 200 (mul 50 y)) - "red") - (dejong - (sub i 1) - (add (sin (mul a y)) (mul x (cos (mul b x)))) - (add (mul x (sin (mul x c))) (cos (mul d y))) - a b c d) - ) - (echo "done")) + (def _dejong (lambda (x y a b c d) + (rest ((point + (add 200 (mul 50 x)) + (add 200 (mul 50 y)) + "red") + (add (sin (mul a y)) (mul x (cos (mul b x)))) + (add (mul x (sin (mul x c))) (cos (mul d y))) + )) )) - (dejong 800 40 40 1.4 -2.3 2.4 -2.1) + + + (def dejong (lambda (r a b c d) + (reduce + (lambda (acc val) + (first ( + (_dejong (first acc) (last acc) a b c d) + ))) + (range 0 r) + (2 1) + ) + )) + + (dejong 4000 1.4 -2.3 2.4 -2.1) )