Skip to content

Instantly share code, notes, and snippets.

View ldacosta's full-sized avatar

Luis Da Costa ldacosta

View GitHub Profile
@ldacosta
ldacosta / BunchOfTraits
Created January 16, 2014 18:47
Let's suppose that I have an abstract trait for which I have 'n' (> 2) concrete implementations. When I mix those traits, I want to have an elegant way to create concrete classes. See below.
// define mixable traits:
trait T
trait T1 extends T
trait T2 extends T
...
trait T100 extends T
// now let's mix them:
trait anotherT extends T
// and create concrete classes.
@ldacosta
ldacosta / String2Base36
Last active January 3, 2016 21:39
How to convert a String into Base36
def toBase36(str: String): Long = {
var id: Long = 0
for (c <- str) {
val d = if (c.isDigit) c.toInt - '0'.toInt
else c.toInt - 'A'.toInt + 10
id = (id * 36) + d
}
id
}
def fromBase36(idp: Long): String = {
val operationsFutureMat: List[Future[Try[currentype]]] = operationsFuture.map {
+ _.map(s => Success(s)).recover {
+ case x => Failure(x)
def prepareDiskFor(profileFileName: String): Boolean = {
// create directory if it doesn't exist
val theDir: File = Utils.getFolderFromFileName(profileFileName)
if (!theDir.exists() && !theDir.mkdirs()) {
error("Impossible to save profile because directory [%s] does not exist nor can I create it".format(theDir.getAbsolutePath))
false
} else {
true
}
}
@ldacosta
ldacosta / betterCounting
Last active August 29, 2015 14:05
Counting Booleans
def isEven(x:Int) = (x % 2 == 0)
def isPositive(x: Int) = (x > 0)
def aList = List(1,2,-2,34,57, -91, -90)
// in this case I go from [Int] => [Bool] => Int. 2 times, because I have 2 functions.
val (numEven, numPos) = (aList.map(isEven).filter(_ == true).length, aList.map(isPositive).filter(_ == true).length)
@ldacosta
ldacosta / gist:045703aabc4aa5844c64
Last active August 29, 2015 14:06
CleanString: Wrapper on String
object Wrappers extends Serializable {
/* ********************************************************* */
// Business of CleanString
// TODO: move this to a saner location
trait Wrapper[T] extends Serializable {
val value: T
}
trait StringWrapper extends Wrapper[String] {
override val value: String
@ldacosta
ldacosta / SparkSomethingNoSerializable
Last active August 29, 2015 14:07
Running the following code throwss NonSerializableException when using the case class
import org.apache.spark.SparkContext
import org.apache.spark.rdd.RDD
import scala.util.control.Exception._
import scala.util.parsing.combinator.Parsers
import scala.util.parsing.input.{CharSequenceReader, Reader, OffsetPosition}
import scala.language.postfixOps
case class myCaseClass(c: Char) extends Serializable
@ldacosta
ldacosta / SparkFactoryNotSerializable.scala
Last active August 29, 2015 14:07
I am trying to narrow down an Exception thrown by Spark when using "factories" to create classes. See example below ====> only factory2 works!
import org.apache.spark.SparkContext
import org.apache.spark.rdd.RDD
/**
* I am trying to narrow down on an Exception thrown by Spark when using "Factories".
* The factories have parameters that are used in the classes' functions.
*
* To run this code: copy-paste this whole content in a Spark-Shell. Execute Test.theMain(sc)
*
*/
@ldacosta
ldacosta / SparkSQL-UDFTypes.scala
Last active August 29, 2015 14:08
Spark SQL: register a table using a case class that has user-defined types
/**
* We want to register a table with Spark SQL whose rows are composed of 2 longs and 1 String.
* We would like to put restrictions of the shape of the String: for example, that it doesn't
* contain non-alphanumeric characters (or whatever...)
* Let's define what we want:
*/
// CleanString defined somewhere else
case class MyCaseClass(firstP: Long, secondP: Long, thirdP: CleanString)
// a Representation is just a wrap around a type
trait Repr[T] extends wrappers.Base.Wrapper[T]
// one way of representing a value is by having the value itself:
case class Eval[T](value: T) extends Repr[T]
// or:
case class Identity[T](value: T) extends Repr[T]
// another way is by println()'ing the value before:
case class Debug[T](raw: T) extends Repr[T] {
def value: T = { println(raw.toString); raw }
}