From 4d0df33005034f1698db46d58dcdc87dcc5a747e Mon Sep 17 00:00:00 2001 From: Bladymir Tellez Date: Sat, 22 Aug 2020 10:50:45 -0500 Subject: [PATCH 1/2] Drop Promises for map and filter --- scripts/library.js | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/scripts/library.js b/scripts/library.js index 465c366..613af65 100644 --- a/scripts/library.js +++ b/scripts/library.js @@ -424,18 +424,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) => { From 7dfb1135fc2ed227b979a5d5df8b317d9942a17d Mon Sep 17 00:00:00 2001 From: Bladymir Tellez Date: Sat, 22 Aug 2020 10:56:29 -0500 Subject: [PATCH 2/2] Update index.html --- index.html | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/index.html b/index.html index 5a4b8f2..68f150c 100644 --- a/index.html +++ b/index.html @@ -1217,17 +1217,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