Last active
December 22, 2016 11:18
-
-
Save abiodun0/6b25d84a96a1f4d08d6d2d9cdcc1de9a to your computer and use it in GitHub Desktop.
Use array destructuring, recursion, and the rest/spread operators to create a function 'double' that will return a new array with all values inside of it multiplied by two. Do not use any array helpers!
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
def fold(f, init, arr): | |
return init if len(arr) < 1 else fold(f, f(init, arr[0]), arr[1:]) | |
def map(f, xs): | |
return fold(lambda acc, v: acc + [f(v)], [], xs) | |
def multiplyByTwo(x): | |
def multiplySecond(y): | |
return x * y | |
return multiplySecond | |
print map(multiplyByTwo(2), [34,55,87]) |
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
const fold = (f, init, xs) => | |
!xs.length ? init | |
: fold(f, f(init, xs[0]), xs.slice(1)); | |
const map = (f, xs) => | |
fold((acc, v) => [...acc, f(v)], [], xs); | |
const curriedMultiply = x => y => x * y; | |
const double = xs => map(curriedMultiply(2), xs); | |
console.log(double([1, 2, 3, 4])); |
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
const fold = (f, init, xs) => | |
!xs.length ? init | |
: fold(f, f(init, xs[0]), xs.slice(1)); | |
const map = (f, xs) => | |
fold((acc, v) => [...acc, f(v)], [], xs); | |
console.log(map(x => x * 2, [1, 2, 3, 4])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment