This file contains hidden or 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
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(); |
This file contains hidden or 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
const COLLATOR = Intl.Collator('generic', {sensitivity: 'base'}) | |
export function levenshteinEditDistance( | |
source: string, | |
target: string, | |
): number { | |
if (source.length === 0 || target.length === 0) { | |
return 0 | |
} |
This file contains hidden or 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
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 }), | |
) | |
} |
This file contains hidden or 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
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 ], |
This file contains hidden or 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
#!/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'" |
This file contains hidden or 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
package main | |
import "fmt" | |
type listNode struct { | |
value int | |
next *listNode | |
} | |
func (l *listNode) containsCycle() bool { |
This file contains hidden or 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
package main | |
import ( | |
"fmt" | |
"net/http" | |
"time" | |
) | |
func main() { | |
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
This file contains hidden or 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
class DoublyLinkedList { | |
static from(array) { | |
const dll = new DoublyLinkedList() | |
for (const val of array) { | |
dll.append(val) | |
} | |
return dll | |
} | |
constructor() { |
This file contains hidden or 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
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; | |
} |
This file contains hidden or 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
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() |