Skip to content

Instantly share code, notes, and snippets.

View parkerziegler's full-sized avatar
🗺️
Working on cartokit

Parker Ziegler parkerziegler

🗺️
Working on cartokit
View GitHub Profile
@parkerziegler
parkerziegler / bfs.py
Created March 14, 2022 19:07
A concise implementation of breadth-first search (BFS) in Python.
"""
Assume we have a graph modeled as a dictionary like so:
graph = {
'a': ['b', 'c'],
'b': ['d'],
'c': ['e'],
'd': ['f'],
'e': [],
'f': []
@parkerziegler
parkerziegler / dfs_rec.py
Created March 14, 2022 19:04
A concise recursive version of depth-first search (DFS) in Python.
"""
Assume we have a graph modeled as a dictionary like so:
graph = {
'a': ['b', 'c'],
'b': ['d'],
'c': ['e'],
'd': ['f'],
'e': [],
'f': []
}
@parkerziegler
parkerziegler / dfs.py
Last active March 14, 2022 19:06
A concise iterative version of depth-first search (DFS) in Python.
"""
Assume we have a graph modeled as a dictionary like so:
graph = {
'a': ['b', 'c'],
'b': ['d'],
'c': ['e'],
'd': ['f'],
'e': [],
'f': []
}
@parkerziegler
parkerziegler / max-in-map.ts
Created January 30, 2022 18:17
Get the key of the entry with the maximum value in a Map.
function maxInMap<K>(map: Map<K, number>) {
return [...map.entries()].reduce((acc, el) =>
el[1] > acc[1] ? el : acc
)[0];
}
@parkerziegler
parkerziegler / regex-named-capture-groups.js
Created April 25, 2021 01:39
An example of how to name capture groups in regular expressions for easier access.
const re = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
const match = re.exec('2021-04-24');
const { year, month, day } = match.groups;
console.log({ year, month, day });
// { year: '2021', month: '04', day: '24' }
@parkerziegler
parkerziegler / migrate-to-rescript.sh
Last active December 9, 2020 02:03
A Bash script to run an automated upgrade from Reason (bs-platform@<8) to ReScript (bs-platform@>=8).
#!/usr/bin/env bash
function npm-do { (PATH=$(npm bin):$PATH; eval $@;) }
for file in src/*.re src/*.rei; do
[ -f "$file" ] || break
FILENAME=${file#src/}
BASENAME=${FILENAME%%.*}
cd src
@parkerziegler
parkerziegler / generate-srcSet.js
Created September 7, 2020 02:19
A simple Node.js script to generates assets for srcSet from a base asset using sharp.
const sharp = require('sharp');
const fs = require('fs');
const path = require('path');
// Set your sizes array based on the needs of your application.
const sizes = [
{ width: 480, suffix: 'sm' },
{ width: 800, suffix: 'md' },
{ width: 1080, suffix: 'lg' },
{ width: 1280, suffix: 'xl' },
@parkerziegler
parkerziegler / intersperse.ts
Created October 1, 2019 20:20
Intersperse items, including React elements, with a separator.
/**
* Modified from Sophie's StackOverflow answer here:
* https://stackoverflow.com/questions/23618744/rendering-comma-separated-list-of-links
*/
function intersperse<T>(arr: T[], sep: any) {
if (arr.length === 0) {
return [];
}
@parkerziegler
parkerziegler / use_trace_update.ts
Created September 19, 2019 10:41
A simple React hook to run a shallow comparison over all key-value pairs in an object against their previous values. Useful for determining which of your props are triggering unexpected re-renders.
import React from 'react';
/* eslint-disable no-console */
export const useTraceUpdate = <P>(props: P) => {
const prev = React.useRef(props);
React.useEffect(() => {
const changedProps = Object.entries(props).reduce((ps, [k, v]) => {
if (prev.current[k] !== v) {
ps[k] = [prev.current[k], v];
}