Skip to content

Instantly share code, notes, and snippets.

@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 ()
@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 / 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 / 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 / wescraper.py
Last active August 29, 2015 14:02
wescraper
import time, string
from scrapy.spider import BaseSpider
from scrapy.http import FormRequest
from scrapy.item import Item, Field
from scrapy.selector import HtmlXPathSelector
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
{-# LANGUAGE FlexibleContexts #-}
{- Construct recursive descent parsers for the following grammars -}
import Text.Parsec
(<||>) :: ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
p <||> q = try p <|> q
{- S -> + S S | - S S | a -}
data G1 = P1G1 G1 G1 | P2G1 G1 G1 | T1G1 deriving (Eq, Show)
g1 :: Stream s m Char => ParsecT s u m G1
@jrraymond
jrraymond / colorPixel1.hs
Last active August 29, 2015 14:26
vectors_conundrum
type Point = (Float,Float)
type GridU = U.Vector Point
{- Explanation: colorPixel evaluates to the color of a pixel for a given world
- Antialiasing is tracing > 1 rays through each pixel and averaging the results.
- Soft shadows are shadows that do not have a hard edge, this requires sending multiple shadow rays to each light.
- Depth of field: only objects at a certain distance are in focus. To do this we send multiple rays though each antialising
- point with each base perturbed slightly. Objects are in focus if they are at the distance the view plane is at.
-
- Three arguments:
total time = 132.70 secs (132702 ticks @ 1000 us, 1 processor)
run.img Main 427 1 0.0 0.0 98.2 97.6
render RayTracer 428 1 0.0 0.0 98.2 97.6
render.cs RayTracer 585 1 98.1 97.6 98.2 97.6
compare RayTracer 639 3971855 0.0 0.0 0.0 0.0
== Geometry3 638 1676386 0.0 0.0 0.0 0.0
wLights RayTracer 637 1871708 0.0 0.0 0.0 0.0
wAmbient RayTracer 636 1871708 0.0 0.0 0.0 0.0
wImgHt RayTracer 633 3840000 0.0 0.0 0.0 0.0
@jrraymond
jrraymond / quickSelects.scala
Created October 22, 2016 19:41
quickSelect
import org.scalacheck.Properties
import org.scalacheck.Prop.{forAll, BooleanOperators}
import scala.util.Random
def quickSelect[A <% Ordered[A]](seq: Seq[A], n: Int, rand: Random = new Random): A = {
val pivot = rand.nextInt(seq.length);
val (left, right) = seq.partition(_ < seq(pivot))
if (left.length == n) {
seq(pivot)
} else if (left.length < n) {
shuffle.partitions=200
collect: 5s, 2 stages, 220 tasks
runJob: 8s, 6 stages, 660 tasks
shuffle.partitions=60
collect: 5s, 2stages, 80 tasks
runJob: 8s, 6 stages, 240 tasks
collect:
stage 1 remains the same, 5s, 20 tasks, 1909.2MB input, 48.1MB shuffle write