This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -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). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
OlderNewer