Scala Advent Calendar jp 2011の記事です。12/07に割り振られていたことに気づいていませんでしたorz。申し訳ありません。 さて、ネタを後で考えるつもりだったので迷ったのですが、直近で面白かったネタということでパーザコンビネータのちょっと変わった 使い方について紹介したいと思います。
さて、いきなり話が飛びますが、皆様はRubyのヒアドキュメントについてご存知でしょうか?
puts <
// Inversefizzbuzz | |
// http://www.jasq.org/2/post/2012/05/inverse-fizzbuzz.html | |
// | |
// fork from https://gist.github.com/2699068 | |
object InverseFizzbuzz extends App { | |
def zzubzzif(pattern:Seq[String]) = { | |
def fizzbuzz(n:Int) = (n%3, n%5) match{ | |
case (0,0) => "fizzbuzz" | |
case (0,_) => "fizz" |
object RetryUtil { | |
case class RetryException(throwables: List[Throwable]) extends Exception | |
def retry[T](retryLimit: Int, retryInterval: Int, shouldCatch: Throwable => Boolean)(f: => T): T = { | |
// @annotation.tailrec | |
def _retry( errors: List[Throwable], f: => T):T = { | |
try { | |
f | |
} catch { |
// コンストラクタをプライベートにし、コンパニオンオブジェクト経由でのみ生成させる。 | |
class ClassName[A] private (val clazz: Class[A]) { | |
// toString で、引数に指定されたクラスの名称を返す。 | |
override def toString(): String = { | |
clazz.getName | |
} | |
} | |
object ClassName { | |
// Classを直接渡して生成する。 |
/* | |
* http://projecteuler.net/problem=2 | |
* http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%202 | |
*/ | |
val fib: List[Int] => List[Int] = { | |
case Nil | _ :: Nil => throw new IllegalArgumentException | |
case xs @ (a :: b :: tail) => { | |
val nextNum = a + b | |
if (nextNum >= 4000000) { | |
xs |
{-# LANGUAGE NoImplicitPrelude, MultiParamTypeClasses, Rank2Types, TypeOperators #-} | |
newtype Id a = | |
Id a | |
data a :\/ b = | |
Left a | |
| Right b | |
data a :/\ b = |
import scala.reflect.runtime.universe._ | |
import scala.reflect._ | |
object App{ | |
def main(args : Array[String]){ | |
val m = toMap(User("Kudo",2,false)) | |
println(m) | |
//Map(admin_? -> false, gender -> 2, name -> Kudo) |
package s | |
object Test { | |
// Observe that x.companion is statically typed such that foo is callable | |
def f1() = { | |
val x = new Foo | |
println(x) // Foo instance | |
println(x.companion) // Foo companion | |
println(x.companion.foo) // I'm foo! |
import sbt._ | |
import Keys._ | |
import Build.data | |
object build extends Build { | |
lazy val runAll = TaskKey[Unit]("run-all") | |
lazy val standardSettings = Seq( | |
runAllIn(Compile) | |
) |
The z in scalaz means this: it was early 2008 and I was working for a Java consultancy and so of course, I used the most appropriate tool for the job: scala. But it had *terrible* libraries, so I offered to fix those while also meeting my other objectives. Turns out that the Scala guys were extremely hostile to even half-decent libraries (and still are to this day). I still struggle to wrap my head around this sometimes. | |
Anyway, so I thought, well fuck it, I will just keep them to myself for now. My (awesome) employer had already agreed that we'd probably open-source such a thing, but I was concerned most about my primary goal. So then it came time to "name" this library. I had named it "scalax" simply so that I did not have the inclination to think of a proper name. Then I found out that such a library was being developed and with my internal name! Whatever, I thought. | |
So I looked at this scalax library, hoping that I could just abandon my efforts and jump on board with everyone else -- they too had figure |