Skip to content

Instantly share code, notes, and snippets.

@pocketberserker
Created March 27, 2016 04:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pocketberserker/6c618ecbd8244bc61a66 to your computer and use it in GitHub Desktop.
Save pocketberserker/6c618ecbd8244bc61a66 to your computer and use it in GitHub Desktop.

scodec紹介

自己紹介

icon

  • なかやん・ゆーき / ぺんぎん / もみあげ
  • @pocketberserker / id:pocketberserker
  • 仕事でScalaを使うようになったという噂

scodecとは

https://github.com/scodec/scodec

バイナリデータを解析するためのコンビネータライブラリ。

下記のような制約の元、実装されている。

  • プロトコル定義を書き写せる、かつ読みやすいように
  • purely functionalなエンコーディング、デコーディング
  • バイナリ構造から型へのマッピングは静的に検証されるべき
  • コンパイラプラグインを使わない

依存ライブラリ等

  • scodec-bits
  • shapeless
  • macro paradise(Scala 2.10の場合)
  • 1.7.xからscalaz非依存になった
  • 1.8.xからScala.jsサポート

必要なもの

  • HList, Coproductをはじめとしたshapelessの知識
    • なくても書けないことはないが、嵌ったときにつらい
  • コンパイル時間

Introductionより

https://github.com/scodec/scodec#introduction

今回必要そうな部分のみ抜粋

import scodec._
import codecs._

case class Point(x: Int, y: Int, z: Int)

val pointCodec = (int8 :: int8 :: int8).as[Point]

https://github.com/scodec/scodec/blob/2736f9e7db81d94cb2b9256fd8958851a2266616/shared/src/main/scala/scodec/package.scala#L269 https://github.com/scodec/scodec/blob/2736f9e7db81d94cb2b9256fd8958851a2266616/shared/src/main/scala/scodec/package.scala#L130

https://github.com/scodec/scodec/blob/2736f9e7db81d94cb2b9256fd8958851a2266616/shared/src/test/scala/scodec/examples/DerivedCodecExamples.scala

作ってみたもの

https://github.com/pocketberserker/scodec-msgpack

  • scodecを初めて触ったときに作った
    • このときはshapelessの知識がほとんどなかった
  • 現在の最新版ではCoproductで自動生成したりしている
  • 効率とか考えていないので遅いはず(噂では100倍)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment