Skip to content

Instantly share code, notes, and snippets.

@hanbzu
hanbzu / _.md
Created January 30, 2014 10:05
bub
gistup
@hanbzu
hanbzu / README.md
Last active August 29, 2015 13:57
Exponential water tank

This example aims to demonstrate our inability to fully grasp [exponential functions][wikg]. As [Albert Bartlett][barl] once said, "The greatest shortcoming of the human race is our inability to understand the exponential function." This little D3 animation is based on a paper by Dr Bartlett.

Our action hero, a pixelated version of [Chris Martenson][chrs], stands on a platform inside an empty 4000 litre water tank. At the very bottom of the tank lies a magic drop of water. Invisible to the eye now, it doubles in size every 10 seconds.

Although the growth rate is constant, for a long time we see no change. But there's a well known limit, the capacity of the tank. Once he realises that water is rising exponentially, poor pixellated Chris has no time left to react.

Our brains are wired to predict future behaviour based on past behaviour (see [here][psyc]). But what happens when something growths exponentially? For a long time, the numbers are so little in relation to the scale that we hardly see the ch

@hanbzu
hanbzu / protost.html
Created September 5, 2011 15:38
Brief demonstrator of a rail s-t graph (a timetable mesh)
<!DOCTYPE html>
<html>
<head>
<script class="jsbin" src="http://cdnjs.cloudflare.com/ajax/libs/raphael/1.5.2/raphael-min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style>
@hanbzu
hanbzu / ranalysis.Rmd
Last active December 24, 2015 21:19
A reference card of useful R and data analysis (with R) snippets. I used the Rmd extension, which R studio compiles into a didactic visual sheet with the outcomes of each command.
Data analysis with R -- It's better and better now
==================================================
A nice R resource, the “cookbook for R”: http://wiki.stdout.org/rcookbook/
Installing
----------
```{r installing}
# For Linux: Install package directly from CRAN accessible for all users
@hanbzu
hanbzu / pairs.scala
Last active December 26, 2015 18:19
Scala: pairs
// Pairs can be constructed easily using parentheses:
val pair: (Char, Int) = ('c', 1)
// In order to access the two elements of a pair, you can use the accessors `_1` and `_2`:
val theChar = pair._1
val theInt = pair._2
// Another way to deconstruct a pair is using pattern matching:
pair match {
case (theChar, theInt) =>
@hanbzu
hanbzu / rmwhitespace.java
Created October 28, 2013 12:25
Java: Remove whitespace
// Remove all whitespace from String
"This is a string".replaceAll("\\s+","")
@hanbzu
hanbzu / mkstrconcat.scala
Last active December 26, 2015 18:39
Scala: Concatenate Lists of Strings with mkString
// A list of strings into a single string
val food = "rice" :: "cookies" :: "watermelon" :: Nil
food.foldLeft("")(_ + ", " + _)
// But we would have to remove the first two chars:
// ", rice, cookies, watermelon"
// Now with mkString
val fruits = "apple" :: "orange" :: "kiwi" :: Nil
fruits mkString ", "
@hanbzu
hanbzu / occurrences3.scala
Created October 29, 2013 07:49
Scala: Occurrences with pairs, groupBy and mapValues
/**
* For each unique character in the list `chars`, it calculates the number of
* times it occurs. times(List('a', 'b', 'a')) should return List(('a', 2), ('b', 1))
*/
def howManyTimes(w: String): List[(Char, Int)] = { // w = "Wooord"
val grouped = w.groupBy(x => x.toLower) // Map(d -> d, w -> W, r -> r, o -> ooo)
val occ = grouped.mapValues(_.length) // Map(d -> 1, w -> 1, r -> 1, o -> 3)
occ.toList // List((d,1), (w,1), (r,1), (o,3))
}
@hanbzu
hanbzu / functional_javascript.js
Last active December 26, 2015 23:59
JavaScript: Functional aspects of the language. Based on Christian Johansen's http://cjohansen.no/talks/2012/javazone/#/ talk.
// A simple function
function add(a, b) {
console.log(a + b);
}
// A first class function
// It can be passed along as a variable
var add = function (a, b) {
console.log(a + b);
};