Skip to content

Instantly share code, notes, and snippets.

@takezoe
takezoe / Properties.scala
Created October 1, 2011 12:12
The simple wrapper of java.util.Properties for Scala
class Properties(path: String) {
private val props: java.util.Properties = new java.util.Properties()
props.load(Thread.currentThread.getContextClassLoader.getResourceAsStream(path))
def apply(key: String): Option[String] = Option(props.getProperty(key))
}
object Properties {
def apply(path: String) = new Properties(path)
}
@takezoe
takezoe / OptionalTest.java
Created October 15, 2011 02:44
Guava 10で導入されたOptionalを試してみる
package jp.sf.amateras.guava;
import static junit.framework.Assert.*;
import org.junit.Test;
import com.google.common.base.Optional;
public class OptionalTest {
@Test
public void testOf(){
@takezoe
takezoe / ScalatePlugin.scala.patch
Created October 26, 2011 12:45
Patch for xsbt-scalate-precompile-plugin to support nested directory.
--- ScalatePlugin.scala.org 2011-10-26 21:35:49.000000000 +0900
+++ ScalatePlugin.scala 2011-10-26 21:03:04.000000000 +0900
@@ -35,9 +35,11 @@
engine.generateScala(source).source
}
- private def generate (engine: TemplateEngine, template: File, outputdir: File, log: Logger) = {
+ private def generate (engine: TemplateEngine, template: File, outputdir: File, log: Logger): File = {
log.info(" compiling template: " + template)
- IO.write(scala(template, outputdir), code(engine, template))
@takezoe
takezoe / gist:1372510
Created November 17, 2011 06:21
wraps java.io.InputStream by scala.collection.immutable.Stream
val stream = fromInputStream(new java.io.FileInputStream("test.jpg"), 8 * 1024)
/**
* Makes Stream which reads every bytes with specified size from java.io.InputStream.
*/
@tailrec
def fromInputStream(in: java.io.InputStream, bufferSize: Int): Stream[Array[Byte]] = {
val buffer = new Array[Byte](bufferSize)
in.read(buffer) match {
case -1 => Stream.empty
@takezoe
takezoe / gist:1372537
Created November 17, 2011 06:45
read bytes from java.io.InputStream and process them by closure
processInputStream(new java.io.FileInputStream("test.jpg"), 8 * 1024){ bytes =>
...
}
/**
* Invokes the given function for each read bytes with specified size from java.io.InputStream.
*/
@tailrec
def processInputStream(in: java.io.InputStream, bufferSize: Int)(func: Array[Byte] => Unit): Unit = {
val buffer = new Array[Byte](bufferSize)
@takezoe
takezoe / MyLispParser.scala
Created November 29, 2011 08:13
MyLisp implemented using Scala parser combinator library
package mylisp
import scala.util.parsing.combinator.RegexParsers
import scala.collection.mutable.{Map => MutableMap}
object MyLispParser extends App {
val source = """
(defun sayHello (name) (println "Hello " name "!"))
(defun sum (a b)(if (eq a b) a (sum (+ a 1) b)))
(println (sum 1 1000))
@takezoe
takezoe / gist:1449859
Created December 9, 2011 02:29
XML response with Unfiltered
import unfiltered.request._
import unfiltered.response._
import scala.xml._
case class Xml(nodes: NodeSeq)
extends ComposeResponse(TextXmlContent ~> ResponseString(nodes.toString))
// Usage
def intent = {
case GET(Path(Seg("hello" :: name :: Nil))) => {
@takezoe
takezoe / Configurable.scala
Created December 17, 2011 16:30
Configure an instance by the given function
class Configurable[T](value: T) {
def configure (func: T => Unit): T = {
func(value)
value
}
}
object Configurable {
implicit def any2configurable[T](value: T): Configurable[T] = new Configurable[T](value)
}
@takezoe
takezoe / TypeClassSample1.scala
Created December 25, 2011 17:46
Example of type class in Scala
/** 型クラス */
trait Equals[A] {
def equals(a1: A, a2: A): Boolean
}
/** 型クラスを使用した関数(実際はcontext boundで書くかも?) */
def equals[A](a1: A, a2: A)(implicit eq: Equals[A]): Boolean = eq.equals(a1, a2)
/** String用のEquals実装 */
implicit object StrEquals extends Equals[String]{
@takezoe
takezoe / JSONSample.scala
Created December 30, 2011 15:02
Example of scala.util.parsing.json
import scala.util.parsing.json._
val result = JSON.parseFull("""
{"name": "Naoki", "lang": ["Java", "Scala"]}
""")
result match {
case Some(e) => println(e) // => Map(name -> Naoki, lang -> List(Java, Scala))
case None => println("Failed.")
}