Skip to content

Instantly share code, notes, and snippets.

@huynhjl
huynhjl / S.scala
Created October 9, 2011 06:41
scalaz traverse with state example
import scalaz._
import Scalaz._
import java.io._
// http://groups.google.com/group/scalaz/msg/cb7a190b60b2518c?dmode=source
val reader = new BufferedReader(new FileReader("S.scala"))
val s: Stream[String] = Stream.continually(reader.readLine()).takeWhile(_ != null)
type X[x] = State[(Int, StringBuilder), x]
@huynhjl
huynhjl / Batcher.scala
Created July 20, 2011 08:59
Iterator Batcher.batch(Int, IndexedSeq[T] => Seq[U]): Iterator[U]
class Batcher[T](iter: Iterator[T]) {
def batch[U](size: Int, doWithBatch: IndexedSeq[T] => Seq[U]): Iterator[U] =
iter.grouped(size).map(g => doWithBatch(g.toIndexedSeq).toIterator).flatten
}
implicit def toBatcher[T](iter:Iterator[T]) = new Batcher(iter)
val iter = Iterator.from(1).take(20)
def step1(i: Int) = (i, "v" + i)
@huynhjl
huynhjl / getch.c
Created June 21, 2011 13:23
read console
#include <conio.h>
#include <ctype.h>
#include <stdio.h>
int main( void )
{
int ch;
_cputs( "Type 'Y' when finished typing keys: " );
do
@huynhjl
huynhjl / Pi.scala
Created June 18, 2011 01:26
parallel pi tests
object Pi extends App {
println((0 to args(0).toInt).par.map(i => 4. * math.pow(-1, i) / (2. * i + 1.)).sum)
}
object Pi1 extends App {
println((0 to args(0).toInt).par.view.map(i => 4. * math.pow(-1, i) / (2. * i + 1.)).sum)
}
object Pi2 extends App {
println((0 to args(0).toInt).map(i => 4. * math.pow(-1, i) / (2. * i + 1.)).sum)
@huynhjl
huynhjl / HeapDumper.scala
Created June 12, 2011 03:43
Memory Footprint
object HeapDumper {
import javax.management.MBeanServer
import java.lang.management.ManagementFactory
import com.sun.management.HotSpotDiagnosticMXBean
private val HOTSPOT_BEAN_NAME = "com.sun.management:type=HotSpotDiagnostic";
// field to store the hotspot diagnostic MBean
@huynhjl
huynhjl / repl.scala
Created May 29, 2011 14:48
java.lang.UnsupportedOperationException: Position.point crash
// win 7 trunk
import collection.JavaConverters._
val props = System.getProperties.asScala
props.filter(_._1.contains("path"))
@huynhjl
huynhjl / SalesRow.scala
Created May 1, 2011 15:28
mapValues on innermost map
// http://stackoverflow.com/questions/5846208/mapvalues-on-innermost-map-of-nested-maps
case class SalesRow(val month:Int, val country:String,
val person:String, val amount:Float)
object Test {
def main(args:Array[String] = null) {
val list = new collection.mutable.ListBuffer[SalesRow]()
for (m <- 1 to 12; p <- List("Joe", "Bill")) {
val i = 2 * (if (p == "Joe") 7 else 13)
@huynhjl
huynhjl / MyXMLEventReader.scala
Created April 20, 2011 04:07
Getting position from XMLEvent
// http://stackoverflow.com/questions/5715453/scala-xml-pull-parser-and-location
// Originally from Scala API copyrighted by LAMP/EPFL
// See http://www.scala-lang.org/node/146 for License
// Modified to get Source position
import scala.xml.pull._
import scala.xml._
import scala.io.{Position, Source}
import scala.xml.parsing.{ ExternalSources, MarkupHandler, MarkupParser }
@huynhjl
huynhjl / JLine2ClassLoaderTest.scala
Created January 22, 2011 09:25
jansi.dll already loaded in another classloader
// scala -cp "jcl-core-2.2.2.jar;log4j-1.2.16.jar"
// http://sourceforge.net/projects/jcloader/files/jcl2/org/xeustechnologies/jcl/2.2.2/jcl-core-2.2.2.jar/download
// see http://jcloader.sourceforge.net/gettingstarted.html
import org.xeustechnologies.jcl._
import java.lang.reflect.Method
// jline2
//val t = new jline.WindowsTerminal
//t.getWidth
@huynhjl
huynhjl / Clos.scala
Created October 18, 2010 06:32
Closure capturing temporary objects?
// http://stackoverflow.com/questions/3956607/how-should-i-avoid-unintentionally-capturing-the-local-scope-in-function-literals
object Clos {
def method: Iterator[Int] = {
val huge = (1 to 2000000).toList
val n = huge.last
(1 to n).iterator map (_ + 1)
}
def gc() { println("GC!!"); Runtime.getRuntime.gc }