Skip to content

Instantly share code, notes, and snippets.

View forestbelton's full-sized avatar

Forest Belton forestbelton

View GitHub Profile
@forestbelton
forestbelton / my_log.c
Last active August 29, 2015 14:04
quick logarithm
#include <math.h>
double my_log(double x) {
static const double log2 = 0.693147;
double y, logy;
int n;
// factor x = y * 2^n, where 1 <= y < 2
// frexp gives us the range 0.5 <= y < 1
start
= (d:die _ { return d })*
number
= [0-9]
nonzero
= [1-9]
diesize
@forestbelton
forestbelton / data.g
Last active November 24, 2015 19:53
data declaration peg parser
Decl "data declaration"
= Data name:TName params:TParam* Equals first:Constructor rest:Alternative* {
return {
name: name,
params: params,
constructors: [first].concat(rest)
};
}
Data "data keyword"
@forestbelton
forestbelton / solver.html
Last active July 8, 2016 05:00
dfs "sliding on ice" puzzle solver
<!DOCTYPE html>
<html>
<head>
<style>
#grid {
line-height: 0;
}
#grid > * {
margin:0;
@forestbelton
forestbelton / Polynomial.hs
Last active March 2, 2018 20:09
Zhegalkin polynomials and conversion to algebraic normal form
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE GADTs #-}
import qualified Data.Set as S
import Data.List (intercalate, sort)
data UnaryOp
= Not
deriving (Show)

Keybase proof

I hereby claim:

  • I am forestbelton on github.
  • I am forestbelton (https://keybase.io/forestbelton) on keybase.
  • I have a public key ASB7l0uda7N7t_GLdLbqcuk7FEIn5nZ1CqS1OEJ3elTQhQo

To claim this, I am signing this object:

@forestbelton
forestbelton / trie-monoid.hs
Last active August 12, 2018 02:26
tries are monoids
import qualified Data.Map as M
newtype Trie c = Trie { getTrie :: M.Map c (Trie c) }
deriving (Show)
type Word c = [c]
instance Ord c => Monoid (Trie c) where
mempty = Trie M.empty
(Trie t) `mappend` (Trie t') = Trie $ M.unionWith mappend t t'
@forestbelton
forestbelton / lexer.h
Last active March 15, 2019 07:36
lexer header sketch
#ifndef CASE_LEXER_H_
#define CASE_LEXER_H_
struct lexer;
/**
* A data type which accepts a string of characters and produces a stream of
* tokens based on a set of predefined rules.
*/
typedef struct lexer lexer;
@forestbelton
forestbelton / extract-notes.sh
Created April 29, 2019 06:14
Extract notes from Notes.app
#!/usr/bin/bash
set -euxo pipefail
SQLITE=/Volumes/Macintosh\ HD/usr/bin/sqlite3
XXD=/Volumes/Macintosh\ HD/usr/bin/xxd
GZIP=/Volumes/Macintosh\ HD/usr/bin/gzip
NOTES_DB=NoteStore.sqlite
"${SQLITE}" ${NOTES_DB} 'select quote(zdata) from zicnotedata where zdata is not null' > notes-raw.txt
@forestbelton
forestbelton / dataclass.js
Created February 12, 2020 06:18
types without class
const dataclass = f => function() {
Object.entries(f(...arguments)).forEach(([k, v]) => {
this[k] = v
})
}
const F = dataclass((x, y) => ({x, y}))
const f = new F(1, 2)
console.log(f instanceof F) // "true"