Skip to content

Instantly share code, notes, and snippets.

View davepkennedy's full-sized avatar

Dave Kennedy davepkennedy

View GitHub Profile
@davepkennedy
davepkennedy / euler1.erl
Created June 6, 2011 11:30
Euler1, Erlang style
-module(euler1).
-export([sum/1]).
sum(0) -> 0;
sum(X) when X rem 3 == 0 -> X + sum(X-1);
sum(X) when X rem 5 == 0 -> X + sum(X-1);
sum(X) -> sum(X-1).
@davepkennedy
davepkennedy / gist:1560769
Created January 4, 2012 16:20
jetty config
<build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.26</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<stopKey>foo</stopKey>
@davepkennedy
davepkennedy / heapsort
Created January 9, 2014 13:55
Heap sort in Scala
object heapsort {
def left(i: Int) = (2*i) + 1 //> left: (i: Int)Int
def right(i: Int) = left(i) + 1 //> right: (i: Int)Int
def parent(i: Int) = (i - 1) / 2 //> parent: (i: Int)Int
def swap(a: Array[Int], i: Int, j: Int): Unit = {
val t = a(i)
a(i) = a(j)
a(j) = t
} //> swap: (a: Array[Int], i: Int, j: Int)Unit
@davepkennedy
davepkennedy / gist:8433906
Created January 15, 2014 10:19
Number to text (not quite perfect)
object numbertext {
val singleDigits = Array("zero", "one","two","three","four","five","six","seven","eight","nine")
//> singleDigits : Array[String] = Array(zero, one, two, three, four, five, six
//| , seven, eight, nine)
val teens = Array("ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen")
//> teens : Array[String] = Array(ten, eleven, twelve, thirteen, fourteen, fift
//| een, sixteen, seventeen, eighteen, nineteen)
val tens = Array ("","","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety")
//> tens : Array[String] = Array("", "", twenty, thirty, fourty, fifty, sixty,
//| seventy, eighty, ninety)
@davepkennedy
davepkennedy / gist:8439399
Created January 15, 2014 16:30
Max factor of the permutations of a list
object permutations {
def insertAt[T](e: T, p: Int, l: List[T]): List[T] = p match {
case 0 => e :: l
case _ => l.head :: insertAt(e, p-1, l.tail)
} //> insertAt: [T](e: T, p: Int, l: List[T])List[T]
def permute[T] (l: List[T]): List[List[T]] = l match {
case Nil => Nil
case h :: t =>
println(h, t)
Latency Comparison Numbers
--------------------------
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns
Send 1K bytes over 1 Gbps network 10,000 ns 0.01 ms
Read 4K randomly from SSD* 150,000 ns 0.15 ms
@davepkennedy
davepkennedy / gist:10968405
Created April 17, 2014 09:29
Tailrec compliant Fibonacci seq in Scala
def fib (count: Int): Int = {
@tailrec def _fib (count: Int, value: Int, accum: Int = 0): Int = count match {
case 0 => accum
case _ => _fib(count - 1, accum, value + accum)
}
_fib(count, 1)
}
@davepkennedy
davepkennedy / gist:dfd923cd2f03df1016fa
Created June 25, 2014 20:25
Adding two numbers without using +
def add (a: Int, b: Int): Int = {
@tailrec
def helper (c: Int, d: Int): Int = d match {
case 0 => c
case _ => helper (c ^ d, (c & d) << 1)
}
helper(a, b)
}
@davepkennedy
davepkennedy / App.java
Created July 1, 2014 11:26
SQLite + JDBI
public class App
{
private static final String JDBC_DRIVER = "org.sqlite.JDBC";
private static final String CONNECTION_STRING = "jdbc:sqlite:/tmp/testdb.db";
public static void main( String[] args ) throws SQLException, ClassNotFoundException {
SQLiteDataSource ds = new SQLiteDataSource();
ds.setUrl("jdbc:sqlite:/tmp/data.db");
DBI dbi = new DBI(ds);
Handle h = dbi.open();
@davepkennedy
davepkennedy / binarysearch
Created July 4, 2014 13:17
Binary search on a Seq
def search [T <% Ordered[T]] (s: Seq[T], elem: T): Boolean = {
var low = 0
var high = s.length - 1
while (low <= high) {
val mid = (low + high) / 2
if (s(mid) == elem) {return true}
if (elem < s(mid)) {high = mid-1}
if (elem > s(mid)) {low = mid+1}
}
false