Created
July 17, 2019 17:27
-
-
Save matthewdavi/b5b1c094dea6fce49c7c2df8009218ee to your computer and use it in GitHub Desktop.
default params as a form of "let binding"
This file contains 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
/* I've been thinking about default params a bit. And how you can use earlier params as values in the default params. | |
It's a nice way to write functions without introducing any variable declaration statements in the body */ | |
const greaterThan = num1 => num2 => num1 < num2; | |
const not = fn => (...args) => !fn(...args); | |
const quickSort = ( | |
nums, | |
pivot = nums[Math.floor(Math.random() * nums.length)], | |
left = nums.filter(not(greaterThan(pivot))), | |
right = nums.filter(greaterThan(pivot)), | |
done = new Set(nums).size <= 1 | |
) => (done ? nums : [...quickSort(left), ...quickSort(right)]); | |
const filterWith = fn => ( | |
[first, ...rest], | |
acc = [], | |
passes = fn(first), | |
next = passes ? acc.concat(first) : acc | |
) => (rest.length === 0 ? next : filterWith(fn)(rest, next)); | |
const GCD = ( | |
num1, | |
num2, | |
larger = Math.max(num1, num2), | |
smaller = Math.min(num2, num2), | |
remainder = larger % smaller | |
) => (remainder === 0 ? smaller : GCD(larger, remainder)); | |
const mapWith = fn => ([first, ...rest], acc = [], mapped = fn(first)) => | |
rest.length === 0 ? [...acc, mapped] : mapWith(fn)(rest, [...acc, mapped]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment