Skip to content

Instantly share code, notes, and snippets.

View jesseschalken's full-sized avatar

Jesse Schalken jesseschalken

View GitHub Profile

My preferred code style is 2-space K&R. This is intended to provide a justification for this style.

Why K&R?

K&R style has the following properties:

  1. Provides symmetric size (in terms of screen space consumed) between the opening and closing syntax of a clode block.
  2. Forces no empty or meaningless lines, thereby avoiding artificial distance between related things that should be together.
  3. Consumes the minimum vertical space while keeping the opening and closing syntax of a block on separate lines from the content.

Interfaces and Sum Types

The way different programming languages implement the mapping of runtime polymorphic values to code both at the source level and in the language implementation is a defining characteristic. There are two opposite approaches that languages facilitate to varying degrees:

  1. Interfaces / Traits / Abstract Classes / Inheritance / Method Overriding (from here on, just "Interfaces")
  2. Sum Types / Algebraic Data Types / Tagged Unions / Pattern Matching (from here on, just "Sum Types")

Example

You have two types: A, containing a string, and B, containing an int, and two operations: x and y.

@jesseschalken
jesseschalken / foo.md
Last active September 22, 2022 09:44
C++ Special Member Functions
  • ⚪ = uninitialized memory
  • 🔵 = value 1
  • 🟢 = value 2
name operator note
⚪ → 🔵 construction A() constructor may require parameters
constructor may have multiple overloads
🔵 → ⚪ destruction ~A()
import { ClientReadableStream } from "grpc-web";
import { Observable } from "rxjs";
export function grpcToRx<T>(f: () => ClientReadableStream<T>): Observable<T> {
return new Observable<T>(subscriber => {
const stream = f();
stream.on("error", e => subscriber.error(e));
stream.on("end", () => subscriber.complete());
stream.on("data", x => subscriber.next(x));
return () => stream.cancel();
@jesseschalken
jesseschalken / Uri.php
Last active January 28, 2022 08:14
Simple implementation of \Psr\Http\Message\UriInterface
<?php
class Uri implements \Psr\Http\Message\UriInterface {
const PASS = 'pass';
const USER = 'user';
const PORT = 'port';
const SCHEME = 'scheme';
const QUERY = 'query';
const FRAGMENT = 'fragment';
const HOST = 'host';
@jesseschalken
jesseschalken / format serialize.php
Last active January 12, 2022 15:37
Pretty print a PHP serialized value. Useful for finding the differences between two serialized values.
<?php
/*
MIT License
Copyright (c) 2021 Jesse Schalken
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
@jesseschalken
jesseschalken / boolsToBraille.kt
Created April 29, 2021 23:49
Convert Booleans to a braille String in Kotlin Java Scala
fun Iterable<Boolean>.toBrailleString(): String =
chunked(8) {
fun p(i: Int, j: Int) =
if (i < it.size && it[i]) 1 shl j else 0
'\u2800' +
p(0, 0) + p(1, 1) + p(2, 2) + p(3, 6) +
p(4, 3) + p(5, 4) + p(6, 5) + p(7, 7)
}.toCharArray().concatToString()
T.also(block: (T) -> Unit): T
T.apply(block: T.() -> Unit): T
T.let(block: (T) -> R): R
T.run(block: T.() -> R): R
export interface HashImpl<T> {
hash?(value: T): unknown;
equals(a: T, b: T): boolean;
}
export class AssociationList<K, V> implements Map<K, V> {
private inner: {readonly key: K; value: V}[] = [];
constructor(
private readonly impl: HashImpl<K>,
@jesseschalken
jesseschalken / iuhgi.md
Created July 16, 2020 14:13
C++ pointer alternatives
C C++
T* owning a single object T
T* borrowing a single object T&
T* owning a single object, nullable std::optional<T>
T* owning a single object, polymorphic std::unique_ptr<T>
T* owning an array (dynamic size) std::vector<T>
T[N] owning an array (static size) std::array