Skip to content

Instantly share code, notes, and snippets.

@ithayer
ithayer / hoisting-loop.js
Created April 4, 2012 05:33
Simple example of Javascript variable hoisting
// Demonstration of how easy it is for this to mess up your loops.
var txt = ["a","b","c"];
for (var i = 0; i < 3; ++i ) {
var msg = txt[i];
setTimeout(function() { alert(msg); }, i*1000);
}​
// Alerts 'c', 'c', 'c'
# Should enable the little battery icon.
xfce4-power-manager
echo "Setting middle button emulation"
xinput set-int-prop 12 "Evdev Middle Button Emulation" 8 1
echo "Turning off dual mode video"
sudo sh -c "echo OFF > /sys/kernel/debug/vgaswitcheroo/switch"
echo "Turhing off bluetooth"
@ithayer
ithayer / clojure-flow-1.clj
Created July 28, 2012 21:31
Clojure Thrush Operator Code Examples
;; Start of what the above flow in clojure would look like.
;; Tokenization step.
(->> (clojure.string/split (slurp "/tmp/X") #" ") tokenize-word)
;; For debugging, just look at the first 100 words...
(->> (clojure.string/split (slurp "/tmp/X") #" ") tokenize-word (take 100))
;; or, look at some in the middle...
(->> (clojure.string/split (slurp "/tmp/X") #" ")
@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
;; 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
;; 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
;; 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]
;; 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
;; 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})
;; 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.