Skip to content

Instantly share code, notes, and snippets.

@abiodun0
Last active December 22, 2016 11:18
Show Gist options
  • Save abiodun0/6b25d84a96a1f4d08d6d2d9cdcc1de9a to your computer and use it in GitHub Desktop.
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!
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])
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]));
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