Skip to content

Instantly share code, notes, and snippets.

@jrraymond
jrraymond / reservoir.hs
Created April 6, 2014 06:56
Picks a random number from a stream with uniform probability. O(1) space.
import System.Random
f :: [a] -> IO a
f xs = f' xs [] 1 where
f' :: [a] -> [a] -> Int -> IO a
f' [] z _ = return $ head z
f' (x:xs) z i = do r <- randomRIO (1,i)
if r == 1
then f' xs [x] (i + 1)
else f' xs z (i + 1)
@jrraymond
jrraymond / anagram.hs
Last active August 29, 2015 13:58
Tests whether two strings are anagrams in O(n) time.
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Foldable
isAnagram :: [Char] -> [Char] -> Bool
isAnagram xs ys = f xs ys Map.empty
where
f :: [Char] -> [Char] -> Map Char Int -> Bool
f (x:xs) ys m = if x `Map.member` m
@jrraymond
jrraymond / prepend.sh
Created April 4, 2014 22:49
Prepends text to files in a directory
#!/bin/bash
JSCSS=$'/* Copyright (C) 2014 Wesleyan University
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
@jrraymond
jrraymond / .jshintrc
Last active February 5, 2017 04:10
A starter JSHint jshintrc file with all options from the [JSHint Options Reference](http://www.jshint.com/docs/options/) list. Just copy into home/project folder and customize.
{
//Enforcing options
"bitwise" : true, // no bitwise operators
"camelcase" : true, // variable names camelCase or UPPER_CASE
"curly" : true, // curly braces around blocks
"eqeqeq" : true, // no == and !=
"es3" : false, // ECMAScript 3 specification
"forin" : true, // for in loops must filter object's items
"freeze" : false, // prevent overwriting native objects
"immed" : true, // immediate function invocations must be wrapped in ()