Merge pull request #139 from blad/add-library-helpers

Add Library Methods: `while`, `push`, `pop`, `cons`, `not` and `object`
This commit is contained in:
Devine Lu Linvega 2020-08-23 18:45:01 -07:00 committed by GitHub
commit 7a1e1eec80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 79 additions and 1 deletions

View File

@ -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)
(test "recursive key selector" ((of (js) "Math" "max") 2 4) 4)

View File

@ -1211,6 +1211,14 @@ function Library (client) {
}
return args[args.length - 1]
}
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]
@ -1240,6 +1248,18 @@ function Library (client) {
this.len = (item) => { // Returns the length of a list.
return item.length
}
this.cons = (arr, ...items) => { // Retruns a new array with the items appended.
return arr.concat(items)
}
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();
}
this.first = (arr) => { // Returns the first item of a list.
return arr[0]
}
@ -1278,6 +1298,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)
}

View File

@ -415,8 +415,18 @@ function Library (client) {
return args[args.length - 1]
}
this.not = (a) => {
return !a
}
// 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]
@ -451,6 +461,21 @@ function Library (client) {
return item.length
}
this.cons = (arr, ...items) => { // Retruns a new array with the items appended.
return arr.concat(items)
}
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();
}
this.first = (arr) => { // Returns the first item of a list.
return arr[0]
}
@ -498,6 +523,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)
}