Skip to content

Instantly share code, notes, and snippets.

@munkacsitomi
Created December 2, 2020 14:25
Show Gist options
  • Save munkacsitomi/ab03bf0f5f2a48bea91157c5700bc4cc to your computer and use it in GitHub Desktop.
Save munkacsitomi/ab03bf0f5f2a48bea91157c5700bc4cc to your computer and use it in GitHub Desktop.
Fun JavaScript stuff
// 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