Merge pull request #138 from blad/fix-map-filter

Fix `map` and `filter` Functions
This commit is contained in:
Devine Lu Linvega 2020-09-18 09:28:07 -07:00 committed by GitHub
commit c7c8f48780
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 20 deletions

View File

@ -1225,17 +1225,11 @@ function Library (client) {
fn(arg, i)
}
}
this.map = (arr, fn) => { // Run a function on each element in a list.
return Promise.all(arr.map(fn)).then(result => { return result })
this.map = (arr, fn) => { // Returns a new list with fn applied to each value.
return arr.map(fn);
}
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 => {
return list.filter((_, index) => {
return result[index]
})
})
this.filter = (arr, fn) => { // Returns a new list, without values evaluated to false by fn.
return arr.filter(fn);
}
this.reduce = (arr, fn, acc) => {
const length = arr.length

View File

@ -434,18 +434,12 @@ function Library (client) {
}
}
this.map = (arr, fn) => { // Run a function on each element in a list.
return Promise.all(arr.map(fn)).then(result => { return result })
this.map = (arr, fn) => { // Returns a new list with fn applied to each value.
return arr.map(fn);
}
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 => {
return list.filter((_, index) => {
return result[index]
})
})
this.filter = (arr, fn) => { // Returns a new list, without values evaluated to false by fn.
return arr.filter(fn);
}
this.reduce = (arr, fn, acc) => {