Skip to content

Instantly share code, notes, and snippets.

View akr4's full-sized avatar

UEDA Akira akr4

  • Tokyo, Japan
View GitHub Profile
@akr4
akr4 / gist:5497337
Created May 1, 2013 18:44
HTML escape & unescape
(defun unhtml (start end)
(interactive "r")
(save-excursion
(save-restriction
(replace-string "&" "&" nil start end)
(replace-string "<" "&lt;" nil start end)
(replace-string ">" "&gt;" nil start end))))
(defun html (start end)
(interactive "r")
@akr4
akr4 / gist:3999182
Created November 2, 2012 07:09
xsbt: add Hadoop jars to unmanaged classpath
lazy val hadoopHomePath =
Properties.envOrNone("HADOOP_HOME").map { file }.getOrElse(error("please set HADOOP_HOME"))
lazy val main = Project(groupName, file("."),
settings = defaultSettings ++ Seq(
unmanagedClasspath in Compile <<= (unmanagedClasspath in Compile, baseDirectory) map {
(cp, bd) => cp ++ Attributed.blankSeq(hadoopHomePath ** "*.jar" get)
}
)
)
@akr4
akr4 / gist:3862236
Created October 9, 2012 23:52
Processing with Scala
// modified from http://d.hatena.ne.jp/alpha_neet/20110706/1309901375
import processing.core._
import processing.core.PConstants._
trait Application extends PApplet {
def main(args: Array[String]) {
PApplet.runSketch(Array("title"), this)
}
}
@akr4
akr4 / maze.scala
Created July 13, 2012 19:43
A* アルゴリズムで迷路を解く
scala> import maze._
import maze._
scala> val m = Mazes.load(new java.io.File("maze.txt"))
m: maze.Maze = maze.Maze@20ccb51
scala> Mazes.encodeRunLength(m.solve.get)
res0: List[(maze.Direction.Value, Int)] = List((S,1), (E,10), (S,2), (W,31), (N,4), (E,2), (S,2), (E,2), (N,4), (E,7), (N,1))
@akr4
akr4 / gist:2402522
Created April 17, 2012 00:22
固定サイズの Queue
class FixedSizeQueue[A](limit: Int) extends scala.collection.mutable.Queue[A] {
override def +=(elem: A) = {
super.+=(elem)
while (size > limit) { dequeue }
this
@akr4
akr4 / gist:2324183
Created April 7, 2012 00:11
カレントディレクトリ以下で mvn clean する
import scala.sys.process._
for (pom <- ("find . -name pom.xml" !!) split "\n") {
"mvn -f " + pom + " clean" run
}
@akr4
akr4 / gist:2279505
Created April 1, 2012 23:44
Scala + Rome で RSS 出力
package com.fourseasonszoo.niboshi
import com.sun.syndication.io._
import com.sun.syndication.feed.synd._
import scala.collection.JavaConverters._
import org.scala_tools.time.Imports._
import scalax.io._
import java.io.StringWriter
object Main extends App {
@akr4
akr4 / gist:2136168
Created March 20, 2012 14:28
レーベンシュタイン距離を計算する
object Levenshtein {
def calc(a: String, b: String): Int = {
val d = Array.ofDim[Int](a.size + 1, b.size + 1)
// initialization
for (i <- 0 to a.size) d(i)(0) = i
for (j <- 0 to b.size) d(0)(j) = j
for (
i <- 1 to a.size;
@akr4
akr4 / gist:1604256
Created January 13, 2012 02:15
Console StopWatch
while (true) {
readLine
println("start")
val start = System.currentTimeMillis
readLine
val end = System.currentTimeMillis
println("end")
println("%.1f".format( (end - start).toDouble / 1000))
}
@akr4
akr4 / gist:1503641
Created December 20, 2011 22:39
ネストしたコレクション Stream[Iterator[_]] を 1 つの iterator で lazy に走査する
scala> val a = (1 to 100).grouped(10).map(_.iterator)
a: Iterator[Iterator[Int]] = non-empty iterator
scala> val b = Stream.continually{ print("#"); a.next }.take(10)
#b: scala.collection.immutable.Stream[Iterator[Int]] = Stream(non-empty iterator, ?)
scala> val c = b.iterator.flatten
c: Iterator[Int] = non-empty iterator
scala> c.foreach(print)