Skip to content

Instantly share code, notes, and snippets.

View weskerfoot's full-sized avatar

Wesley Kerfoot weskerfoot

View GitHub Profile
#lang racket
;; hello world in Racket
;; comments begin with ;
;; some people like to use ;;
;; function application is done by surrounding the function name and arguments in parens
;;
;; if you have seen languages like Python or JavaScript, f(a,b,c) is (f a b c) in scheme
(displayln "Hello, world!")
@weskerfoot
weskerfoot / timeout.py
Created August 12, 2014 22:47
make a function time out after a certain number of calls in a period of time
down vote
import time
def timeout(f, k, n):
last_time = [time.time()]
count = [0]
def inner(*args, **kwargs):
distance = time.time() - last_time[0]
import qualified Prelude
import Prelude hiding (show)
class Show a where
show :: a -> Text
instance Show a => Show [a] where
show xs = append ("[", append ((showList xs), "]")) where
showList (x:[]) = show x
function offile() {
IFS='';
(while read -r line
do
file "$line" 2> /dev/null;
done <<< $(/usr/bin/ls)) | grep "$1" | tr -s ' ';
}
#lol gvim -p $(offile ASCII | cut -d ':' -f 1 | sed ':a;N;$!ba;s/\n/ /g')
addn = (n) ->
() ->
n = n + 1
n
add1 = addn 1
console.log(add1())
console.log(add1())
console.log(add1())
console.log(add1())
@weskerfoot
weskerfoot / sbphi.hs
Created October 17, 2012 23:42
Fun with Stern-Brocot tree
import Control.Monad
import Data.Ratio
data FakeRatio = FakeRatio { numr :: Int, denom :: Int} deriving (Show)
data SBTriple = SBTriple { left :: FakeRatio,
mid :: FakeRatio,
right :: FakeRatio}
data Directions = TLeft | TRight deriving (Show)
@weskerfoot
weskerfoot / lsystem.hs
Created October 20, 2012 00:41
L-System
import Control.Monad
data LSymbol = LRule Char | LDeriv String
type Alphabet = [LSymbol]
type Axiom = [LSymbol]
-- a production is a finite mapping of LSymbol -> LSymbol
-- if no production exists for a given LSymbol on the LHS of a Production
from math import log
def calcShannonEnt(dataSet):
numEntries = len(dataSet)
labelCounts = {}
for featVec in dataSet:
currentLabel = featVec[-1]
if currentLabel not in labelCounts.keys():
labelCounts[currentLabel] = 0
else:
@weskerfoot
weskerfoot / perms.hs
Created November 1, 2012 03:48
Haskell - permutations
import Control.Monad
enumerate xs = zip [1..] xs
dlists :: [a] -> [(a, [a])]
dlists xs = [(x, [y | (j, y) <- enumerate xs, j /= i]) | (i, x) <- enumerate xs]
permute (x:y:[]) = [y:x:[], x:y:[]]
permute xs = let difflist = dlists xs
in join [map (i:) (permute d) | (i, d) <- difflist]
@weskerfoot
weskerfoot / lgraphs.hs
Created November 28, 2012 21:40
lsysgraphics.hs
{-# LANGUAGE GADTs, FlexibleInstances #-}
import Graphics.Gloss
import Graphics.Gloss.Data.Vector
import LSystem
data PlantInstruction a where
DrawForward :: PlantInstruction a
TurnLeft :: (Show a, Num a) => a -> PlantInstruction a
TurnRight :: (Show a, Num a) => a -> PlantInstruction a