Skip to content

Instantly share code, notes, and snippets.

@gigamonkey
gigamonkey / TwentyQuestions.java
Created October 5, 2025 20:02
TwentQuestions that reads questions and guesses from file
import java.util.*;
import java.io.*;
import java.nio.file.*;
/**
* Twenty questions that parses from a file that looks like:
*
* Is it a mammal? {
* Does it live in the water? {
* Does it get caught in tuna nets? {
@gigamonkey
gigamonkey / PrimeStream.java
Created September 24, 2025 03:12
kicking the tires on Gatherer mechanism in Java streams.
import java.util.*;
import java.util.stream.*;
public class PrimeStream {
private static Gatherer<Long, List<Long>, Long> sieve =
Gatherer.ofSequential(
ArrayList::new,
(soFar, n, downstream) -> {
if (isPrime(soFar, n)) {

Crockford: One of the things I’ve been pushing is code reading. I think that is the most useful thing that a community of programmers can do for each other—spend time on a regular basis reading each other’s code. There’s a tendency in project management just to let the programmers go off independently and then we have the big merge and if it builds then we ship it and we’re done and we forget about it.

One of the consequences of that is that if you have weak or confused programmers you’re not aware of their actual situation until much too late. And so the risks to the project, that you’re going to have to build with stuff that’s bad and the delays that that causes, that’s unacceptable. The other thing is that you may have brilliant programmers on the project who are not adequately mentoring the other people on the team. Code reading solves both of those problems.

Seibel: Can you talk a bit about how you conduct a code reading?

Crockford: At each meeting, someone’s responsible for reading their code,

@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),