Skip to content

Instantly share code, notes, and snippets.

View justinmeiners's full-sized avatar

Justin Meiners justinmeiners

View GitHub Profile
@vidarh
vidarh / closures-basic.c
Created December 18, 2009 12:10
A number of ways to implement closures in C, in preparation of an upcoming blog post
#include <stdio.h>
#include <stdlib.h>
struct closure {
void (* call)(struct closure *);
int x;
};
CL-USER> (asdf:operate 'asdf:load-op :hello)
; loading system definition from /home/cametan/hello.asd into
; #<PACKAGE "ASDF0">
; registering #<SYSTEM HELLO {AB3F071}> as HELLO
NIL
CL-USER>

Polymorphism in Scheme via compile time dispatch

Here's an example of polymorphism in C++:

int nth ( int a[] , int i ) { return a[i] ; }

double nth ( double a[] , int i ) { return a[i] ; }

One way to think of this is that there are two functions called nth. One can be called with an int array and the other can be called with a double array. The function that is called is determined at compile time by looking at the type of the expression of the first argument.

Generics in Scheme

In a previous note I demonstrated polymorphism in Scheme using Chez Scheme's compile time values and properties.

The code in that example is pretty raw; you might not want to code each generic in that manner. One approach is to decouple the signature/method table. We can do this by factoring out some code into a 'make-generic' procedure:

(define (make-generic table)
  (lambda (stx)
 (lambda (lookup)
@chaoxu
chaoxu / word_problem_braid_group.hs
Created June 23, 2011 05:54
Word Problem for Braid Group
default (Int, Integer, Rational, Double)
if' :: Bool -> a -> a -> a
if' True x _ = x
if' False _ y = y
isBraidIdentity x n =
[1..n] == concat (map (braidReduce x) [[y] | y <- [1..n]])
braidReduce = flip (foldr homomorphism)
@jboner
jboner / latency.txt
Last active May 3, 2024 15:17
Latency Numbers Every Programmer Should Know
Latency Comparison Numbers (~2012)
----------------------------------
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns 3 us
Send 1K bytes over 1 Gbps network 10,000 ns 10 us
Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD
@aras-p
aras-p / ImportMeshUtility.cpp
Created May 31, 2012 15:01
Unity tangent space calculation
// THIS IS ONLY A PORTION OF THE FILE
// WILL NOT COMPILE OUT OF THE BOX!
void OrthogonalizeTangent (TangentInfo& tangentInfo, Vector3f normalf, Vector4f& outputTangent)
{
TangentInfo::Vector3d normal = { normalf.x, normalf.y, normalf.z };
TangentInfo::Vector3d tangent = tangentInfo.tangent;
TangentInfo::Vector3d binormal = tangentInfo.binormal;
@nikcub
nikcub / README.md
Created October 4, 2012 13:06
Facebook PHP Source Code from August 2007
@bxt
bxt / proxy.py
Last active August 30, 2020 15:57
A very basic caching python HTTP proxy server.
# Originally from http://sharebear.co.uk/blog/2009/09/17/very-simple-python-caching-proxy/
#
# Usage:
# A call to http://localhost:80000/example.com/foo.html will cache the file
# at http://example.com/foo.html on disc and not redownload it again.
# To clear the cache simply do a `rm *.cached`. To stop the server simply
# send SIGINT (Ctrl-C). It does not handle any headers or post data.
import BaseHTTPServer
import hashlib
@lelandbatey
lelandbatey / whiteboardCleaner.md
Last active April 25, 2024 02:01
Whiteboard Picture Cleaner - Shell one-liner/script to clean up and beautify photos of whiteboards!

Description

This simple script will take a picture of a whiteboard and use parts of the ImageMagick library with sane defaults to clean it up tremendously.

The script is here:

#!/bin/bash
convert "$1" -morphology Convolve DoG:15,100,0 -negate -normalize -blur 0x1 -channel RBG -level 60%,91%,0.1 "$2"

Results