From 50e13e932d1634a4104e49808d852c6104d8c8b2 Mon Sep 17 00:00:00 2001 From: Quentin Leonetti Date: Thu, 18 Jul 2019 10:54:13 +0200 Subject: [PATCH] update reduce and examples --- desktop/sources/scripts/library.js | 9 +++++++-- examples/animate.lisp | 7 ++++--- examples/dejong.lisp | 10 ++++++---- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/desktop/sources/scripts/library.js b/desktop/sources/scripts/library.js index 1ea70c6..f639907 100644 --- a/desktop/sources/scripts/library.js +++ b/desktop/sources/scripts/library.js @@ -90,8 +90,13 @@ function Library (ronin) { }) } - this.reduce = (fn, arr, acc = 0) => { - return arr.reduce(fn, acc) + this.reduce = async (fn, arr, acc) => { + const length = arr.length + let result = acc === undefined ? subject[0] : acc + for (let i = acc === undefined ? 1 : 0; i < length; i++) { + result = await fn(result, arr[i], i, arr) + } + return result; } this.len = (item) => { diff --git a/examples/animate.lisp b/examples/animate.lisp index b43d696..ed3eadf 100644 --- a/examples/animate.lisp +++ b/examples/animate.lisp @@ -1,12 +1,13 @@ ; animate ( + (clear) (def t (sin (div (time) 100))) - (def pos (add 200 (mul 30 t))) + (def pos (add 200 30 (mul 30 t))) (defn square (a) (rect a a a a)) (stroke (square pos) 1 "red") - (animate) - ;(animate false) to stop animation + ; set false to stop + (animate true) ) \ No newline at end of file diff --git a/examples/dejong.lisp b/examples/dejong.lisp index 9d19d27..c701fa7 100644 --- a/examples/dejong.lisp +++ b/examples/dejong.lisp @@ -1,13 +1,15 @@ ; dejong attractor ( - (clear) - (defn point (x y color) (fill (circle x y 0.1) color)) + (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)) - "rgba(255,0,0,0.5)") + "red") (add (sin (mul a y)) (mul x (cos (mul b x)))) (add (mul x (sin (mul x c))) (cos (mul d y))) )) @@ -23,5 +25,5 @@ (2 1) ) ) - (dejong 128000 1.4 -2.3 2.4 -2.1) + (dejong 1280 1.6 -2.3 2.4 -2.1) )