Created
April 9, 2011 12:16
-
-
Save akihiro4chawon/911359 to your computer and use it in GitHub Desktop.
scalaz の練習 - 特殊文字の累計出現回数を数えてみたw
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
// scalaz の ぁゃιぃ 文字数を数える | |
// ちなみに手元の 7e22c46151c24a7b291354091d3b9dc442e3352f ではちょうど 2500 | |
import scalaz._ | |
import scalaz.Scalaz._ | |
import scala.collection.JavaConversions._ | |
import scala.io.Source | |
import java.util.jar.{JarFile, JarEntry} | |
object SymbolCount { | |
def symbolCount(text: List[Char]) = { | |
def liftC[A, B](f: A => B) = {a: A => Const(f(a))} | |
val symbolCountBody: (Char) => Const[Int, ⊥] = | |
liftC {c: Char => if (c >= '\u00C0') 1 else 0} | |
def symbolCount(text: List[Char]): Const[Int, ⊤] = | |
text.traverse[({type λ[α]=Const[Int, α]})#λ, ⊤](symbolCountBody) | |
symbolCount(text): Int | |
} | |
def main(args: Array[String]) = { | |
val jarFilename = "scalaz-core_2.8.1-6.0-SNAPSHOT-sources.jar" | |
val jarFile = new JarFile(jarFilename) | |
val counts = for { | |
entry <- jarFile.entries | |
sourcename = entry.getName if sourcename endsWith ".scala" | |
source = Source.fromInputStream(jarFile.getInputStream(entry), "UTF-8") | |
} yield sourcename -> symbolCount(source.toList) | |
println(counts.map{_._2}.sum + " special symbols in "+jarFilename) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment