Skip to content

Instantly share code, notes, and snippets.

View tototoshi's full-sized avatar

Toshiyuki Takahashi tototoshi

View GitHub Profile
@tototoshi
tototoshi / scalatraのparams("parameterName")がNoneとかじゃなくてNoSuchElementException返してくるのすごく納得いかない.scala
Created February 11, 2012 09:24
scalatraのparams("parameterName")がNoneとかじゃなくてNoSuchElementException返してくるのすごく納得いかない
import java.util.NoSuchElementException
import scala.util.control.Exception._
import org.scalatra._
trait SafeParams { self: ScalatraKernel =>
def paramsSafely(key: String): Option[String] = {
catching(classOf[NoSuchElementException]).opt {
params(key)
}
@tototoshi
tototoshi / echo.scala
Created January 5, 2012 13:31
Scalaでスクリプトとか書くときのコマンドライン引数の解析
#!/bin/sh
exec scala "$0" "$@"
!#
val helpMessage = """
Usage:
-n do not output the trailing newline
-x repeat x times
@tototoshi
tototoshi / ScalaQueryColumnOption.scala
Created January 2, 2012 15:23
ScalaQueryでカラムのオプションを指定する。
// see: https://github.com/szeiger/scala-query/blob/0.9.5/src/main/scala/org/scalaquery/ql/basic/BasicColumnOptions.scala
package com.github.tototoshi.example
import org.scalaquery._
import session.{ Database, Session }
import ql._
import basic.BasicDriver.Implicit._
import basic.{ BasicTable => Table }
@tototoshi
tototoshi / XMLSelector.scala
Created December 31, 2011 03:59
ScalaでjQueryっぽいこと
import scala.xml._
import scala.util.parsing.combinator._
trait SelectorParser extends RegexParsers {
val html: NodeSeq
def tagSelector(tag: String)(filter: NodeSeq => Boolean): NodeSeq = html \\ tag filter filter
def idFilter(id: String): NodeSeq => Boolean = (n: NodeSeq) => (n \ "@id" text) == id
@tototoshi
tototoshi / WebScraping.scala
Created December 30, 2011 10:05
dispatch+Lift+scala-ioでスクレイピング
package com.github.tototoshi.example
import scala.xml.{ NodeSeq, Elem }
import dispatch._
import net.liftweb._
import common._
import util._
@tototoshi
tototoshi / JavaOption.java
Created December 27, 2011 09:45
JavaでOption
interface Function1<T, U> {
U apply(T t);
}
interface Optional<T> {
T get();
T getOrElse(T defaultValue);
boolean isDefined();
<U> Optional<U> map(Function1<T, U> f);
}
@tototoshi
tototoshi / FiltersAreMonoids.scala
Created December 23, 2011 08:48
Filters are monoids
scala> val gt10: Int => Boolean = (n: Int) => n > 10
gt10: Int => Boolean = <function1>
scala> val lt5: Int => Boolean = (n: Int) => n < 5
lt5: Int => Boolean = <function1>
scala> 0 to 15 filter (lt5 |+| gt10)
res35: scala.collection.immutable.IndexedSeq[Int] = Vector(0, 1, 2, 3, 4, 11, 12, 13, 14, 15)
@tototoshi
tototoshi / ScalaQueryWithConnectionPool.scala
Created December 23, 2011 04:10
ScalaQueryでConnectionPoolを使う
/* build.sbt
libraryDependencies ++= Seq(
"org.scalaquery" % "scalaquery_2.9.0-1" % "0.9.5",
"postgresql" % "postgresql" % "8.4-702.jdbc4",
"commons-dbcp" % "commons-dbcp" % "1.4",
"com.github.seratch" %% "scalikejdbc" % "0.1.4" withSources ()
)
resolvers ++= Seq(
@tototoshi
tototoshi / scala_query_case_class.scala
Created December 22, 2011 12:06
ScalaQueryでcase classへのマッピング
import org.scalaquery._
import ql._
import session.{ Session, Database }
import basic.{ BasicTable => Table }
import org.scalaquery.ql.basic.BasicDriver.Implicit._
object Main extends App {
val db = Database.forURL("jdbc:postgresql:example",
driver="org.postgresql.Driver",
@tototoshi
tototoshi / KanjiNumberParser.scala
Created December 18, 2011 05:50
漢数字パーサだよ
import scalaz._
import Scalaz._
import scala.util.parsing.combinator._
object KanjiNumberParser extends RegexParsers {
def one = "一" ^^ { _ => 1L }
def two = "二" ^^ { _ => 2L }
def three = "三" ^^ { _ => 3L }
def four = "四" ^^ { _ => 4L }
def five = "五" ^^ { _ => 5L }