Skip to content

Instantly share code, notes, and snippets.

@calebsander
calebsander / Makefile
Created October 9, 2019 00:22
CS 24 git Recitation — graphing program example
CC = clang-with-asan
CFLAGS = -Wall -Wextra
graph: function.o graph.o main.o
$(CC) $(CFLAGS) -lm $^ -o $@
%.o: %.c
$(CC) $(CFLAGS) -c $^ -o $@
@calebsander
calebsander / monads.md
Created April 28, 2019 23:11
An introduction to monads (in Haskell)

An introduction to monads (in Haskell)

Motivation

Consider how stateful computations could be expressed in a pure functional language like Haskell. We will use a mutable stack as an example, but we'll see later that the same ideas generalize to any sort of mutable state.

A stack is a simple data structure used extensively to model imperative computation. It is an ordered collection of values where all modifications happen at one end of the collection. In Java, for example, a Stack is accessed/manipulated with the following two methods:

@calebsander
calebsander / base64.wast
Created July 17, 2018 23:16
base-64 encode and decode in WASM
(module
;; Memory layout
;; 0 - 63: lookup
;; 64 - 186: revLookup
;; 187 - : input, followed by output
(global $INPUT (export "INPUT") i32 (i32.const 187))
(memory (export "memory") 1)
(data (i32.const 0) "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
(func (export "init")
(local $i i32)
@calebsander
calebsander / patterns.md
Last active June 30, 2020 19:30
Handy JavaScript shorthands

Arrays

  • Create a new array with n elements, each initialized to x
new Array(n).fill(x)
  • Create a new array with n elements, each initialized as f()
// Imperatively
const a = []
for (let i = 0; i < n; i++) a[i] = f()
@calebsander
calebsander / neopixel.ino
Created March 20, 2016 16:51
Script for dank pit LEDs
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define START_PIN 3
#define NUM_STRIPS 3
#define NUM_PIXELS 150
// Parameter 1 = number of pixels in strip
function Fraction(n, d) {
this.n = n;
if (d) this.d = d;
else this.d = 1;
}
Fraction.prototype.reciprocal = function() {
return {n: this.d, d: this.n};
}