Skip to content

Instantly share code, notes, and snippets.

package main
func main() {
c := make(chan string)
done := make(chan int)
const N = 10000000
go func() {
for range N {
c <- "Hello!"
<-c
package main
import "fmt"
func main() {
in := make(chan int)
out := in
for range 1000000 {
c := make(chan int)
go relay(c, out)
@bbriano
bbriano / https.go
Created October 28, 2024 09:09
Automatic certificate
package main
import (
"flag"
"fmt"
"log"
"net/http"
"golang.org/x/crypto/acme/autocert"
)
@bbriano
bbriano / getcert
Created June 24, 2024 13:09
get certificate using certbot
#!/bin/sh
certbot certonly -d example.com --standalone
// Example usage of Go's new range over func feature.
// See https://github.com/golang/go/issues/61897.
package main
import "fmt"
func natural(yield func(int) bool) {
for i := 0; true; i++ {
if !yield(i) {
break
@bbriano
bbriano / tictactoe.py
Last active June 7, 2024 20:25
add bots
# Tic Tac Toe game
#
# Usage: python3 tictactoe.py size players...
import random
# Board represents a tictactoe board with width == height == size >= 1.
# Valid positions are [1..size^2] (inclusive).
class Board:
def __init__(self, size):
// Experimenting with Dijkstra's mutual exclusion problem.
// See "Solution of a Problem in Concurrent Programming Control"
// https://dl.acm.org/doi/pdf/10.1145/365559.365617 for details.
//
// Here, each "computer" is incrementing `a` a million times.
// There's N=2 computers, so we expect a=2e6 at the end.
// We got a<2e6. Not sure why. My guess is line 31,32 is not atomic,
// allowing multiple goroutine to execute the critical section at the same time.
package main
@bbriano
bbriano / bourne_args.md
Created March 27, 2024 07:53
$* vs $@ vs "$*" vs "$@"

The difference between $* and $@ lies in the way they behave when they occur inside double quotes: $* behaves in the normal way, whereas $@ creates a separate double-quoted string for each command-line argument. That is, "$*" behaves as if you had written "$1 $2 $3", whereas "$@" behaves as if you had written "$1" "$2" "$3".

# Bijection from integers to rationals, showing rational numbers are countably infinite.
#
# Note that f is a bijection from positive integers to positive fractions.
# Finding a bijection from integers to rationals (ℤ → ℚ) is left as an exercise to the reader.
from fractions import Fraction
from math import ceil
def main() -> None:
for i in range(1, 100):
@bbriano
bbriano / birthday.py
Created February 21, 2024 06:15
In a room of 23 people, there's more than 50% chance someone shares a birthday.
import random
import matplotlib.pyplot as plt
NDAYS = 365
def main():
sim = [0 for _ in range(NDAYS+2)]
N = 1000
for _ in range(N):
for i in range(len(sim)):