Skip to content

Instantly share code, notes, and snippets.

View daithiocrualaoich's full-sized avatar

Daithi O Crualaoich daithiocrualaoich

View GitHub Profile
@daithiocrualaoich
daithiocrualaoich / gist:1189097
Created September 2, 2011 16:37
Scala Collections Pimp to add distinctBy(hash: ... => ...) for hash filterings
implicit def seq2Distinct[T, C[T] <: Seq[T]](tees: C[T]) = new {
import collection.generic.CanBuildFrom
import collection.mutable.{HashSet => MutableHashSet}
def distinctBy[S](hash: T => S)(implicit cbf: CanBuildFrom[C[T],T,C[T]]): C[T] = {
val builder = cbf()
val seen = MutableHashSet[S]()
for (t <- tees) {
if (!seen(hash(t))) {
@daithiocrualaoich
daithiocrualaoich / intersect.scala
Last active December 21, 2015 10:48
Intersection for ordered lists.
def intersect(ens: List[Int], ems: List[Int]): List[Int] = (ens, ems) match {
case (Nil, _) => Nil
case (_, Nil) => Nil
case (en :: ensTail, em :: emsTail) if en == em => en :: intersect(ensTail, emsTail)
case (en :: ensTail, em :: _) if en < em => intersect(ensTail dropWhile { _ < em }, ems)
case (en :: _, em :: emsTail) => intersect(ens, emsTail dropWhile { _ < en })
}
intersect(List(1, 2, 3, 4), List(3, 4, 5, 6)) // = List(3, 4)
intersect(List(3, 4, 5, 6), List(1, 2, 3, 4)) // = List(3, 4)
package com.gu.scoobi
import com.nicta.scoobi.Scoobi._
object ScoobiRemoteCalculation extends ScoobiApp {
def run() {
val test = onHadoop {
scala.math.pow(2, 16)
}
@daithiocrualaoich
daithiocrualaoich / build.sbt
Created December 19, 2012 10:17
Scoobi `build.sbt` with the Avro snapshot fixed.
name := "Scoobi"
version := "1-SNAPSHOT"
scalaVersion := "2.9.2"
scalacOptions ++= Seq("-Ydependent-method-types", "-deprecation")
libraryDependencies += "com.nicta" %% "scoobi" % "0.6.0-cdh3-RC2"
@daithiocrualaoich
daithiocrualaoich / ApplicationParameters.scala
Created December 18, 2012 16:03
Using the Hadoop Java API from Scala.
package com.gu.hadoop
import org.apache.hadoop.util.GenericOptionsParser
object ApplicationParameters {
def apply(args: Array[String]): List[String] = {
new GenericOptionsParser(args).getRemainingArgs.toList
}
}
@daithiocrualaoich
daithiocrualaoich / gist:4269195
Created December 12, 2012 16:20
Circular impress.js slide layout in Javascript
var steps = document.getElementsByClassName("step");
var r = 2000;
for (var i = 0; i < steps.length; i++) {
var theta = -i/(steps.length-1) * 2 * Math.PI;
var x = r * Math.cos(theta);
var y = r * Math.sin(theta);
var rotation = theta/(2*Math.PI) * 360 - 90;
steps[i].setAttribute("data-x", Math.round(x).toString());
@daithiocrualaoich
daithiocrualaoich / gist:1994249
Created March 7, 2012 16:36
Scala List MultiMaps
trait ListMultiMaps {
type ListMultiMap[A, B] = Map[A, List[B]]
implicit def listMultiMap2ListMultiMapOperations[A, B](map: ListMultiMap[A, B]) = new ListMultiMapOperations(map)
object ListMultiMap {
def apply[A, B](): ListMultiMap[A, B] = Map[A, List[B]]()
def apply[A, B](kvs: List[(A, B)]): ListMultiMap[A, B] = {
var rst = apply[A, B]()
kvs foreach { kv =>
@daithiocrualaoich
daithiocrualaoich / gist:1760314
Created February 7, 2012 15:43
URL parameter parsing
val Param = """(.+)=(.+)""".r
def params(parameters: String): Map[String, String] = {
(parameters split "\\?") match {
case Array(path, queryString) =>
val kvs = (queryString split "&").toList collect {
case Param(key, value) => key -> value
}
kvs.toMap
@daithiocrualaoich
daithiocrualaoich / gist:1555284
Created January 3, 2012 15:10
.option on nullable types
implicit def nullable2Option[N >: Null](n: N) = new {
lazy val option: Option[N] = Option(n)
}
val s1: String = "1234"
s1.option // Option[String] = Some(1234)
val s2: String = null
s2.option // Option[String] = None
autoCompilerPlugins := true
addCompilerPlugin("org.scala-lang.plugins" % "continuations" % "2.9.1")
scalacOptions += "-P:continuations:enable"