Skip to content

Instantly share code, notes, and snippets.

View joelgrus's full-sized avatar
🤷‍♀️
I'm probably arguing on Twitter

Joel Grus joelgrus

🤷‍♀️
I'm probably arguing on Twitter
View GitHub Profile
module Comparison
type Comparison = LessThan | Equal | GreaterThan
module Natural
type Natural = One | SuccessorOf of Natural
let Two = SuccessorOf One
let Three = SuccessorOf Two
let Four = SuccessorOf Three
// and so on
// given two natural numbers, either they're *equal*, or one is *greater than* the other
@joelgrus
joelgrus / lampsort
Last active August 29, 2015 14:10
non-recursive quicksort, based on http://bertrandmeyer.com/2014/12/07/lampsort/
from collections import deque
def partition(a, lo, hi):
"""partition a[lo] to a[hi] using a[hi] as the pivot"""
pivot_value = a[hi]
insert_at = lo
for i in range(lo, hi):
if a[i] < pivot_value:
a[i], a[insert_at] = a[insert_at], a[i]
insert_at += 1
@joelgrus
joelgrus / simple_regression_batch_gradient_descent
Created June 10, 2015 03:23
simple regression using batch gradient descent
def target_fn(theta):
"""want to minimize squared errors as a function of *theta*, so we hardcode in the data"""
alpha, beta = theta
return sum_of_squared_errors(alpha, beta, num_friends_good, daily_minutes_good)
def gradient_fn(theta):
"""similarly, need gradient as a function of *theta*, so hardcode in the data"""
alpha, beta = theta
result = [0, 0]
for x_i, y_i in zip(num_friends_good, daily_minutes_good):
@joelgrus
joelgrus / switch.py
Last active August 29, 2015 14:25
stealing an idea from trenthauck
# got the idea from https://gist.github.com/tshauck/6ebb0c2d6adc667608b8
def switch(*args, default=None):
try:
return next(value for cond, value in args if cond)
except StopIteration:
return default
x = 1
@joelgrus
joelgrus / monopoly.py
Last active October 4, 2015 15:08
semi-functional rewrite of Norvig monopoly simulator to avoid global state
# Rewrite of Norvig's monopoly simulator to avoid global state
# except for `board`, which is used immutably.
# http://nbviewer.ipython.org/url/norvig.com/ipython/Probability.ipynb
import random
split = str.split
# The board: a list of the names of the 40 squares
board = split("""GO A1 CC1 A2 T1 R1 B1 CH1 B2 B3
JAIL C1 U1 C2 C3 R2 D1 CC2 D2 D3
@joelgrus
joelgrus / state.js
Last active January 24, 2016 15:15
sort-of explicit state in cycle.js
const State = Immutable.Record({count:0});
const state$ = action$.startWith(new State())
.scan((state, delta) => state.updateIn(['count'], count => count + delta));
const vtree$ = state$.map(...)
@joelgrus
joelgrus / nonrandom.purs
Last active March 22, 2016 17:17
trying to sequence effects in purescript-flare
width :: Number
width = 600.0
height :: Number
height = 600.0
-- Generate a Drawing of n deterministic points
nonRandomPoints :: Int -> Drawing
nonRandomPoints n = foldMap makeCircle points
where
module Main where
import Prelude (Unit)
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)
import Debug.Trace (trace)
import Pux (start, renderToDOM, EffModel(), App)
import Prelude ((+), (-), const, show, (++), pure, ($), (>>=))
import Pux.Html (Html, (!), (#), bind, div, span, button, text)
import Pux.Html.Events (onClick)
@joelgrus
joelgrus / counting.py
Last active November 27, 2016 16:46
choose your collections wisely
"""
how to count as fast as possible
(numbers from Python 3.5.2 on a Macbook Pro)
YMMV, but these results are pretty stable for me, say +/- 0.1s on repeated runs
"""
from collections import Counter, defaultdict
import random
random_numbers = [random.randrange(10000) for _ in range(10000000)]