Skip to content

Instantly share code, notes, and snippets.

@halcat0x15a
halcat0x15a / gist:896279
Created March 31, 2011 12:34
SVGの色をIntシーケンスに。
private def color(node: Node, attr: String): Seq[Int] = {
val a = """#(.{2})(.{2})(.{2})""".r
val b = """#(.)(.)(.)""".r
val c = """rgb\((\d+)\,(\d+)\,(\d+)\)""".r
val d = """rgb\((\d+)%\,(\d+)%\,(\d+)%\)""".r
node.attribute(attr) map (_.text) orElse {
(node.attribute("style") map { style =>
style.text.split(";") find (_.contains(attr)) map { str =>
str.trim.split(":").last
}
@halcat0x15a
halcat0x15a / gist:955212
Created May 4, 2011 13:31
clojureでscala.Function*を作る。
(defmacro sfn [params expr]
`(proxy [~(symbol (str "scala.Function" (count params)))] []
(apply ~params
~expr)))
@halcat0x15a
halcat0x15a / JValue.scala
Created July 6, 2011 11:34
Real World Haskellを参考にJSONを表現してみた。
package json
sealed abstract class JValue[+A](value: A)
case class JString(value: String) extends JValue(value) {
override def toString = "\"" + value + "\""
}
@halcat0x15a
halcat0x15a / gist:1092653
Created July 19, 2011 14:57
型のリストでゴニョゴニョ
trait AbstractList
trait Cons[X, XS <: AbstractList] extends AbstractList
trait Nil extends AbstractList
type TypeList = List[Manifest[_]]
def typeList[A <: AbstractList: Manifest]: TypeList = {
lazy val typeList: PartialFunction[TypeList, TypeList] = {
@halcat0x15a
halcat0x15a / io.scala
Created August 15, 2011 11:26
scalaz.effectsを試したり
import scalaz._
import effects._
import Scalaz._
val i = for {
_ <- putStrLn("Greetings! What is your name?")
inpStr <- readLn
outStr = "Welcome to Scalaz, " |+| inpStr |+| "!"
_ <- putStrLn(outStr)
} yield ()
@halcat0x15a
halcat0x15a / a.scala
Created September 3, 2011 14:00
ScalazでIteratee
sealed trait StreamG[+E]
case class Empty[E]() extends StreamG[E]
case class El[E](el: E) extends StreamG[E]
case class EOF[E]() extends StreamG[E]
sealed trait IterV[+E, +A]
@halcat0x15a
halcat0x15a / a.scala
Created September 4, 2011 19:34
ScalazでIteratee
sealed trait StreamG[+E]
case object Empty extends StreamG[Nothing]
case class El[E](el: E) extends StreamG[E]
case object EOF extends StreamG[Nothing]
sealed trait IterV[+E, +A]
@halcat0x15a
halcat0x15a / gist:1209066
Created September 11, 2011 01:50
夜中のテンションで超便利とか思ってたけど実際作ったらそんなに便利でもなかったマクロ
(use '[clojure.string :only (join split capitalize)])
(defn split-hyphen [string]
(split string #"\-"))
(defn upper-camel [string]
(join (map capitalize (split-hyphen string))))
(defn lower-camel [string]
(let [strings (split-hyphen string)]
@halcat0x15a
halcat0x15a / gist:1261213
Created October 4, 2011 09:15
Scalazの練習。GCJJのAを解いてみた。
import java.io._
import scala.io._
import scalaz._
import Scalaz._
import effects._
import IterV._
object A extends App {
val PairRegex = """(\d+) (\d+)""".r
@halcat0x15a
halcat0x15a / gist:1261217
Created October 4, 2011 09:19
output all element
def putStrLn[E: Show]: IterV[E, IO[Unit]] = {
def step(i: IO[Unit])(input: Input[E]): IterV[E, IO[Unit]] =
input(el = e => Cont(step(i >|> effects.putStrLn(e.shows))),
empty = Cont(step(i)),
eof = Done(i, EOF[E]))
Cont(step(mzero[IO[Unit]]))
}