Skip to content

Instantly share code, notes, and snippets.

View kevinji's full-sized avatar
🏠
Working from home

Kevin Ji kevinji

🏠
Working from home
View GitHub Profile
@kevinji
kevinji / hash-memo.rs
Created January 20, 2022 05:39
A HashMap-backed memoizer.
use std::{collections::HashMap, hash::Hash};
pub struct Memo<T, U, V>
where
T: Fn(U) -> V,
U: Eq + Hash + Copy,
V: Copy,
{
f: T,
cache: HashMap<U, V>,
@kevinji
kevinji / state_machine.ml
Created January 19, 2020 07:36
A simple state machine encoded as GADTs.
type _ state =
| A : [`A] state
| B : [`B] state
| C : [`C] state
type (_, _) edge =
| AC : ([`A], [`C]) edge
| AB : ([`A], [`B]) edge
| BC : ([`B], [`C]) edge
@kevinji
kevinji / ivar.js
Last active April 7, 2019 17:24
A simple implementation of an IVar using ES6 Promises.
function createIvar() {
let set;
const get = new Promise(resolve => { set = resolve });
return Object.freeze({ get, set });
}
@kevinji
kevinji / 1password.py
Created September 24, 2018 12:29
Properly escape &amp; from 1password passwords and URLs using the command-line tool, op.
import json
import subprocess
import sys
def read_json(*args):
output = subprocess.check_output(*args)
return json.loads(output)
def pp(obj):
"""
@kevinji
kevinji / powerset.js
Created December 13, 2017 21:48
Find the powerset of an array.
// Source: https://stackoverflow.com/a/42774138/558592 (slightly tweaked)
function powerset(arr) {
return (Array.from({ length: 1 << arr.length }, (_, k) => k)
.map(i => arr.filter((_, j) => i & (1 << j))));
}
@kevinji
kevinji / quine.py
Created July 19, 2015 09:42
A quine for Python.
c='print("c="+repr(c)+";eval(c)")';eval(c)
@kevinji
kevinji / facets.py
Created July 14, 2014 02:47
Download wallpapers from the FACETS website, http://www.facets.la/.
"""
Download wallpapers from the FACETS website.
"""
import urllib.request
from urllib.error import HTTPError, URLError
from bs4 import BeautifulSoup
"""
Grab all images from the front page, and save them to a folder.
@kevinji
kevinji / shift_letters.py
Last active March 14, 2024 06:09
Shift letters in a string by a certain number of places.
def shift_letters(string, shift):
new_string = ""
for char in string:
if char.isupper():
end_ord = 90 # Z
elif char.islower():
end_ord = 122 # z
else:
# Skip non-letters
@kevinji
kevinji / smugmug.js
Last active March 14, 2024 06:09
Grabs the download URLs for each of the images on a Smugmug gallery, and zips them up into a downloadable file.
/**
* smugmug.js is a script that grabs the download URLs for each of the images on
* a Smugmug gallery, and zips them up into a downloadable file. smugmug.js can
* be run as a bookmarklet or in the browser console.
*
* To run sumugmug.js as a bookmarklet, simply copy the code into a JavaScript
* minifier site like JSCompress (http://jscompress.com/), add `javascript:`
* to the beginning of the (now one-line) output, and then copy the code into
* the location or the URL section of a newly-created bookmark.
*