Last active
August 29, 2015 13:57
Revisions
-
travisbrown revised this gist
Mar 18, 2014 . 5 changed files with 76 additions and 21 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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(_)) ))) } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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) } } } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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) } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,31 +1,19 @@ import scalaz.concurrent.Task import play.api.libs.json._, play.api.libs.functional.syntax._ object PlayJsonDemo extends JsonDemo { val arraySize = __.json.pick[JsArray].map(_.value.size) val entitySum = ( (__ \ '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 parseLine(line: String) = Task(Json.parse(line).as(resultReads)) } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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" -
travisbrown renamed this gist
Mar 17, 2014 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
travisbrown created this gist
Mar 17, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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) } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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" )