Created
December 2, 2020 14:25
-
-
Save munkacsitomi/ab03bf0f5f2a48bea91157c5700bc4cc to your computer and use it in GitHub Desktop.
Fun JavaScript stuff
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Old, imperative way | |
const originalDoubleList = list => { | |
const newList = []; | |
for (let i = 0; i < list.length; i++) { | |
newList[i] = list[i] * 2; | |
} | |
return newList; | |
}; | |
console.log(originalDoubleList([20])); | |
// New, declarative way with a twist ;) | |
const doubleList = list => list.myMap(x => x * 2); | |
const box = value => ({ | |
myMap: f => box(f(value)), | |
toString: () => `box value: ${value}` | |
}); | |
const tmp = doubleList(box(20)); | |
console.log(tmp.toString()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment