Skip to content

Instantly share code, notes, and snippets.

@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 / occurrences.scala
Last active May 8, 2016 10:08
Scala: Occurrences with pairs, Map and foldLeft
/**
* This function computes for each unique character in the list `chars` the number of
* times it occurs. times(List('a', 'b', 'a')) should return List(('a', 2), ('b', 1))
*/
def howManyTimes(chars: List[Char]): List[(Char, Int)] = {
def incr(acc:Map[Char, Int], c:Char) = {
val count = (acc get c).getOrElse(0) + 1 // If acc get c is none getOrElse makes it 0
// To add values to the Map we use operator +
// If key does not exist, a new (key, value) is inserted, otherwise substitution occurs
acc + ((c, count)) // I Double brakets tell scala we're dealing with a pair
@hanbzu
hanbzu / occurrences2.scala
Last active December 28, 2016 23:54
Scala: Occurrences with pairs, groupBy and map
/**
* 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)] = {
val grouped = w.groupBy(x => x.toLower) // Group by element (all chars to lowercase)
grouped.map(x => (x._1, x._2.length)).toList // Create pair for each element
}
@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);
};
@hanbzu
hanbzu / prop.js
Created October 30, 2013 15:28
JavaScript: Prop: Property of. Thanks to Christian Johansen.
// If I define the 'prop' function
function prop(name) {
return function (object) {
return object[name];
};
}
// Instead of this
var str = "Mentioned by " + tweeps.map(function (t) {
return t.name;