Skip to content

Instantly share code, notes, and snippets.

View alexy's full-sized avatar

Alexy Khrabrov alexy

View GitHub Profile
@alexy
alexy / ScallopTest.scala
Created April 9, 2012 18:42
still fails to parse trailing args list?
import org.rogach.scallop._
import org.scalatest.FunSuite
import org.scalatest.matchers.ShouldMatchers
/**
* Created with IntelliJ IDEA.
* User: Alexy
* Date: 4/6/12
* Time: 10:34 AM
@alexy
alexy / HelloKitty.scala
Created April 9, 2012 18:08
extending verify to check semantics: both options must be set or unset together
(dateOpt, oldRankerPairsFileOpt) match {
case (Some(_),Some(_)) => Console.err.println("You gave both a date and the previous ranker-pairs file. Good.")
case (None, None) => // nothin' is nothin' is nothin'
case _ => sys.error("You have to give an old ranker-pairs file for a date, and vice versa. Bad.")
}
@alexy
alexy / ScallopTest.scala
Created April 6, 2012 17:53
Holder fails, builder parses trailing args allright
import org.rogach.scallop._
import org.scalatest.FunSuite
import org.scalatest.matchers.ShouldMatchers
/**
* Created with IntelliJ IDEA.
* User: Alexy
* Date: 4/6/12
* Time: 10:34 AM
@alexy
alexy / ParsePairResults.scala
Created April 3, 2012 19:51
packaging scallop options
case object o {
val suffix: Boolean = opts[Boolean]("suffix")
val csv: Boolean = opts[Boolean]("csv")
val minMax: Option[Int] = opts.get("min")
override def toString: String =
"\n"+
"\n suffix=> " + suffix +
"\n csv => " + csv +
"\n minMax=> " + (minMax match { case Some(n) => n.toString; case _ => "nothing" })
@alexy
alexy / io.scala
Created March 30, 2012 19:16
how do we move val w declaration inside try?
def printFileWriter(name: String): PrintWriter = {
new PrintWriter(new FileWriter(name))
}
def writing[B](fileName: String)(f: PrintWriter => B): B = {
lazy val w = printFileWriter(fileName) // so throws only in try, if ever
try { f(w) }
finally { w.close() }
}
@alexy
alexy / mongo-casbah-bulk-load.scala
Created March 27, 2012 20:07
Mongo Bulk Load from Casbah
import com.mongodb.casbah._
import org.joda.time.DateTime
import collection.mutable.ArrayBuffer
//...
def mongoGobble(co: Conf, chunk: List[Quad], globalIndex: Int) = {
val gobbles = chunk.zipWithIndex map { case ((x,y,r,ri),i) =>
val n = globalIndex + i
val id = co.batch match {
@alexy
alexy / evenly.scala
Created March 24, 2012 22:32
Why manifest is needed?
def evenlyN[T](a: Array[T], n: Int): Array[T] = {
if (n < 1 || a.size < n) return a
val step = (a.size + 1) / n
val indices = (1 to n) map (_*step)
(indices map (a(_))).toArray
}
<console>:12: error: could not find implicit value for evidence parameter of type ClassManifest[T]
(indices map (a(_))).toArray
@alexy
alexy / responses.scala
Created March 2, 2012 22:42
case class updates
sealed trait Response
case object Right extends Response
case object Wrong extends Response
case object Skip extends Response
case class Responses(right: Long = 0, wrong: Long = 0, skip: Long = 0) {
def apply(r: Response) = r match {
case Right => right
case Wrong => wrong
case Skip => skip
@alexy
alexy / jobconf.txt
Created January 12, 2012 22:09
Initialize a Hadoop job from Scala
package com.klout.labs.braver.util
import org.apache.hadoop.mapreduce.Job
import org.apache.hadoop.conf.Configuration
object Hadoop {
def setJobConfig(name: String, jobClass: Class[_], outputClasses: Option[Tuple2[Class[_],Class[_]]]) {
val conf = new Configuration()
@alexy
alexy / seq-kv.txt
Created January 12, 2012 21:28
Sequence File Output for key Text and value Text
trait OutputConverter[K, V, S] {
def toKeyValue(s: S): (K, V)
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
trait DataSink[K, V, B] {
def outputTypeName: String
def outputPath: Path
def outputFormat: Class[_ <: FileOutputFormat[K,V]]