Skip to content

Instantly share code, notes, and snippets.

View matthewdavi's full-sized avatar

Matthew Davis matthewdavi

View GitHub Profile
@matthewdavi
matthewdavi / demo.js
Created May 31, 2021 13:50
how to return early from a forEach
function forEachWithReturn(array, cb){
try{
array.forEach(cb)
}
catch(e){
return e;
}
}
const three = forEachWithReturn([1,2,3,4,5], (num) => {
@matthewdavi
matthewdavi / settings.json
Created December 17, 2019 14:39
vs code settings
{
"sublimeTextKeymap.promptV3Features": true,
"editor.multiCursorModifier": "ctrlCmd",
"editor.snippetSuggestions": "top",
"editor.formatOnPaste": true,
"workbench.colorTheme": "Material Theme",
"editor.fontSize": 14,
"terminal.integrated.shell.osx": "/bin/zsh",
"terminal.external.osxExec": "Hyper.app",
"eslint.packageManager": "yarn",
@matthewdavi
matthewdavi / index.js
Created July 17, 2019 17:27
default params as a form of "let binding"
/* 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)),
@matthewdavi
matthewdavi / Fibonacci.tsx
Last active June 28, 2019 19:24
Recursive react component
/* I haven't seen anyone write about this, but I thought about it this morning in the shower and it works, why wouldn't it?
JSX is just sugar for function calls (React.createElement). */
const Fib: React.FC<{
start?: number;
end: number;
previous?: number[];
}> = ({ end, start = 2, previous = [1, 1] }) => {
const memo = previous.concat(
previous[previous.length - 1] + previous[previous.length - 2]