Skip to content

Instantly share code, notes, and snippets.

@leifwickland
leifwickland / ClassUtils.scala
Last active November 30, 2016 18:32
How to get a `Class[T]`
// Credit to aluizdinizsilva
object ClassUtils {
def tagToClass[C](tag: WeakTypeTag[C]) = Class.forName(tag.tpe.baseClasses.head.asClass.fullName).asInstanceOf[Class[C]]
object syntax extends ClassTagSyntax
}

Contrarian Tastes

Using Hadoop MapReduce, Apache Spark, or another distributed computing technology, analyze the Netflix Prize Dataset. (Click the "Download" link in the upper right corner, not the "uci.edu" URL near the bottom of the page.) The README in that file describes the format of the data.

We're looking for movies that are well-loved by users who dislike movies most users like.

Find the M movies which have been rated the highest across all users (of movies which have been rated by at least R users). (If there's a tie for the Mth spot, prefer most recent publication then alphabetical order of title.) These are the "top movies."

Of users who have rated all top M movies, find the U users which have given the lowest average rating of the M movies. (If there's a tie for the Uth spot, prefer users with the lower ID.) These are the "contrarian users."

sealed trait Pokemon {
def name: String
def elemental: String
}
// I want a macro like this.
// (The type names may need to need to be in strings.)
// (I'm not entirely sure that quasiquote macros can do this magic.)
enum[Pokemon](
Bulbasaur -> ("フシギダネ", "Grass"),
sealed trait Pokemon {
def name: String
def elemental: String
}
object Pokemon {
final val Values: Seq[Pokemon] = themAll[Pokemon]
// Produces: Seq(
// Bulbasaur,
// Ivysaur,
@leifwickland
leifwickland / SnapshotRowsToDruidQuery.scala
Created November 17, 2015 16:21
scalaz.stream.chunkUpToNBy2
import scalaz.stream.{ Process, Process1 }
object SnapshotRowsToDruidQuery {
/**
* Break the stream into a chunks of up to `n` elements as long as `f` returns true.
*/
def chunkUpToNBy2[I](n: Int, f: (I, I) => Boolean): Process1[I, Vector[I]] = {
require(n > 0, s"chunk size, `n`, must be > 0. It was $n.")
def newChunk(startingWith: I): Process1[I, Vector[I]] = {
go(Vector(startingWith), startingWith)
@leifwickland
leifwickland / jucAsScala.scala
Last active September 9, 2015 16:49
j.u.c.ConcurrentMap asScala
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.ConcurrentMap
import scala.collection.JavaConverters._
val m: ConcurrentMap[String, String] = new ConcurrentHashMap[String, String]
m.put("1", "one")
m.put("2", "two")
val cm: scala.collection.concurrent.Map[String, String] = m.asScala
cm += ("3" -> "three")
cm.foreach(println)
@leifwickland
leifwickland / JumpConsistentHash.scala
Created March 21, 2015 06:11
JumpConsistentHash Scala
def jumpConsistentHash(theKey: Long, bucketCount: Int): Int = {
var b = -1L
var j = 0L
var key = theKey
while (j < bucketCount && j >= 0) {
b = j
key = key * 2862933555777941757L + 1
j = ((b + 1) * ((1L << 31).toDouble / ((key >> 33) + 1).toDouble)).toLong
}
return b.toInt
def f(s: String) = {
s.split(':') match {
case Array(k,v) => k -> v
case a => error("bad: " + a)
}
}
@leifwickland
leifwickland / gist:f208f2e23698c8cd4aee
Created December 1, 2014 19:02
sbt default repository config
# If you launch sbt with -Dsbt.override.build.repos=true -Dsbt.repository.config=$FILE
# then this configuration should be roughly equivalent to the default behavior.
[repositories]
local
typesafe-ivy-releases: https://repo.typesafe.com/typesafe/ivy-releases/, [organization]/[module]/[revision]/[type]s/[artifact](-[classifier]).[ext], bootOnly
maven-central
sbt-plugins: https://repo.scala-sbt.org/scalasbt/sbt-plugin-releases/, [organization]/[module]/(scala_[scalaVersion]/)(sbt_[sbtVersion]/)[revision]/[type]s/[artifact](-[classifier]).[ext], bootOnly
@leifwickland
leifwickland / gist:710c5e7637a5bbe6a817
Created November 21, 2014 03:14
Convert a JPG to PDF with ghostscript
gs -q -sPAPERSIZE=letter -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=output.pdf /usr/share/ghostscript/9.10/lib/viewjpeg.ps -c \(image/path.jpg\) viewJPEG