Skip to content

Instantly share code, notes, and snippets.

@ithayer
ithayer / reducers.clj
Last active August 29, 2015 14:05
Transducers Examples
;; Example of reducing fn that computes sufficient
;; statistics for a mean.
=> (defn mean-reducer [memo x]
(-> memo
(update-in [:sum] + x)
(update-in [:count] inc)))
=> (reduce mean-reducer {:sum 0 :count 0} (range 10))
{:count 10, :sum 45}
http://example.com/js/main__V0123456789abcdef__.js
@ithayer
ithayer / fix-format.py
Last active December 15, 2015 11:59
Simple python script to regex replace the contents of a file.
#!/usr/bin/env python
import os
import re
from optparse import OptionParser
parser = OptionParser()
(options, args) = parser.parse_args()
;; In project.clj
:plugins [[s3-wagon-private "1.1.2"]]
:repositories {"private" {:url "s3p://mybucket/releases/"
:passphrase :env
:username :env}}
;; Gotcha 1: Using :creds :env didn't seem to work for me.
;; Gotcha 2: Using a location of just s3p://mybucket didn't
;; seem to work, the releases/ directory is needed.
;; Babbage provides some built-in simple accumulators.
user> (use 'babbage.core)
user> (require '[babbage.provided.core :as p])
nil
;; Create several sets of interest.
user> (def mysets
(sets {:firefox? #(-> % :browser (= :firefox))
:chrome? #(-> % :browser (= :chrome))
:ie? #(-> % :browser #{:ie-7 :ie-8 :ie-9 :ie-10})
;; Update our 'browsers' function to print when it's run.
user> (defgraphfn browsers [raw-visitors]
(println "Running browsers on" (count raw-visitors) "visitors")
(map (fn [_] (rand-nth [:chrome :firefox :ie-9]) )
raw-visitors))
#'user/browsers
;; and run our computation graph lazily.
user> (defn all-visitors []
(run-graph-strategy
;; Create a function that given visitors, computes
;; the spend for each - here, just a random number.
user> (defgraphfn spends [raw-visitors]
(map (fn [_] (rand-int 10))
raw-visitors))
#'user/spends
;; Create a function that given visitors, computes the
;; browser for each - here, just randomly assigned.
user> (defgraphfn browsers [raw-visitors]
;; Simple mean function.
#> (defn compute-mean [xs] (/ (reduce + xs) (count xs)))
;; Check the average spend for firefox.
#> (->> (all-visitors)
(filter #(-> % :browser (= :firefox)))
(keep :spend)
compute-mean)
100.2
;; All the visitors to my site.
#> (->> (all-visitors) count)
1000
;; Which browsers?
#> (->> (all-visitors) (map :browser) frequencies)
{:ie-10 50 :ie-9 250 :ie-8 200 :firefox 140 :chrome 360}
;; Average spend?
#> (let [spent (->> (all-visitors) (keep :spend))]
(/ (reduce + spent) (count spent)))
123.12
@ithayer
ithayer / a.txt
Created October 1, 2012 04:31
Code for Minimal ClojureScript Project Post
;; Install dependencies
lein deps
;; Build my ClojureScript.
lein cljsbuild once
;; Start me listening on port 8888.
lein run