This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- 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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** 型クラス */ | |
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]{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") | |
} |
OlderNewer