Skip to content

Instantly share code, notes, and snippets.

@travisbrown
Last active August 29, 2015 13:57

Revisions

  1. travisbrown revised this gist Mar 18, 2014. 5 changed files with 76 additions and 21 deletions.
    19 changes: 19 additions & 0 deletions ArgonautDemo.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    import scalaz._, Scalaz._, concurrent.{ Future, Task }
    import argonaut._, Argonaut._

    object ArgonautDemo extends JsonDemo {
    val entitySum = DecodeJson(c =>
    entityNames.traverseU(c.get[JsonArray](_).map(_.size)).map(_.sum)
    )

    val resultReads = DecodeJson(c =>
    c.get[Json]("delete").map(_ => Result.deletion) ||| (
    (c --\ "entities").as(entitySum) |@|
    (c --\ "user" --\ "profile_background_color").as[String]
    )(Result.tweet _)
    )

    def parseLine(line: String) = new Task(Future.fork(Future.delay(
    Parse.decodeEither(line)(resultReads).leftMap(new RuntimeException(_))
    )))
    }
    28 changes: 28 additions & 0 deletions Json4sDemo.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    import scalaz.concurrent.Task
    import org.json4s._
    import org.json4s.jackson.JsonMethods.parse

    /**
    * Quick and dirty: who cares about nice error messages or being careful about
    * validation, let's just throw exceptions.
    */
    object Json4sDemo extends JsonDemo {
    def parseLine(line: String) = Task {
    parse(line) match {
    case obj: JObject =>
    if (obj.values.contains("delete")) Result.deletion else {
    val entities = obj \ "entities"

    val entityCount = entityNames.map(entities \ _).collect {
    case JArray(values) => values.size
    }.sum

    val color = obj \ "user" \ "profile_background_color" match {
    case JString(value) => value
    }

    Result.tweet(entityCount, color)
    }
    }
    }
    }
    18 changes: 18 additions & 0 deletions JsonDemo.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    import scalaz._, Scalaz._, concurrent.Task, stream.io.linesR
    import shapeless.contrib.scalaz._

    trait JsonDemo {
    val entityNames = List("hashtags", "symbols", "urls", "user_mentions")

    case class Result(deletions: Int, entities: Int, colors: Map[String, Int])

    object Result {
    val deletion = Result(1, 0, Map.empty)
    def tweet(entities: Int, color: String) =
    Result(0, entities, (color != "C0DEED") ?? Map(color -> 1))
    }

    def parseLine(line: String): Task[Result]
    def lines = linesR("../sample-all-2014-03-16.json")
    def result = lines.gatherMap(16)(parseLine).runFoldMap(identity)
    }
    30 changes: 9 additions & 21 deletions PlayJsonDemo.scala
    Original file line number Diff line number Diff line change
    @@ -1,31 +1,19 @@
    import scalaz._, Scalaz._, concurrent.Task, stream.io.linesR
    import shapeless._, contrib.scalaz._
    import scalaz.concurrent.Task
    import play.api.libs.json._, play.api.libs.functional.syntax._

    case class Result(deletions: Int, entities: Int, colors: Map[String, Int])

    object Result {
    val deletion = Result(1, 0, Map.empty)
    def tweet(entities: Int, color: String) =
    Result(0, entities, (color != "C0DEED") ?? Map(color -> 1))
    }

    object PlayJsonDemo {
    object PlayJsonDemo extends JsonDemo {
    val arraySize = __.json.pick[JsArray].map(_.value.size)
    val entitySum = (
    (__ \ 'hashtags ).json.pick[JsArray].map(_.value.size) and
    (__ \ 'symbols ).json.pick[JsArray].map(_.value.size) and
    (__ \ 'urls ).json.pick[JsArray].map(_.value.size) and
    (__ \ 'user_mentions).json.pick[JsArray].map(_.value.size)
    (__ \ 'hashtags).read(arraySize) and
    (__ \ 'symbols).read(arraySize) and
    (__ \ 'urls).read(arraySize) and
    (__ \ 'user_mentions).read(arraySize)
    )((a, b, c, d) => a + b + c + d)

    val resultReads = (__ \ 'delete).json.pick.map(_ => Result.deletion) or (
    (__ \ 'entities).read(entitySum) and
    (__ \ 'user \ 'profile_background_color).read[String]
    )(Result.tweet _)

    def result: Task[Result] = linesR(
    "../sample-all-2014-03-16.json"
    ).gatherMap(16)(
    line => Task(Json.parse(line).as(resultReads))
    ).runFoldMap(identity)
    }
    def parseLine(line: String) = Task(Json.parse(line).as(resultReads))
    }
    2 changes: 2 additions & 0 deletions build.sbt
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,8 @@ resolvers ++= Seq(

    libraryDependencies ++= Seq(
    "com.typesafe.play" %% "play-json" % "2.2.2-RC1",
    "io.argonaut" %% "argonaut" % "6.0.3",
    "org.json4s" %% "json4s-jackson" % "3.2.6",
    "org.scalaz.stream" %% "scalaz-stream" % "0.3.1",
    "org.scalaz" %% "scalaz-concurrent" % "7.0.6",
    "org.typelevel" %% "shapeless-scalaz" % "0.2-SNAPSHOT"
  2. travisbrown renamed this gist Mar 17, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. travisbrown created this gist Mar 17, 2014.
    31 changes: 31 additions & 0 deletions PlayJsonDemo.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    import scalaz._, Scalaz._, concurrent.Task, stream.io.linesR
    import shapeless._, contrib.scalaz._
    import play.api.libs.json._, play.api.libs.functional.syntax._

    case class Result(deletions: Int, entities: Int, colors: Map[String, Int])

    object Result {
    val deletion = Result(1, 0, Map.empty)
    def tweet(entities: Int, color: String) =
    Result(0, entities, (color != "C0DEED") ?? Map(color -> 1))
    }

    object PlayJsonDemo {
    val entitySum = (
    (__ \ 'hashtags ).json.pick[JsArray].map(_.value.size) and
    (__ \ 'symbols ).json.pick[JsArray].map(_.value.size) and
    (__ \ 'urls ).json.pick[JsArray].map(_.value.size) and
    (__ \ 'user_mentions).json.pick[JsArray].map(_.value.size)
    )((a, b, c, d) => a + b + c + d)

    val resultReads = (__ \ 'delete).json.pick.map(_ => Result.deletion) or (
    (__ \ 'entities).read(entitySum) and
    (__ \ 'user \ 'profile_background_color).read[String]
    )(Result.tweet _)

    def result: Task[Result] = linesR(
    "../sample-all-2014-03-16.json"
    ).gatherMap(16)(
    line => Task(Json.parse(line).as(resultReads))
    ).runFoldMap(identity)
    }
    14 changes: 14 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    scalaVersion := "2.10.3"

    resolvers ++= Seq(
    "Scalaz Bintray Repo" at "http://dl.bintray.com/scalaz/releases",
    "Sonatype OSS Snapshots" at "http://oss.sonatype.org/content/repositories/snapshots/",
    "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"
    )

    libraryDependencies ++= Seq(
    "com.typesafe.play" %% "play-json" % "2.2.2-RC1",
    "org.scalaz.stream" %% "scalaz-stream" % "0.3.1",
    "org.scalaz" %% "scalaz-concurrent" % "7.0.6",
    "org.typelevel" %% "shapeless-scalaz" % "0.2-SNAPSHOT"
    )