Skip to content

Instantly share code, notes, and snippets.

View christianscott's full-sized avatar

Christian Scott christianscott

View GitHub Profile
@christianscott
christianscott / levenshtein_distance.rs
Created July 4, 2019 12:36
Levenshtein edit distance (rust)
use std::cmp::{self, Ord};
pub fn levenshtein_distance(source: &str, target: &str) -> usize {
if source.is_empty() || target.is_empty() {
return 0;
}
let source_chars: Vec<char> = source.chars().collect();
let target_chars: Vec<char> = target.chars().collect();
@christianscott
christianscott / levenshtein_edit_distance.ts
Created July 3, 2019 15:20
Levenshtein edit distance solution using DP
const COLLATOR = Intl.Collator('generic', {sensitivity: 'base'})
export function levenshteinEditDistance(
source: string,
target: string,
): number {
if (source.length === 0 || target.length === 0) {
return 0
}
@christianscott
christianscott / all_settled.ts
Created July 3, 2019 12:56
Promise allSettled polyfill
type SettleResult<T> =
| { status: 'fulfilled'; value: T }
| { status: 'rejected'; reason: any };
function reflect<T>(promise: Promise<T>): Promise<SettleResult<T>> {
return promise.then(
value => ({ status: 'fulfilled', value }),
reason => ({ status: 'rejected', reason }),
)
}
@christianscott
christianscott / f2.js
Last active June 12, 2019 16:45
How many bytes in a family emoji?
let family = '👨‍👨‍👧‍👧'
let codePoints = [...family]
// [ '👨', '‍', '👩', '‍', '👧', '‍', '👦' ]
let bytes = codePoints.map(cp => [...Buffer.from(cp)])
// [ [ 240, 159, 145, 168 ],
// [ 226, 128, 141 ],
// [ 240, 159, 145, 169 ],
// [ 226, 128, 141 ],
@christianscott
christianscott / git-get.sh
Created June 5, 2019 00:55
Clone a repo from github into a GOPATH-style working directory
#!/usr/bin/env bash
set -e
main() {
local repo_path;
repo_path="$1"
if ! [[ $repo_path =~ ^.+/.+$ ]]; then
echo "invalid repo path. git-get expects a path of the form 'user/repo'"
@christianscott
christianscott / linked_list.go
Last active May 28, 2019 04:20
Linked list cycle detection in Golang
package main
import "fmt"
type listNode struct {
value int
next *listNode
}
func (l *listNode) containsCycle() bool {
@christianscott
christianscott / server.go
Created May 26, 2019 03:33
Chunked HTTP responses in Go
package main
import (
"fmt"
"net/http"
"time"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
@christianscott
christianscott / doubly_linked_list.js
Created April 15, 2019 14:24
Doubly linked list - with reverse!
class DoublyLinkedList {
static from(array) {
const dll = new DoublyLinkedList()
for (const val of array) {
dll.append(val)
}
return dll
}
constructor() {
@christianscott
christianscott / keyed_set.ts
Last active April 9, 2019 01:28
KeyedSet -- set with a "key", like the Python set
export class KeyedSet<T, K> implements Set<T> {
private readonly keyValMap: Map<K, T> = new Map();
constructor(private readonly makeKey: (value: T) => K) {}
add(value: T) {
const key = this.makeKey(value);
this.keyValMap.set(key, value);
return this;
}
@christianscott
christianscott / memoize.ts
Last active March 27, 2019 22:43
Memoize decorator
interface IMemoizeCache<K, V> {
has(key: K): boolean;
get(key: K): V | undefined;
set(key: K, value: V): this;
}
type MapKey = string | number | symbol
class ArrayKeyedMap<K extends any[], V> implements IMemoizeCache<K, V> {
private readonly map: Map<MapKey, V> = new Map()