Skip to content

Instantly share code, notes, and snippets.

@crakjie
crakjie / time.scala
Last active February 15, 2016 22:16
Elapsed time of a piece of code
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
def time[T](f: => T, message: String = "" ): T = {
val a = System.currentTimeMillis()
val r = f
val b = System.currentTimeMillis()
Future { println(message + " elapsed time :"+ (b - a) + "ms" + Console.RESET )}
r
}
@crakjie
crakjie / ImplicitGetOrElse.scala
Last active August 29, 2015 14:07
Enable getOrElse on collection
import scala.collection.GenTraversableOnce
implicit class ImplicitGetOrElse[T]( seq: Seq[T] ) {
def getOrElse[B >: T]( or: => GenTraversableOnce[B] ) = {
if ( seq.isEmpty ) or else seq
}
}
@crakjie
crakjie / SeqDisjonction
Created March 19, 2015 18:18
Hack seq to add scalaz disjonction as operator which make a left when the list is empty
import scalaz.{\/, \/- ,-\/}
implicit final class SeqOps[A](seq : Seq[A]) {
def \/>[T](t : T) : T \/ Seq[A] = if (seq.isEmpty) -\/(t) else \/-(seq)
}
//usage
val right = Seq(1,2) \/> "Oh a nel!"
val left = Nil \/> "!#& a Nil !!"
@crakjie
crakjie / CassandraLeftJoinRDD.scala
Last active September 5, 2016 15:34
CassandraLeftJoinRDD
package com.datastax.spark.connector.rdd
import org.apache.spark.metrics.InputMetricsUpdater
import com.datastax.driver.core.Session
import com.datastax.spark.connector._
import com.datastax.spark.connector.cql._
import com.datastax.spark.connector.rdd.reader._
import com.datastax.spark.connector.util.CqlWhereParser.{EqPredicate, InListPredicate, InPredicate, RangePredicate}
import com.datastax.spark.connector.util.{CountingIterator, CqlWhereParser}
@crakjie
crakjie / CirceCheck.scala
Created September 3, 2015 13:06
A simple test to know if there is a performence pb in circe
package perf
import play.api.libs.json.Json
import io.circe._, io.circe.generic.auto._, io.circe.jawn._, io.circe.syntax._
import io.circe._
import io.circe.generic.auto._
import io.circe.jawn._
import io.circe.syntax._
//use 0.1.1 circe
//use play 2.3.8
package com.misterbell.shed.models.travellers
import shapeless.ops.record.Merger
import shapeless._
object OpenBaseCopyDemo extends App {
import openCopySyntax._
import mergeSyntax._
@crakjie
crakjie / git-clean-branches
Created November 23, 2015 18:03
Delete each branch that already in the branch master. Put it in you path and you will have auto complete functionality of git.
#!/bin/bash
while read -r line
do
if [ "$line" != "origin/master" ] && [ "$line" != "origin/HEAD -> origin/master" ]
then
inMaster=$(git branch master --contains $line)
if [ "$inMaster" = " master" ] || [ "$inMaster" = "* master" ]
then
branchName=${line#origin/}
echo "$inMaster"
@crakjie
crakjie / case-class-pool.scala
Last active September 12, 2017 14:16
Try to have a case class pooling
abstract case class Toto private[Toto] (i: Int, j: String) {
val hashCodeVal : Int
override def hashCode() : Int = hashCodeVal
def copy(i: Int = i, j: String = j) = {
Toto(i,j)
}
}
object Toto {
val totoPool = scala.collection.mutable.WeakHashMap[Int, Toto]()
@crakjie
crakjie / keepRight
Created February 21, 2018 10:05
optimistique sequance that prefer good result to bad ones
def keepRight[F[_], G[_, _], B, C](l: F[G[B, C]])(
implicit //proofs
G: Bifoldable[G],
Me: scalaz.MonadError[G[B, ?], B],
F: MonadPlus[F],
Mobf: Monoid[F[B]], // proof we had a zero
Moc: Monoid[F[C]], // proof we had a zero
Mob: Monoid[B], // proof we can fold
eqB: scalaz.Equal[F[B]],
eqC: scalaz.Equal[F[C]],
def |>[A,B]( a : A)(f : A => B ) : B = f(a)
@ 2 |> Some.apply
res16: Some[Int] = Some(2)