Skip to content

Instantly share code, notes, and snippets.

@huynhjl
huynhjl / MemLeak.scala
Created April 13, 2019 04:21
tensorflow-scala-0.4.1 resource leak
import org.platanios.tensorflow.api._
object MemLeak {
val rt = Runtime.getRuntime
def mb(bytes: Long): String = f"${bytes.toFloat / 1024 / 1024}%.1fMB"
def printMemory(): Unit = {
rt.gc()
@huynhjl
huynhjl / continuations.scala
Last active October 12, 2015 19:38
Continuation Monad
package worksheets
//http://blog.tmorris.net/continuation-monad-in-scala/
object continuations {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
import Continuation._
// http://hackage.haskell.org/packages/archive/mtl/2.0.1.0/doc/html/Control-Monad-Cont.html
@huynhjl
huynhjl / play_cygwin.patch
Created June 16, 2012 03:52
Patch for play and framework/build to run under cygwin and mingw32
diff -r --unified play-2.0-orig/framework/build play-2.0-cygwin/framework/build
--- play-2.0-orig/framework/build 2012-03-12 20:25:28.000000000 -0700
+++ play-2.0-cygwin/framework/build 2012-06-15 17:56:57.436000000 -0700
@@ -8,4 +8,4 @@
DEBUG_PARAM="-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=${JPDA_PORT}"
fi
-java ${DEBUG_PARAM} -Xms512M -Xmx1536M -Xss1M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=384M -Dfile.encoding=UTF8 -Dplay.version="${PLAY_VERSION}" -Dsbt.ivy.home=`dirname $0`/../repository -Dplay.home=`dirname $0` -Dsbt.boot.properties=`dirname $0`/sbt/sbt.boot.properties -jar `dirname $0`/sbt/sbt-launch.jar "$@"
\ No newline at end of file
+java ${DEBUG_PARAM} -Xms512M -Xmx1536M -Xss1M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=384M -Dfile.encoding=UTF8 -Dplay.version="${PLAY_VERSION}" -Dsbt.ivy.home=`dirname $0`/../repository -Dplay.home=`dirname $0` -Dsbt.boot.properties=$BOOTPROP`dirname $0`/sbt/sbt.boot.properties -jar `dirname $0`/sbt/sbt-launch.jar "$@"
@huynhjl
huynhjl / WrapInLastColumn.scala
Created April 16, 2012 05:39
Does cursor wrap when char is printed in last col?
/**
* Determine terminal width (for instance with stty -a) then pass that width as
* the first argument. If the cursor does not move to the next line after
* printing the 'a' characters then the terminal does not automatically wrap
* when the character is printed in the last column.
* For instance in Ubuntu, the cursor will not be visible.
*/
public class WrapInLastColumn {
public static void main(String[] args) {
int width = 80;
@huynhjl
huynhjl / build.sh
Created April 14, 2012 12:33
play 2.0 scripts for cygwin
#! /usr/bin/env sh
PLAY_VERSION="2.0"
if [ -z "${JPDA_PORT}" ]; then
DEBUG_PARAM=""
else
DEBUG_PARAM="-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=${JPDA_PORT}"
fi
@huynhjl
huynhjl / L.scala
Created April 8, 2012 07:07
Playing with Lucene QueryParser and Query classes
import org.apache.lucene.queryParser.QueryParser
import org.apache.lucene.search._
import org.apache.lucene.analysis._
import org.apache.lucene.analysis.en.EnglishAnalyzer
import org.apache.lucene.analysis.tokenattributes._
import org.apache.lucene.util.Version.LUCENE_35
case class TextQuery(analyzer: Analyzer) {
val parser = new QueryParser(LUCENE_35 , "dummyfield", analyzer)
def fromString(searchTerms: String) = {
@huynhjl
huynhjl / A.scala
Created March 20, 2012 06:33
Question about parser combinator
// http://stackoverflow.com/questions/9780196/scala-creating-a-basic-dynamic-function-parser
import scala.util.parsing.combinator._
object Expr { type VARS = Map[String, Any] }
import Expr._
sealed trait Expr { def eval(v: VARS) : Any }
case class If(cond: Cond, ifTrue: Expr, ifFalse: Expr) extends Expr {
def eval(v: VARS) =
@huynhjl
huynhjl / listFoldLeft.scala
Created December 27, 2011 18:16
List foldLeft implementations
object C extends App {
def time[F](f: => F) = {
val t0 = System.nanoTime
val ans = f
printf("Elapsed: %.3f\n",1e-9*(System.nanoTime-t0))
ans
}
def lots[F](n: Int, f: => F): F = if (n <= 1) f else { f; lots(n-1,f) }
@huynhjl
huynhjl / xMissing_yDuplicated.scala
Created November 20, 2011 08:14
brain teaser on missing and duplicated number
import util.Random
// setup:
val size = 1e6.toInt
val sorted = (1 to size).toIndexedSeq
val missing = Random.nextInt(size) + 1
val duplicated = Random.nextInt(size) + 1
val input = Random.shuffle(sorted.filter(_ != missing) :+ duplicated)
// find x and y: