Skip to content

Instantly share code, notes, and snippets.

View dacr's full-sized avatar
🕷️

Crosson David dacr

🕷️
View GitHub Profile
@dacr
dacr / memreftest.scala
Last active December 19, 2015 22:29
Inner block local variables without returning any reference are not freed !
#!/bin/sh
exec scala -deprecation -nocompdaemon "$0" "$@"
!#
def sleep(inSeconds:Int) = Thread.sleep(inSeconds*1000L)
def usedMemoryInMo_sillyimpl() = {
System.gc
sleep(2)
val rt= java.lang.Runtime.getRuntime
@dacr
dacr / memreftest2.scala
Created July 18, 2013 13:32
revisted...
#!/bin/sh
exec scala -deprecation -nocompdaemon "$0" "$@"
!#
def sleep(inSeconds:Int) = Thread.sleep(inSeconds*1000L)
def usedMemoryInMo_sillyimpl() = {
System.gc
sleep(2)
val rt= java.lang.Runtime.getRuntime
@dacr
dacr / gist:6057315
Last active December 20, 2015 02:38
Check if a remote HTTP server is accessible or not (DNS resolution test only is not enough)
import scala.concurrent._
import ExecutionContext.Implicits.global
val f = future { new java.net.URL("http://mvnrepository.com/artifact/com.jcraft/jsch").openConnection() }
.map { cnx =>
cnx.setConnectTimeout(5000)
cnx.setReadTimeout(5000)
cnx.setAllowUserInteraction(false)
cnx.setUseCaches(false)
cnx }
@dacr
dacr / gist:6057924
Created July 22, 2013 21:35
Playing with generic numbers and collections
// -----------------------------------------------
// Generic square method that works with any kind
// of numerical type
// -----------------------------------------------
def square[N](x: N)(implicit n: Numeric[N]) = {
n.times(x, x)
}
square(10d)
square(BigDecimal(4))
@dacr
dacr / memtest.scala
Last active December 20, 2015 03:49
conceptual block doest not zero out references, so the following code fails with an OutOfMemoryError.
#!/bin/sh
exec scala -J-Xms80m -J-Xmx80m -deprecation -nocompdaemon "$0" "$@"
!#
def allocate(szInMb:Int=24) =
Array.fill[Byte](1024*1024*szInMb)(util.Random.nextInt(256).toByte)
def testit = {
val showStep = {var s=0 ; () => {s+=1 ; println(s"step$s")}}
{ showStep(); val x= allocate(20) }
#!/bin/sh
exec scala -J-Xms80m -J-Xmx80m -deprecation -nocompdaemon "$0" "$@"
!#
def allocate(szInMb:Int=24) =
Array.fill[Byte](1024*1024*szInMb)(util.Random.nextInt(256).toByte)
def allocally[A](proc: => A) {proc}
def testit = {
@dacr
dacr / count-lines.scala
Last active December 20, 2015 07:38
A scala script that counts the number of lines in source code files
#!/bin/sh
exec /opt/analysis/analysis "$0" "$@"
!#
import scalax.file._
import scalax.file.ImplicitConversions._
val dirs = if (args.size>0) args.toList else List(".")
val exts = List(
"scala","xml","drl","java","ksh","sh","sql","sbt","properties","conf"
@dacr
dacr / json4s_serialization.scala
Last active December 20, 2015 09:28
serialization example using json4s
import java.util.Date
trait Currency {
val symbol:String
val name:String
}
// unfortunately Objects is not supported by json4s (or not yet)
case class Dollar() extends Currency {
@dacr
dacr / compact.scala
Created July 31, 2013 19:58
Generic compact collection functions : compact and compactBy
import scala.collection.generic.CanBuildFrom
import scala.annotation.tailrec
def compact[A, I[A]](list: I[A])
(implicit bf: CanBuildFrom[I[A], A, I[A]], c2i:I[A]=>Iterable[A]): I[A] = {
var builder = bf.apply()
var prev: Option[A] = None
for (item <- list) {
if (prev == None || prev.get != item) builder += item
prev = Some(item)
@dacr
dacr / gist:6556768
Last active December 23, 2015 00:58
Two doubles generator function
scala> def doubler(start:Long):Stream[Long]=start#::doubler(2*start)
doubles: (start: Long)Stream[Long]
scala> doubler(1250).take(12).toList
res21: List[Long] = List(1250, 2500, 5000, 10000, 20000, 40000, 80000, 160000, 320000, 640000, 1280000, 2560000)
scala> def doubler[N](start:N)(implicit n: Numeric[N]):Stream[N]=start#::doubler(n.plus(start,start))
doubles: [N](start: N)(implicit n: Numeric[N])Stream[N]
scala> doubler(1250d).take(6).toList