Skip to content

Instantly share code, notes, and snippets.

View Chandler's full-sized avatar

Chandler Abraham Chandler

View GitHub Profile
@Chandler
Chandler / gist:11013054
Last active August 29, 2015 14:00
function composition
abstract class PrintSomething() {
def apply(s: String): Unit
}
object PrintSomething {
/**
* @param printMethod a method that prints something
* @return a PrintSomething with an apply method
* equal to your printMethod
type PrintSomething = (String) => String
val printer1: PrintSomething = (s: String) => {
print("\nI love " + s)
s
}
val printer2: PrintSomething = (s: String) => {
print("\nI hate " + s)
s
update, I have a test case here: https://gist.github.com/Chandler/7766963
I'm running it with multiple files as the input and cascading.hadoop.hfs.combine.files=true
When I run it against stock elephant bird it fails with the original error
"com.twitter.elephantbird.mapred.input.DeprecatedLzoTextInputFormat cannot be cast to org.apache.hadoop.mapred.FileInputFormat"
https://github.com/kevinweil/elephant-bird/pull/359
When I run it with Dmitriy's elephant bird patch it fails with "DeprecatedInputFormatWrapper can not support RecordReaders that don't return same key & value objects. current reader class : class com.twitter.elephantbird.mapreduce.input.LzoLineRecordReader"
class myException extends Exception
object myObj {
val ex = myException
}
// <console>:11: error: not found: value myException
// val ex = myException
//this works
def checkUnauthorizedException[T](t: Try[T]): TestResult =
t match {
case Throw(e) if e.isInstanceOf[UnauthorizedException] => TestResult.Success
case _ => TestResult.fail("found " + t.toString() + " but expected Throw[UnauthorizedException]")
}
//possible to make this more generic? e.g. something like..
case class MyException(errorCode: Int) extends RuntimeException
def onThrow[T <: Throwable](check: Check[T])(implicit m: Manifest[T]): Check[Try[_]] =
expected("A Throw(e: %s)".format(m.erasure)).exceptWhen { case Throw(NonFatal(e: T)) => check(e) }
val check = onThrow {
hasType[Unauthorized.type].on { e: ClientError => e.errorCause }
}
check(Throw(MyException(5)))
def myMethod {
val a: Option[Int] = someOption()
if (a.isNone){
//do a side effect (like increment a hadoop counter)
}
return a
}
def sideEffect() {
//increment hadoop counter
}
val list = List(Some("a"),Some("b"),None, Some("e"), None)
val filtered = list.flatMap { x =>
if(x.isEmpty) sideEffect()
x
}
case class WriteEvent(name: String, records: List[Int])
case class SingleRecord(name: String, value: Int)
val events = List(
WriteEvent("follow", List(1,2,3)),
WriteEvent("block", List(2,3,4)),
WriteEvent("follow", List(5,6,7))
)
scala> def test(name: String = "joe"): Boolean = true
test: (name: String)Boolean
scala> val test: String => Boolean = { (name: String = "joe") => true }
<console>:1: error: ')' expected but '=' found.