Skip to content

Instantly share code, notes, and snippets.

@mkscrg
mkscrg / README.md
Last active August 29, 2015 13:57
Reservoir Sampling

Choose a random sample of 10 values from an unbounded stream. Red marks denote values currently in the sample.

From Wikipedia:

The algorithm creates a "reservoir" array of size k and populates it with the first k items of S. It then iterates through the remaining elements of S until S is exhausted. At the ith element of S, the algorithm generates a random number j between 1 and i. If j is less than k, the jth element of the reservoir array is replaced

{-# OPTIONS_GHC -Wall #-}
{-# LANGUAGE DataKinds, GADTs, TypeOperators #-}
{-# LANGUAGE FlexibleContexts, FlexibleInstances, ScopedTypeVariables #-}
{-# LANGUAGE MultiParamTypeClasses, TypeFamilies #-}
module Main where
main :: IO ()
main = do
print aList
print elt0
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
@mkscrg
mkscrg / Deep.hs
Last active August 29, 2015 14:23
Aeson + REPL
{-# LANGUAGE OverloadedStrings #-}
module Deep where
import Control.Monad
import Data.Aeson
import Data.Aeson.Types
import Data.ByteString.Lazy
import Data.Text
@mkscrg
mkscrg / README.md
Last active August 29, 2015 14:24
PhantomJS + es5-shim + Immutable.js
$ npm install
$ npm start
@mkscrg
mkscrg / gist:725879
Created December 2, 2010 19:18
A test Gist! (Some Python functions playing with deterministic finite automata.)
"""
Arguments are the parameters of a deterministic finite automaton:
q -- states (list of strings)
si -- input alphabet (list of characters)
de -- transition function ((index of q),(index of si) --> (index of q))
s -- start state (index of q)
f -- accept states (list of (index of q))
Returns a generator function which takes a string and generates successive
states (indices of q) as the DFA runs on the string.
"""
[
{
"tags": [
"Others",
"Tech startups",
"Homes",
"Offices",
"Corporate office",
"Work",
"Services",
[
{
"tags": [
"Japanese restaurants",
"Restaurant",
"Sushi",
"Bar",
"Food",
"Food & drink",
"Japanese restaurant"
[
{
"type": "event",
"status": "active",
"image_url": null,
"expiration_date": "2011-10-01T13:00:00-07:00",
"place": {
"name": "Nelson Blue",
"lon": -74.001573,
"city": "New York",
{
"type": "event",
"status": "active",
"image_url": "http://s3.amazonaws.com/prestigedevelopment/beta/offer_photos/4e5f502fceb01f000100139f.jpg?1314869295",
"place": {
"name": "Chelsea Piers - Pier 62",
"lon": -74.005973,
"city": "New York",
"address": ", ",
"postal_code": "",