diff --git a/README.md b/README.md index 2cf29e0..a7f327c 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,7 @@ Ronin helpers are keywords that facilitates adding coordinates from the canvas i - `(condense pixel q)` Condense the data of pixels. - `(balance pixel q)` Change the color balance of pixels. - `(concat ...items)` Concat multiple strings. +- `(split string char)` Split string at character. - `(add ...args)` Adds values. - `(sub ...args)` Subtracts values. - `(mul ...args)` Multiplies values. @@ -102,9 +103,10 @@ Ronin helpers are keywords that facilitates adding coordinates from the canvas i - `(eq a b)` Returns true if a is equal to b, else false. - `(and a b ...rest)` Returns true if all conditions are true. - `(or a b ...rest)` Returns true if at least one condition is true. -- `(map fn arr)` -- `(filter fn arr)` -- `(reduce fn arr acc)` +- `(each arr fn)` Run a function for each element in a list. +- `(map arr fn)` Run a function on each element in a list. +- `(filter arr fn)` Remove from list, when function returns false. +- `(reduce arr fn acc)` - `(len item)` Returns the length of a list. - `(first arr)` Returns the first item of a list. - `(last arr)` Returns the last @@ -124,9 +126,8 @@ Ronin helpers are keywords that facilitates adding coordinates from the canvas i - `(dirpath ~path)` Returns the path of a directory. - `(filepath ~path)` Returns the path of a file. - `(exit ~force)` Exits Ronin. -- `(echo ...args)` -- `(table arg)` -- `(debug arg)` +- `(echo ...args)` Print arguments to interface. +- `(debug arg)` Print arguments to console. - `(time ~rate)` Returns timestamp in milliseconds. - `(js)` Javascript interop. - `(on event f)` Triggers on event. diff --git a/desktop/sources/scripts/library.js b/desktop/sources/scripts/library.js index 0c83082..5b43e57 100644 --- a/desktop/sources/scripts/library.js +++ b/desktop/sources/scripts/library.js @@ -215,6 +215,10 @@ function Library (ronin) { return items.reduce((acc, item) => { return `${acc}${item}` }, '') } + this.split = function (string, char) { // Split string at character. + return string.split(char) + } + // Math this.add = (...args) => { // Adds values. @@ -320,14 +324,21 @@ function Library (ronin) { // Arrays - this.map = async (fn, arr) => { + this.each = async (arr, fn) => { // Run a function for each element in a list. for (let i = 0; i < arr.length; i++) { const arg = arr[i] await fn(arg) } } - this.filter = (fn, arr) => { + this.map = async (arr, fn) => { // Run a function on each element in a list. + for (let i = 0; i < arr.length; i++) { + const arg = arr[i] + arr[i] = await fn(arg) + } + } + + this.filter = (arr, fn) => { // Remove from list, when function returns false. const list = Array.from(arr) return Promise.all(list.map((element, index) => fn(element, index, list))) .then(result => { @@ -337,7 +348,7 @@ function Library (ronin) { }) } - this.reduce = async (fn, arr, acc) => { + this.reduce = async (arr, fn, acc) => { const length = arr.length let result = acc === undefined ? subject[0] : acc for (let i = acc === undefined ? 1 : 0; i < length; i++) { @@ -463,17 +474,12 @@ function Library (ronin) { ronin.source.quit(force) } - this.echo = (...args) => { + this.echo = (...args) => { // Print arguments to interface. ronin.log(args) return args } - this.table = (arg) => { - console.table(arg) - return arg - } - - this.debug = (arg) => { + this.debug = (arg) => { // Print arguments to console. console.log(arg) return arg } diff --git a/examples/batch/folder.lisp b/examples/batch/folder.lisp new file mode 100644 index 0000000..ddf89cf --- /dev/null +++ b/examples/batch/folder.lisp @@ -0,0 +1,24 @@ +; open every file in a folder. +(defn filter-jpg + (file-name) + (eq + (last + (split file-name ".")) "jpg")) +; +(def images + (filter + (dir) filter-jpg)) +; +(debug + (concat "Found: " + (len images))) +; +(defn image-operation + (file-name) + ( + (def file-path + (concat + (dirpath) "/" file-name)) + (open file-path))) +; +(each images image-operation) \ No newline at end of file diff --git a/examples/benchmark/general.lisp b/examples/benchmark/general.lisp index dc232c1..e240340 100644 --- a/examples/benchmark/general.lisp +++ b/examples/benchmark/general.lisp @@ -35,9 +35,9 @@ (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) + (test "map" (map (1 2 3) (λ (a) (add 1 a))) (2 3 4)) + (test "filter" (filter (2 3 4 5 6)) (λ (a) (eq 0 (mod a 2))) (2 4 6)) + (test "reduce" (reduce (1 2 3) (λ (acc val) (add acc val)) 4) 10) ; Scope diff --git a/examples/demo/dejong.lisp b/examples/demo/dejong.lisp index d373008..994e288 100644 --- a/examples/demo/dejong.lisp +++ b/examples/demo/dejong.lisp @@ -16,11 +16,11 @@ (defn dejong (r a b c d) (reduce + (range 0 r) (λ (acc val) (first ( (_dejong (first acc) (last acc) a b c d) ))) - (range 0 r) (2 1) ) )