Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Scalaの(俺的)イケてる
ライブラリ紹介LT

 Scalaの(俺的)イケてる
ライブラリ紹介LT

2017/03/21に「Scala x AdTech Studio」というイベントのLTで発表した内容です。

Takayuki Sakai

March 22, 2017
Tweet

More Decks by Takayuki Sakai

Other Decks in Programming

Transcript

  1. Scala x AdTech Studio
    Scalaͷ(Զత)ΠέͯΔ

    ϥΠϒϥϦ঺հLT

    View full-size slide

  2. ஫ҙ
    - ScalaͰͷ։ൃܦݧ͋Δਓ޲͖ͷ಺༰
    - Akkaͷ࿩͸͠·ͤΜ

    View full-size slide

  3. ϥΠϒϥϦ঺հ

    View full-size slide

  4. Refined
    - ܕʹಠࣗͷ੍໿ΛՃ͑ΒΕΔϥΠϒϥϦ
    - Haskellͷಉ໊ɾಉػೳͷϥΠϒϥϦ

    ʹΠϯεύΠΞ͞ΕͨΒ͍͠

    View full-size slide

  5. Refined
    type UserId = Int Refined Positive
    val userId: UserId = 0 // compile error
    type Probability =
    Double Refined Closed[W.`0.0`.T, W.`1.0`.T]
    val p: Probability = 1.2 // compile error

    View full-size slide

  6. PureConfig
    - ઃఆϑΝΠϧಡΈࠐΈ༻ϥΠϒϥϦ
    - TypesafeConfigͷϥούʔ
    - case classΠϯελϯεʹϚοϓͯ͘͠ΕΔ
    - HOCON, JSONʹରԠ
    - Refinedʹ΋ରԠ

    View full-size slide

  7. PureConfig
    // development.conf (HOCONܗࣜ)

    include “application.conf”
    redis {
    host: “127.0.0.1”
    port: 6379
    }
    // Config.scala
    case class Config(redis: RedisConfig)
    case class RedisConfig(host: String, port: Int)
    val confPath = ConfigFactory.load(“development.conf”)
    val config = loadConfig[Config](confPath).get
    assert(config.redis.host == “127.0.0.1”)
    assert(config.redis.port == 6379)

    View full-size slide

  8. circe
    - JSONΛScalaͷΫϥεʹϚοϐϯά͢Δ

    - json4sΈ͍ͨͳ
    - ʮαʔγʔʯͱಡΉ
    - core෦෼͸reflectionͳ͠Ͱ࣮૷
    - ύϑΥʔϚϯεΛͪΌΜͱܭଌɾߟྀ࣮ͨ͠૷
    - RefinedʹରԠ

    View full-size slide

  9. json4s΍ΊΑ͏
    ͱ༗໊ͳ͋ͷํ΋ݴ͓ͬͯΓ·͢
    - Twitter্Ͱʮ@xuwei_k json4sʯͰݕࡧ

    View full-size slide

  10. Finch
    - finagle্Ͱಈ͘HTTP APIΛ࡞ΔͨΊͷ

    ϧʔςΟϯάϥΠϒϥϦ
    - circeͳͲͱͷ࿈ܞ͕ྑ͍
    - ଎͍

    View full-size slide

  11. Finch
    case class User(name: String, age: Int)
    val getUser: Endpoint[User] =
    get(“user” :: int) { userId: Int =>
    val user: User = User.getById(userId)
    user
    }
    val createUser: Endpoint[Boolean] =
    post(“user” :: jsonBody[User]) { user: User =>
    // create user
    true
    }
    Http.server.serve(“:80”, (getUser :+: createUser).toService)

    View full-size slide

  12. doobie
    - RDBMS΁ͷΫΤϦ݁ՌΛऔಘ
    - ScalaͷΫϥεʹϚοϐϯά΋Մ
    - ORMͰ͸ͳ͍

    - SQL͸ࣗ෼Ͱॻ͔ͳ͍ͱ͍͚ͳ͍
    - ΋͏͙͢RefinedʹରԠ

    View full-size slide

  13. doobie
    sql”SELECT * FROM users WHERE age = $age”
    .query[String]
    .process
    .take(5)
    .list
    .transact(xa)
    .unsafePerformIO
    .foreach(println)

    View full-size slide

  14. doobie
    sql”SELECT * FROM users WHERE age = $age”
    .query[String]
    .process
    .take(5)
    .list
    .transact(xa)
    .unsafePerformIO
    .foreach(println)
    ͜ͷ෦෼͸Prepared StatementΛ

    ࢖ͬͯจࣈྻૠೖ͞ΕΔͷͰɺ

    SQLΠϯδΣΫγϣϯʹ΋ڧ͍


    View full-size slide

  15. Cats
    - ؔ਺ϓϩάϥϛϯά༻ϥΠϒϥϦ
    - ScalazͷޙൃϥΠϒϥϦ
    - ༗໊ϥΠϒϥϦ͸ Scalaz -> Cats

    ʹ৐Γ׵͑ͭͭ͋Δ(ؾ͕͢Δ)

    View full-size slide

  16. ࣾ಺Ͱ͸͜Μͳ

    ϓϧϦΫΤετ΋…

    View full-size slide

  17. Typelevel.scala

    (ίϛϡχςΟ)
    - Refined, circe, Finch

    doobie, Cats

    ͳͲͷϥΠϒϥϦ͕ࢀՃ
    - ͜͜ʹ͋ΔϥΠϒϥϦ͸

    Ұ௨Γ͓͓͖͍͑ͯͨ͞

    View full-size slide