ubilinux prerequisites
# for untar later
apt-get install bzip2
# probably all needed
apt-get install freeglut3 freeglut3-dev -y
# https://gist.github.com/oliversalzburg/a57a8b82dd8ec245f985 | |
function _common_section | |
printf $c1 | |
printf $argv[1] | |
printf $c0 | |
printf ":" | |
printf $c2 | |
printf $argv[2] | |
printf $argv[3] |
def fibonacci(number: Int): Int = { | |
assert(number >= 0) | |
@annotation.tailrec def inner(prev: Int, curr: Int, n: Int): Int = | |
if (n < number) inner(curr, prev + curr, n + 1) | |
else curr | |
if (number == 0) 0 | |
else if (number == 1) 1 | |
else inner(0, 1, 1) |
/* | |
run as a script using `scala flyweight.scala` | |
expected output: | |
Serving CoffeeFlavour(Espresso) to table 121 | |
Serving CoffeeFlavour(Cappuccino) to table 121 | |
Serving CoffeeFlavour(Frappe) to table 552 | |
Serving CoffeeFlavour(Espresso) to table 96 | |
Serving CoffeeFlavour(Cappuccino) to table 3 | |
Serving CoffeeFlavour(Espresso) to table 3 | |
Serving CoffeeFlavour(Frappe) to table 3 |
val rate = 200 millis | |
def throttled[T]: Flow[T, T] = { | |
val tickSource = TickSource(rate, rate, () => () ) | |
val zip = Zip[T, Unit] | |
val in = UndefinedSource[T] | |
val out = UndefinedSink[T] | |
PartialFlowGraph{ implicit builder => | |
import FlowGraphImplicits._ | |
in ~> zip.left ~> Flow[(T,Unit)].map{ case (t, _) => t } ~> out |
case class Trie(v: Option[String], m: Map[Char, Trie]) | |
def addInner(t: Trie, s: List[Char], p: String): Trie = s match { | |
case c::cs => Trie(t.v, t.m.updated(c, addInner(t.m.getOrElse(c, Trie(Some(p + c), Map.empty)), cs, p + c) )) | |
case Nil => t | |
} | |
def add(t: Trie, s: String): Trie = addInner(t, s.toList, "") | |
def findInner(t: Trie, s: List[Char]): Option[String] = s match { |
//todo: use vector, scala 2:10 generic extractors. Currently using 2.9, so List has better pattern matching support | |
type Row = List[Option[Int]] | |
def shiftR(r: Row): Row = r.foldRight(Nil: Row){ | |
case (None, acc) => acc | |
case (Some(x), Some(a)::as) if a == x => Some(a + x)::as | |
case (Some(x), acc) => Some(x)::acc | |
}.padTo(4, None) |