Skip to content

Instantly share code, notes, and snippets.

View awagner83's full-sized avatar

Adam Wagner awagner83

View GitHub Profile
from random import randint
from sortedcontainers import SortedList
def rollingMedian(xs):
prior_values = SortedList()
for x in xs:
if len(prior_values):
yield prior_values[len(prior_values)//2]
else:
_ = require 'underscore'
# Pure functional (with foldl and filter)
countZeroes1 = (xs) -> count1 (_.filter xs, (x) -> x == 0)
count1 = (xs) -> _.foldl xs, ((l, r) -> l + 1), 0
# Pure function (with list length -- usually available in function setting)
countZeroes2 = (xs) -> (_.filter xs, (x) -> x == 0).length
# Underscore 'chain'... has some functional aspects to it
@awagner83
awagner83 / function_functions.coffee
Created January 9, 2013 21:05
Example of function producing functions.
mkLoader = (thingToLoad) ->
-> console.log "loading #{thingToLoad}"
console.log 'create loaders'
loaderA = mkLoader 'foo'
loaderB = mkLoader 'bar'
console.log 'begin loading'
loaderA()
loaderB()
@awagner83
awagner83 / Maybe.lhs
Created August 4, 2012 03:19
Maybe's not so bad after all.
A lonely import (for the very end)
> import Control.Applicative
Given some association list (like a map, but slower)
> myMap :: [(String, Integer)]
> myMap = [("Small", 1), ("Big", 2)]
And a function that deals with two numbers
@awagner83
awagner83 / perhaps.hs
Created July 24, 2012 04:02
Maybe Haskell
data Perhaps a = Definitely a | NoWay deriving Show
perhapsMap :: (a -> b) -> Perhaps a -> Perhaps b
perhapsMap f (Definitely x) = Definitely (f x)
perhapsMap f NoWay = NoWay
main = do
print $ perhapsMap (+2) (Definitely 10)
print $ perhapsMap (+2) (NoWay)
@awagner83
awagner83 / Main.java
Created July 24, 2012 02:37
Maybe Java?
import maybe.Maybe;
import maybe.Mapper;
public class Main {
public static void main(String[] args) {
Mapper<Integer> addTwo = new Mapper<Integer>() {
public Integer map(Integer value) {
@awagner83
awagner83 / app.coffee
Created July 19, 2012 19:56
Facebook & backbone (again, but more event-driven)
CoverPhoto = Backbone.Model.extend
urlRoot: "https://graph.facebook.com/"
Album = Backbone.Model.extend {}
AlbumList = Backbone.Collection.extend
url: "https://graph.facebook.com/203771702995922/albums"
model: Album
parse: (response) -> response['data']
@awagner83
awagner83 / app.js
Created July 19, 2012 14:00
Broken-up backbone test
$(function() {
var Album, AlbumList, AlbumView, Albums, App, AppView;
Album = Backbone.Model.extend({
defaults: function() {
return {
source: null,
name: 'Unnamed Album'
};
}
@awagner83
awagner83 / index.html
Created July 16, 2012 19:25
CoffeeScript Translation of js in https://gist.github.com/3106865
<!DOCTYPE html>
<html>
<head>
<title>Backbone with Mighty Moo Photos</title>
</head>
<body>
<ul id="albums-list">
</ul>
<ul id="friends-list">
@awagner83
awagner83 / primes.hs
Created July 3, 2012 12:11
Lazy Primes
ldp :: Integer -> Integer
ldp = ldpf primes1
ldpf :: [Integer] -> Integer -> Integer
ldpf (p:ps) n | rem n p == 0 = p
| p^2 > n = n
| otherwise = ldpf ps n
primes1 :: [Integer]
primes1 = 2 : filter prime [3..]