Skip to content

Instantly share code, notes, and snippets.

View ldacosta's full-sized avatar

Luis Da Costa ldacosta

View GitHub Profile
@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 / 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)
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
}
}
val operationsFutureMat: List[Future[Try[currentype]]] = operationsFuture.map {
+ _.map(s => Success(s)).recover {
+ case x => Failure(x)
@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 = {
@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.