Skip to content

Instantly share code, notes, and snippets.

@gigamonkey
gigamonkey / Primes.java
Created November 19, 2024 18:30
Primes impl with streams
import static java.lang.Math.sqrt;
import static java.util.stream.IntStream.range;
import static java.util.stream.IntStream.rangeClosed;
public class Primes {
public boolean isPrime(int n) {
return n > 1 && !rangeClosed(2, (int) sqrt(n)).anyMatch(d -> n % d == 0);
}
@gigamonkey
gigamonkey / Reviewers.java
Last active October 31, 2024 20:35
Reviewer assignment algorithm. I'm not sure this actually can generate all possible legal combinations of assignments but it's pretty random and only generates legal assignments. I'm also not sure it can't. First version is mostly procedural. Second one is heavy on streams.
import static java.util.stream.Collectors.joining;
import java.io.*;
import java.nio.file.*;
import java.util.*;
import java.util.stream.*;
public record Reviewers(List<String> handles) {
//////////////////////////////////////////////////////////////////////////////
// Utility methods
@gigamonkey
gigamonkey / Lift.java
Created October 31, 2024 03:46
Demo of lift functions in Java
import java.util.*;
import java.util.function.*;
public class Lift {
public static interface Expression {
public Optional<Double> evaluate();
}
// This is a higher-order function that takes a function (i.e. an istance of a
@gigamonkey
gigamonkey / TwentyQuestions.java
Created October 11, 2024 17:32
A version of Twenty Questions (Four Questions, actually)
import java.util.Scanner;
public class TwentyQuestions {
public static final String[] GUESSES = {
"a hamster",
"a ferret",
"a dog",
"a cat",
"an elephant",
@gigamonkey
gigamonkey / Sexps.java
Created September 26, 2024 22:00
Simple arithmetic sexps parsing and evaluating.
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
public class Sexps {
// Thing that can be evaluated.
public static interface Expression {
public double evaluate();
}
@gigamonkey
gigamonkey / oauth-notes.md
Created June 25, 2024 19:13
Trying to wrap my brain around OAuth (again)

Trying to understand OAuth

Here’s my basic understanding of how to do client-side OAuth, meaning we want JavaScript running in the browser to be able to authenticate to a service (e.g., GitHub) and then access that service directly from the browser. The key security requirement is that the client-side JavaScript can’t know the client secret because the whole point of secrets as that they be secret. Assume in what follows that our web server is running on example.com. Also note that all these requests should be over HTTPS.

@gigamonkey
gigamonkey / pugsql.js
Created June 18, 2024 21:31
PugSQL inspired JS library.
import Database from 'better-sqlite3';
import { argv } from 'process';
import fs from 'fs';
/*
* The kind of queries we support.
*/
const kinds = {
// Execute SQL, returns { changes, lastInsertRowId } object.
run: () => (stmt) => (...args) => stmt.run(...args),
@gigamonkey
gigamonkey / find-times.py
Last active June 9, 2024 18:48
Find multiple times to hold a meeting to maximize number of possible attendees.
#!/usr/bin/env python
"""
Find times to hold the CSA Zoom meeting based on when people said they could do it.
Given a number N, find the N times that maximize the number of people who can come.
"""
from datetime import datetime
from functools import reduce
import json
@gigamonkey
gigamonkey / bouncing.js
Last active March 19, 2024 23:28
Bouncing ball utility functions
// Some utility functions
const distance = (a, b) => Math.abs(a - b);
const distance2d = (p1, p2) => Math.hypot(p1.x - p2.x, p1.y - p2.y);
const clamp = (n, min, max) => (n < min ? min : n > max ? max : n);
const nextPos = (b) => ({ x: b.x + b.dx, y: b.y + b.dy });
/*
* A number is prime if it is not divisible by any number other than 1 and
* itself. 1 is not prime. Thus you can test whether a number greater than 1 is
* prime by checking whether it is divisible by any smaller number greater than
* one.
*/
public class Primes {
public boolean isPrime(int n) {