Skip to content

Instantly share code, notes, and snippets.

View tototoshi's full-sized avatar

Toshiyuki Takahashi tototoshi

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / 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 / AfterComposeSpec.scala
Created March 23, 2012 02:11
specs2 で After を合成
import org.specs2.mutable._
class AfterComposeSpec extends Specification {
"Compose After" should {
"after123" in (After1 then After2 then After3){
success
}
@tototoshi
tototoshi / picture_show_test.md
Created April 10, 2012 12:22
picture show test

!SLIDE

Hello

@tototoshi
tototoshi / either_20120423.scala
Created April 23, 2012 02:57
地味につらい
/* これはできる */
val o = Some(("foo", 1))
for {
(s, i) <- o
} yield {
println(s)
println(i)
}