Skip to content

Instantly share code, notes, and snippets.

View cb372's full-sized avatar

Chris Birchall cb372

View GitHub Profile
@cb372
cb372 / fizzbuzz.scala
Created July 12, 2011 11:58
FizzBuzz code golf for Shibuya Scala meeting no 42 (http://atnd.org/events/17769)
for(i<-1 to 100){var m=0;if(i%3==0){m=1;print("Fizz")};if(i%5==0){m=1;print("Buzz")};if(m<1)print(i);println} // 110
@cb372
cb372 / codejam-small.scala
Created October 20, 2011 04:07
CodeJam 問題C. ビット数 first attempt
/*
* Ridiculous bit counting algorithm
* http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
*/
def f(x:Int):Int = {
var v = x - ((x >> 1) & 0x55555555)
v = (v & 0x33333333) + ((v >> 2) & 0x33333333)
(((v + (v >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24
}
@cb372
cb372 / codejam-large.scala
Created October 23, 2011 05:27
CodeJam 問題C. ビット数 second attempt
def calcAandB(N:BigInt):(BigInt,BigInt) = {
def setIth(i: Int, a:BigInt, b:BigInt, carry:Boolean):(BigInt, BigInt, Boolean) =
(N.bitLength-i, carry) match {
case (n,_) if n<1 => (a,b,carry) // finished all bits of N
case (1,true) => (a,b,carry) // finished all bit of N except msb, and there is no carry
case (n,_) => { // recurse
// handle the carry
val (n_i, c) = (N.testBit(i), carry) match {
case (true, true) => (false, false)
case (false, true) => (true, true)
[info] Starting scala interpreter...
[info]
Welcome to Scala version 2.10.0.r26005-b20111114020239 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_26).
Type in expressions to have them evaluated.
Type :help for more information.
scala> val a = 1
missing argument
bad option: '-bootclasspath'
a: Int = 1
@cb372
cb372 / gist:1778448
Created February 9, 2012 08:27
Time until the musical kills Andy :(
scala> def step(an: Int, xn: Double): Stream[(Int, Double)] = Stream.cons((an, xn), step(an + 1, xn - (0.2 * 365)))
step: (an: Int, xn: Double)Stream[(Int, Double)]
scala> val ageNow = 28
ageNow: Int = 28
scala> val lifeExpectancy = 80
lifeExpectancy: Int = 80
scala> val marchOfTime = step(ageNow * 365, lifeExpectancy * 365)
@cb372
cb372 / gist:1872753
Created February 21, 2012 01:16
Scoobi sbt compile OOM error
[chris@birchall-work 1250 ~/code/mapreduce-sandbox]
: git clone https://github.com/NICTA/scoobi.git
Initialized empty Git repository in /home/chris/code/mapreduce-sandbox/scoobi/.git/
remote: Counting objects: 3955, done.
remote: Compressing objects: 100% (1840/1840), done.
remote: Total 3955 (delta 1608), reused 3710 (delta 1375)
Receiving objects: 100% (3955/3955), 14.39 MiB | 2.15 MiB/s, done.
Resolving deltas: 100% (1608/1608), done.
@cb372
cb372 / gist:1872775
Created February 21, 2012 01:21
Scoobi sbt compile OOM error - sbt log
[debug]
[debug] Initial source changes:
[debug] removed:Set()
[debug] added: Set(/home/chris/code/mapreduce-sandbox/scoobi/src/main/scala/com/nicta/scoobi/impl/exec/MscrReducer.scala, /home/chris/code/mapreduce-sandbox/scoobi/src/main/java/com/nicta/scoobij/DTable.java, /home/chris/code/mapreduce-sandbox/scoobi/src/main/scala/com/nicta/scoobi/io/InputStore.scala, /home/chris/code/mapreduce-sandbox/scoobi/src/main/scala/com/nicta/scoobi/impl/plan/Smart.scala, /home/chris/code/mapreduce-sandbox/scoobi/src/main/scala/com/nicta/scoobi/impl/exec/MscrMapper.scala, /home/chris/code/mapreduce-sandbox/scoobi/src/main/java/com/nicta/scoobij/Ordering.java, /home/chris/code/mapreduce-sandbox/scoobi/src/main/java/com/nicta/scoobij/impl/DGroupedTableImpl.java, /home/chris/code/mapreduce-sandbox/scoobi/src/main/scala/com/nicta/scoobi/io/Helper.scala, /home/chris/code/mapreduce-sandbox/scoobi/src/main/scala/com/nicta/scoobi/impl/exec/TaggedMapper.scala, /home/chris/code/mapreduce-sandbox/scoobi/src/main/java/
@cb372
cb372 / MyTest.java
Created February 23, 2012 00:49
Maven + JUnit example
// put this file in src/test/java/foo/
package foo;
import org.junit.Test;
import org.junit.Assert;
import static org.junit.Assert.assertTrue;
public class MyTest {
@cb372
cb372 / hide-promoted-tweets.user.js
Created March 24, 2012 08:06
A simple userscript to hide promoted tweets on twitter.com
// ==UserScript==
// @match http://twitter.com/*
// @match https://twitter.com/*
// ==/UserScript==
elems = document.getElementsByClassName('tweet')
for (var i=0; i<elems.length; i++) {
e = elems[i];
if (e.nodeName.toLowerCase() == 'div' &&
e.attributes['data-promoted'] &&
e.attributes['data-promoted'].value == "true") {
@cb372
cb372 / ApacheCommonExec.java
Created March 28, 2012 07:37 — forked from sakamotodesu/ApacheCommonExec.java
apache commons exec "write end dead" sample
package com.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecuteResultHandler;
import org.apache.commons.exec.DefaultExecutor;