Skip to content

Instantly share code, notes, and snippets.

View phenan's full-sized avatar

Kazuhiro Ichikawa phenan

  • Yokohama, Japan
View GitHub Profile
@phenan
phenan / Parsers.scala
Created March 9, 2016 01:20
Bottom-up packrat parser combinator supporting left recursion
package lldp
import scala.collection.immutable.PagedSeq
import scala.language.higherKinds
import scala.language.implicitConversions
import scala.language.reflectiveCalls
/**
* Created by ichikawa on 2016/03/07.
@phenan
phenan / ByteParsers.scala
Created May 19, 2017 06:20
Parser combinators for binary files (especially for Java class file)
import java.io._
import scala.util.Try
class ByteReader private (in: DataInputStream) {
def u1: Int = read(1, in.readUnsignedByte())
def u2: Int = read(2, in.readUnsignedShort())
def s1: Int = read(1, in.readByte().toInt)
def s2: Int = read(2, in.readShort().toInt)
@phenan
phenan / Parsers.scala
Created May 23, 2017 03:30
Parser combinators implemented in tagless-final style
import scala.language.higherKinds
import scalaz._
import scalaz.std.option._
import scalaz.syntax.monadPlus._
trait Parsers [Elem, C[+_], Repr[+_, _, _[_]]] {
def cond (f: Elem => Boolean): Repr[Elem, Elem, C]
def elem (e: Elem): Repr[Elem, Elem, C] = cond(_ == e)
def success [T] (r: T): Repr[T, Elem, C]
def failure (msg: String): Repr[Nothing, Elem, C]
@phenan
phenan / Tries.scala
Created October 18, 2017 14:29
scala.util.Try[T] と同様に使える Throwable \/ T の型エイリアス
import scalaz._
/**
* Tries[T] is a type alias of Throwable \/ T.
* It can be used in the same manner as scala.util.Try but it is an instance of monad unlike Try.
*
* Created by phenan on 2017/10/18.
*/
package object tries {
type Tries[+T] = Throwable \/ T
@phenan
phenan / TriesTest.scala
Created October 19, 2017 13:12
Tries.scala のテスト
import org.scalatest._
import scalaz._
import tries._
/**
* Created by phenan on 2017/10/19.
*/
class TriesTest extends FunSpec with DiagrammedAssertions {
@phenan
phenan / ByteParsers.scala
Created October 20, 2017 15:05
バイナリ用パーザコンビネータ
import java.io._
import scalaz.std.list._
import scalaz.syntax.traverse._
// Tries.scala (https://gist.github.com/phenan/63833c2cc715c96c02a4c33e17d9e2ac)
import tries._
/**
* Created by phenan on 2017/10/18.
@phenan
phenan / BytePrinters.scala
Last active October 21, 2017 14:06
バイナリ用プリンタコンビネータ(?)
import java.io._
import scalaz._
import scalaz.std.list._
import scalaz.syntax.traverse._
// Tries.scala (https://gist.github.com/phenan/63833c2cc715c96c02a4c33e17d9e2ac)
import tries._
/**
@phenan
phenan / PrinterExamples.scala
Created October 21, 2017 14:09
BytePrinters.scala の用例 (2通りの書き方)
import combinator._
object PrinterExamples extends BytePrinters {
def u2_s4_data: BytePrinter[(Int, Int)] = {
case (a, b) => u2(a) >> s4(b)
}
def length_bytes: BytePrinter[Array[Byte]] = bs => for {
_ <- u2(bs.length)
_ <- bytes(bs)
} yield ()
@phenan
phenan / StringContextTest.scala
Created February 8, 2018 02:51
StringContext の仕様を確かめるコード
case class StringContext (ss: String*) {
def s (n: Int): String = ss(n)
}
object StringContextTest {
def main (args: Array[String]): Unit = {
println(s"foo${1}bar") // print bar (not foo1bar)
}
}
@phenan
phenan / htmlInterpolation.scala
Created December 3, 2018 05:50
Simple HTML Sanitizer
object HTMLInterpolation {
// definition of tagged type
type Tagged[U] = { type Tag = U }
type @@[T, U] = T with Tagged[U]
trait Sanitized
// use tagged type to express sanitized string
type SanitizedString = String @@ Sanitized