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.
This file contains 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
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); | |
} |
This file contains 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
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 |
This file contains 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
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 |
This file contains 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
import java.util.Scanner; | |
public class TwentyQuestions { | |
public static final String[] GUESSES = { | |
"a hamster", | |
"a ferret", | |
"a dog", | |
"a cat", | |
"an elephant", |
This file contains 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
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(); | |
} |
This file contains 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
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), |
This file contains 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 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 |
This file contains 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
// 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 }); |
This file contains 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
/* | |
* 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) { |
NewerOlder