Skip to content

Instantly share code, notes, and snippets.

timruffles / operation.ts
Last active April 23, 2021 17:11
operation - like Result and various other ADTs in Haskell etc
View operation.ts
export type Operation<T, E> =
typeof NotStarted |
typeof Waiting |
Complete<T> |
export enum OperationState {
NotStartedState = 'NotStarted',
WaitingState = 'Waiting',
CompleteState = 'Complete',
timruffles /
Created June 12, 2020 14:09
A bash script that counts go function and method lines of code (including whitespace and comments)
# Usage: bash some/directory
set -euo pipefail
main() {
for f in $(find $1 -name '*.go' -not -name 'test_*.go'); do
count_funcs < "$f" | awk "{ print \"$f\", \$1, \$2 }"
timruffles /
Created July 11, 2019 13:33
A bad way to generate a random init in a range using only bash built-ins. Useful in a pinch when you aren't sure what external programs are available.
# Gets an int between min max inclusive very inefficiently, but
# only using bash built-ins. More inefficient the smaller the gap
# usage: n=$( bad_random_int 1000 2000 )
bad_random_int() {
local min=$1
local max=$2
local n=0
while [[ "$n" -lt "$min" ]] || [[ "$n" -gt "$max" ]]; do
timruffles / go-quiz.go
Last active August 19, 2019 07:43
What does this program output, and why? Reason it out gophers! Answers in a spoiler block -
View go-quiz.go
package main
import "fmt"
func main() {
type person struct {
nickname string
ppl := []person{
timruffles / demo.go
Last active March 7, 2019 17:19
Go - cost of change for methods in application code. App code has many types that work together, requiring mocking.
View demo.go
// Let's define a type with a method that needs a fair few arguments
type Thing struct {}
func (c *Thing) Verb(a A,b B,c C,d D) {}
// We have things that depend on Thing and we want to test them, so to mock it we'll need to
// define an interface and generate a mock
type Thinger interface {
Verb(a A,b B,c C,d D)
timruffles /
Last active February 25, 2019 15:52
Go dep - adding a fork via `source`. Useful when you want to use a private fork of a public repo for instance, without updating all package paths.
dep ensure -add ${SOURCE}/${PACKAGE}:${FORK}.git@${CONSTRAINT}
timruffles /
Last active October 26, 2022 03:58
Next.js page request handling

How Next.js responds to a page request with HTML

We create the next request handler function via app.getRequestHandler(). This returns a standard express handler, so we use it like expressApp.get('*', middlewareA(), middlewareB(), nextApp.getRequestHandler()).

When the handler is invoked:

  • Server#handleRequest (next-server/server/lib/next-server.js)
    • Parses URL + query string if not already done
  • Server#run
  • Searches for matching route
timruffles / js-values-sketch.c
Created July 13, 2018 07:57
Representing JsValues for JS -> C compiler
View js-values-sketch.c
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdint.h>
#include <string.h>
* So, plan is to store the values for the JS compiler
* in a union, nicking Lua's idea. That way immediate
* values like numbers live inside the JSValue wrapper,
timruffles / enums.ts
Last active June 15, 2017 09:34
string enums (ish, with some caveats) before ts 2.4
View enums.ts
// cast the strings as any
enum StringEnum {
a = 'a' as any,
b = 'b' as any,
const a = StringEnum.a;
const x = 'a';
const y = 1;
timruffles /
Last active June 14, 2017 17:01
A story, wherein I discover 'share()' in RxJS is a tricky blighter

The docs for .share() state: 'Returns a new Observable that multicasts (shares) the original Observable.'.

This sounds like share() is a lovely simple method that'll make your life easier. However share() is really a trap, for if you have the hubris to use .share() the angry complexity bees that live inside RxJS will swarm out and sting you in the eye (in a rate-limited fashion).

Let's see why. Our goal is to take a cold observable, take only the first item from it, do something to it, and share the resulting observable between 2 subscribers. They should both share the values from our new observable. Easy huh?

const coldObservable = Rx.Observable.create(function(observer) {
  console.log('observable created')