From 99721595c0d16a5cd24b7d2f0012700212b18371 Mon Sep 17 00:00:00 2001 From: Bladymir Tellez Date: Sun, 23 Aug 2020 08:12:43 -0500 Subject: [PATCH 1/5] Add Helper Functions to Library - Add `not` to simplify if expressions where the negation is wanted. - Add `cons`, `push`, `pop` to operate on lists. - Add `object` to make constructing an object simpler. --- scripts/library.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/scripts/library.js b/scripts/library.js index 465c366..a7d4ce0 100644 --- a/scripts/library.js +++ b/scripts/library.js @@ -415,6 +415,10 @@ function Library (client) { return args[args.length - 1] } + this.not = (a) => { + return !a + } + // Arrays this.each = (arr, fn) => { // Run a function for each element in a list. @@ -451,6 +455,18 @@ function Library (client) { return item.length } + this.cons = (arr, item) => { // Retruns a new array with the item. + return array.concat([item]) + } + + this.push = (arr, item) => { // Appends the item item into the existing list. + return array.push(item) + } + + this.pop = (arr) => { // Pop the last item from the list and return the item. + return arr.pop(); + } + this.first = (arr) => { // Returns the first item of a list. return arr[0] } @@ -498,6 +514,14 @@ function Library (client) { }, h) } + this.object = (...entries) => { // Creates an object with provided entries. + const result = {} + for (let i = 0; i < entries.length; i += 2) { + result[entries[i]] = entries[i + 1] + } + return result + } + this.keys = (item) => { // Returns a list of the object's keys return Object.keys(item) } From 74d18a2dff96b8625385a840e2617b5f95bbb002 Mon Sep 17 00:00:00 2001 From: Bladymir Tellez Date: Sun, 23 Aug 2020 08:15:07 -0500 Subject: [PATCH 2/5] Update index.js --- index.html | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/index.html b/index.html index 5a4b8f2..7f69a68 100644 --- a/index.html +++ b/index.html @@ -1211,6 +1211,9 @@ function Library (client) { } return args[args.length - 1] } + this.not = (a) => { + return !a + } this.each = (arr, fn) => { // Run a function for each element in a list. for (let i = 0; i < arr.length; i++) { const arg = arr[i] @@ -1240,6 +1243,15 @@ function Library (client) { this.len = (item) => { // Returns the length of a list. return item.length } + this.cons = (arr, item) => { // Retruns a new array with the item. + return array.concat([item]) + } + this.push = (arr, item) => { // Appends the item item into the existing list. + return array.push(item) + } + this.pop = (arr) => { // Pop the last item from the list and return the item. + return arr.pop(); + } this.first = (arr) => { // Returns the first item of a list. return arr[0] } @@ -1278,6 +1290,13 @@ function Library (client) { return acc[key] }, h) } + this.object = (...entries) => { // Creates an object with provided entries. + const result = {} + for (let i = 0; i < entries.length; i += 2) { + result[entries[i]] = entries[i + 1] + } + return result + } this.keys = (item) => { // Returns a list of the object's keys return Object.keys(item) } From 1b64251a3e544236fcb4eeb39fdf583175817fba Mon Sep 17 00:00:00 2001 From: Bladymir Tellez Date: Sun, 23 Aug 2020 08:25:41 -0500 Subject: [PATCH 3/5] Fix Typos and Smaller Issues --- index.html | 11 +++++++---- scripts/library.js | 11 +++++++---- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/index.html b/index.html index 7f69a68..df1992a 100644 --- a/index.html +++ b/index.html @@ -1243,11 +1243,14 @@ function Library (client) { this.len = (item) => { // Returns the length of a list. return item.length } - this.cons = (arr, item) => { // Retruns a new array with the item. - return array.concat([item]) + this.cons = (arr, ...items) => { // Retruns a new array with the items appended. + return arr.concat(items) } - this.push = (arr, item) => { // Appends the item item into the existing list. - return array.push(item) + this.push = (arr, ...items) => { // Appends the items into the existing list. + for (let i = 0; i < items.length; i++) { + arr.push(items[i]) + } + return arr } this.pop = (arr) => { // Pop the last item from the list and return the item. return arr.pop(); diff --git a/scripts/library.js b/scripts/library.js index a7d4ce0..168c499 100644 --- a/scripts/library.js +++ b/scripts/library.js @@ -455,12 +455,15 @@ function Library (client) { return item.length } - this.cons = (arr, item) => { // Retruns a new array with the item. - return array.concat([item]) + this.cons = (arr, ...items) => { // Retruns a new array with the items appended. + return arr.concat(items) } - this.push = (arr, item) => { // Appends the item item into the existing list. - return array.push(item) + this.push = (arr, ...items) => { // Appends the items into the existing list. + for (let i = 0; i < items.length; i++) { + arr.push(items[i]) + } + return arr } this.pop = (arr) => { // Pop the last item from the list and return the item. From 82cfdf567e1cee52dcd6d633946637809f3f834e Mon Sep 17 00:00:00 2001 From: Bladymir Tellez Date: Sun, 23 Aug 2020 08:41:50 -0500 Subject: [PATCH 4/5] Add New Functions to Benchmark Test Suite --- examples/projects/benchmark.lisp | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/examples/projects/benchmark.lisp b/examples/projects/benchmark.lisp index 5f19776..7588c08 100644 --- a/examples/projects/benchmark.lisp +++ b/examples/projects/benchmark.lisp @@ -23,6 +23,8 @@ (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 "not - true" (not true) false) + (test "not - false" (not false) true) (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") @@ -40,6 +42,22 @@ (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) + (def test-list ()) + (test "push" (push test-list 1 2 3) (1 2 3)) + (test "pop - return value" (pop test-list) 3) + (test "pop - modified list" test-list (1 2)) + + (def test-empty-list ()) + (def test-list-2 (cons test-empty-list 4 5 6)) + (test "cons - new list" test-list-2 (4 5 6)) + (test "cons - unmodified list" test-empty-list ()) + + (def expected-object:x 1) + (def expected-object:y 2) + (def test-object (object "x" 1 "y" 2)) + (test "object" test-object:x expected-object:x) + (test "object" test-object:y expected-object:y) + ; Scope (def aaa 123) @@ -57,4 +75,4 @@ ; Interop (test "interop" ((of (of (js) "Math") "max") 2 4) 4) - (test "recursive key selector" ((of (js) "Math" "max") 2 4) 4) \ No newline at end of file + (test "recursive key selector" ((of (js) "Math" "max") 2 4) 4) From 7e6970b9e4c04c4dd6ddabb50b77b715e7cbaf9b Mon Sep 17 00:00:00 2001 From: Bladymir Tellez Date: Sun, 23 Aug 2020 11:53:07 -0500 Subject: [PATCH 5/5] Implement while --- index.html | 5 +++++ scripts/library.js | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/index.html b/index.html index df1992a..fe16f62 100644 --- a/index.html +++ b/index.html @@ -1214,6 +1214,11 @@ function Library (client) { this.not = (a) => { return !a } + this.while = (fn, action) => { + while (fn()) { + action() + } + } this.each = (arr, fn) => { // Run a function for each element in a list. for (let i = 0; i < arr.length; i++) { const arg = arr[i] diff --git a/scripts/library.js b/scripts/library.js index 168c499..5e0600b 100644 --- a/scripts/library.js +++ b/scripts/library.js @@ -421,6 +421,12 @@ function Library (client) { // Arrays + this.while = (fn, action) => { + while (fn()) { + action() + } + } + this.each = (arr, fn) => { // Run a function for each element in a list. for (let i = 0; i < arr.length; i++) { const arg = arr[i]