This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val operationsFutureMat: List[Future[Try[currentype]]] = operationsFuture.map { | |
+ _.map(s => Success(s)).recover { | |
+ case x => Failure(x) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
* | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 } | |
} |
OlderNewer