Skip to content

Instantly share code, notes, and snippets.

@themattchan
themattchan / KdTree.java
Last active March 3, 2016 08:54
Coursera Algs pt 1 Hw 5, example for my blog post.
import edu.princeton.cs.algs4.*;
import java.util.*;
public class KdTree {
private abstract class Node implements TreeApply {
protected Point2D point;
public abstract void apply (TreeFunction function);
}
private interface TreeFunction {
@themattchan
themattchan / homework-sty
Last active May 11, 2016 04:19
[DEPRECATED: see https://github.com/themattchan/latex-styles] My LaTeX CS/Math homework style as a YASnippet
# -*- mode: snippet; require-final-newline: nil -*-
# name: homework-sty
# key: homework
# binding: direct-keybinding
# --
\documentclass[10pt,letterpaper]{article}
\usepackage[T1]{fontenc}
\usepackage[parfill]{parskip} % line break instead of indentation
\usepackage[margin=1in]{geometry}
\usepackage{float} % use attribute [H] to force images to stay where they should be
%
% Listings hacks to emulate the math-style Haskell from Functional Pearls,
% Programming in Haskell, Pearls of Functional Algorithm Design. and other books.
%
% This is probably as close an emulation of the math-style generated with lhs2tex as I can get.
%
% Modified from:
% https://raw.githubusercontent.com/UCSD-PL/230-web/master/templates/haskellListings.tex
%
(* Folds *)
let rec fold_right f a xs = match xs with
| [] -> a
| x :: xs -> f (fold_right f a xs) x
let rec fold_left f a xs = match xs with
| [] -> a
| x :: xs -> fold_left f (f a x) xs
(* TODO: implement monoids and the foldable typeclass with modules*)
@themattchan
themattchan / DFA.hs
Last active November 15, 2015 04:29
{- LANGUAGE TupleSections -}
import Control.Monad
import qualified Data.Set as S
--type State = String
type Symbol = Char
data DFA st = DFA { states :: [st],
alphabet :: [Symbol],
delta :: st -> Symbol -> st,
startSt :: st
@themattchan
themattchan / SEXPRS FTW
Last active November 15, 2015 04:29
fuck xml.
#lang racket
(require xml)
(require xml/xexpr)
(define to-html (compose display
xexpr->string))
(define CONFIGS '((siteName "Andromeda")
(latitude 1234.567)
#lang racket
(require test-engine/racket-tests)
(define (rotations lst)
(letrec ((go (λ (n acc)
(if (= n 0) acc
(go (sub1 n) (let ((c (car acc)))
`(,`(,@(cdr c) ,(car c)) ,@acc)))))))
(reverse (go (length lst) (list lst)))))
#lang racket
#|
Simplified SXML grammar
<Element> ::= (<Symbol> <Children>...)
| (<Symbol> <AttribList> <Children>...)
<Children> ::= <Element> | <Value>
<AttribList> ::= (<Attribute> ...) | (@ <Attribute> ...)
module Golf where
import Data.List
skips :: [a] -> [[a]]
skips xs = map (map snd) $ zipWith filter filters (replicate len ids)
where len = length xs
ids = zip [1..len] xs
filters = map (\n (a,_) -> a `mod` n == 0) [1..len]
localMaxima :: [Integer] -> [Integer]
# MarkdownBlock
# Insert & parse Markdown anywhere, even nested in html
#
# Author: Matthew Chan <matt@themattchan.com>
# Date: January 2015
# License: GNU GPL
#
# Usage:
# {% markdown %}
# <md_text>