Skip to content

Instantly share code, notes, and snippets.

View dacr's full-sized avatar
🕷️

Crosson David dacr

🕷️
View GitHub Profile
@dacr
dacr / gist:4559994
Last active December 11, 2015 06:29
scala 2.10.0, script example to illustrate how to get futures results as soon as they become available.
#!/bin/sh
exec scala -nocompdaemon -usejavacp -savecompiled "$0" "$@"
!#
/* will output :
END of the script reached
OK - slept 1s
OK - slept 2s
OK - slept 4s
OK - slept 8s
@dacr
dacr / gist:4634691
Created January 25, 2013 14:05
small test to check memory benefit of String substring change in Java 1.7.0_06
#!/bin/sh
exec scala -deprecation -nocompdaemon "$0" "$@"
!#
def usedMemoryInMo() = {
val rt= java.lang.Runtime.getRuntime
(rt.totalMemory - rt.freeMemory)/1024/1024
}
println(util.Properties.javaVersion)
@dacr
dacr / gist:4642782
Last active July 9, 2020 11:02
Custom scala collection examples
import scala.collection._
import scala.collection.mutable.{ArrayBuffer,ListBuffer, Builder}
import scala.collection.generic._
import scala.collection.immutable.VectorBuilder
// ================================ CustomTraversable ==================================
object CustomTraversable extends TraversableFactory[CustomTraversable] {
@dacr
dacr / gist:4718280
Created February 5, 2013 22:22
custom scala set collection example.
// ============================= NamedCustomSet ===================================
object NamedCustomSet {
def apply[A](name:String, s:A*) = new NamedCustomSet(name, s.toSet)
implicit def canBuildFrom[A,B]: CanBuildFrom[NamedCustomSet[B], A, NamedCustomSet[A]] =
new CanBuildFrom[NamedCustomSet[B], A, NamedCustomSet[A]] {
def apply(from: NamedCustomSet[B]) = newBuilder(from.name)
def apply() = newBuilder
}
@dacr
dacr / gist:4749592
Last active December 12, 2015 09:09
1000 distinct remote ssh executions in 35s (1s required on each host)
#!/bin/sh
exec java -jar jassh.jar -nocompdaemon -usejavacp -savecompiled "$0" "$@"
!#
// jassh.jar can be downloaded here : http://code.google.com/p/janalyse-ssh/
import jassh._
import concurrent._
import duration._
import scala.util._
@dacr
dacr / gist:5180825
Created March 17, 2013 09:39
list, view and stream - An example to understand the differences
#!/bin/sh
exec scala -deprecation -nocompdaemon "$0" "$@"
!#
val l=List(0, 1, 2)
var x=1
val m1=l.map(_ +x) // c1
val v1=l.view.map(_+x) // c2
@dacr
dacr / gist:5266909
Created March 28, 2013 21:20
Append a stream A to a stream B doesn't imply evaluation of all items of the first Stream A.
#!/bin/sh
exec scala -deprecation -nocompdaemon "$0" "$@"
!#
def trace(x:Int) = {println(x); x}
val x=(1 to 5).toStream.map(trace _)
// 1 is printed
val y=(5 to 1 by -1).toStream.map(trace _)
@dacr
dacr / gist:5322751
Last active December 15, 2015 20:59
Looking for the shortest (1 line only) and cleanest way to extract a map from a collection of parameters
val args=Array("x=5", "y=10", "debug=true", "-verbose", "toto=truc1=titi", "nop", "tata=")
val argsRE="([^=]+)=(.*)".r
// ------- solution 1 with regex
args.flatMap{case argsRE(k,v) => Some(k->v) case _=> None}.toMap
// ------- solution 2 - with regex
args.collect{case argsRE(k,v) => k->v}.toMap
@dacr
dacr / gist:5437528
Created April 22, 2013 18:55
Play : Get ride of Thread.sleep to simulate remote processing
package controllers
import play.api._
import play.api.mvc._
import play.api.libs.concurrent.Execution.Implicits._
import play.api.libs.concurrent.Akka
import play.api.Play.current
import scala.concurrent._
import scala.concurrent.duration._
@dacr
dacr / small-algo.scala
Created May 9, 2013 20:07
A small algorithm to find the indexof the first element superior or equal to the given value (in an ordered sequence)
#!/bin/sh
exec scala "$0" "$@"
!#
import scala.annotation.tailrec
// ---------------------------------------------------
def searchFirstGreaterOrEqual[T<%Ordered[T]](seq: Seq[T], value: T):Option[Int] = {
@tailrec
def binarysearch(left:Int, right:Int):Option[Int] = {