Skip to content

Instantly share code, notes, and snippets.

View tcsavage's full-sized avatar
🦡

Tom Savage tcsavage

🦡
View GitHub Profile
.dynamic-toc > * {
margin-top: 0px;
margin-bottom: 0px;
}
.dynamic-toc > * br {
display: none;
}
.dynamic-toc > *:first-child {
@tcsavage
tcsavage / Map.hs
Last active August 29, 2015 14:12
Implementation of Conal's TMap and Map from his Denotational Design paper
{-# LANGUAGE StandaloneDeriving, GeneralizedNewtypeDeriving #-}
module Map where
import Data.Functor.Compose
import Data.Monoid
import Prelude hiding (lookup)
newtype TMap k v = TMap (k -> v)
;; F-Algebra recursion schemes:
(defprotocol Functor
"Functor"
(fmap [xs f]))
(defn cata [alg f] (alg (fmap f (partial cata alg))))
(defn ana [coalg a] (fmap (coalg a) (partial ana coalg)))
@tcsavage
tcsavage / lengthindexed.cpp
Last active August 29, 2015 14:00
Length-indexed list type
#include <iostream>
#include <functional>
#include <memory>
#include <assert.h>
// Based on Bartosz Milewski's immutable list class.
// http://bartoszmilewski.com/2013/11/13/functional-data-structures-in-c-lists/
// Defined externally to List because it needs to be independent from the size parameter.
template <typename T>
@tcsavage
tcsavage / diffuseM.hs
Created May 6, 2013 14:42
Diffuse shading with shadows
diffuseM :: Colour -> Material () (BSDF Colour)
diffuseM col = proc () -> do
out <- diffuse -< col -- Get diffuse shading
shad <- traceM <<< getInidentRay -< () -- Test path to light
returnA -< maybe holdout (\(_,_,_,e) -> if e then out else holdout) shad -- Set BSDF to black if in shadow
\begin{landscape}
\begin{longtable}{|l|p{120pt}|p{120pt}|p{120pt}|p{120pt}|}
\caption{Functional Requirements} \\
\hline
\textbf{ID} & \textbf{What} & \textbf{How} & \textbf{Test} & \textbf{Expected Result} \\ \hline
\endfirsthead
\multicolumn{4}{c}%
{\tablename\ \thetable\ -- \textit{Continued from previous page}} \\
\hline
\textbf{ID} & \textbf{What} & \textbf{How} & \textbf{Test} & \textbf{Expected Result} \\ \hline
trait Monoid {
static fn mempty() -> self;
fn mappend(other: self) -> self;
}
impl int: Monoid {
static fn mempty() -> int { 0 }
fn mappend(other: int) -> int { self + other}
}
@tcsavage
tcsavage / primaility.hs
Last active December 14, 2015 10:19
Testing time complexity of different primality tests
module Main where
import Criterion.Main
-- We're going to use this constant as a known prime to test our functions with.
testPrime :: Int
testPrime = 16127
-- And a known non-prime as well.
testNonPrime :: Int
Build LeapC of project LeapC with configuration Debug
CompileC build/LeapC.build/Debug/LeapC.build/Objects-normal/x86_64/leap_controller.o ../src/leap_controller.cpp normal x86_64 c++ com.apple.compilers.gcc.4_2
cd /Users/tom/Documents/LeapC/xcode
setenv LANG en_US.US-ASCII
/Developer/usr/bin/gcc-4.2 -x c++ -arch x86_64 -fmessage-length=0 -pipe -Wno-trigraphs -fpascal-strings -fasm-blocks -O0 -Wreturn-type -Wunused-variable -Wuninitialized -Wshorten-64-to-32 -DDEBUG=1 -isysroot /Developer/SDKs/MacOSX10.6.sdk -fvisibility-inlines-hidden -mmacosx-version-min=10.6 -gdwarf-2 -iquote /Users/tom/Documents/LeapC/xcode/build/LeapC.build/Debug/LeapC.build/LeapC-generated-files.hmap -I/Users/tom/Documents/LeapC/xcode/build/LeapC.build/Debug/LeapC.build/LeapC-own-target-headers.hmap -I/Users/tom/Documents/LeapC/xcode/build/LeapC.build/Debug/LeapC.build/LeapC-all-target-headers.hmap -iquote /Users/tom/Documents/LeapC/xcode/build/LeapC.build/Debug/LeapC.build/LeapC-project-headers.hmap -F/Users/tom/Documents/LeapC/xcode
test :: Int -> [Int]
test f
| f <= 0 = []
| otherwise = f : test (f-1)