Last active
December 14, 2015 10:58
-
-
Save halcat0x15a/5075401 to your computer and use it in GitHub Desktop.
Machines and Argonaut
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 characters
import scala.util.Try | |
import scalaz._, Scalaz._ | |
import scalaz.effect._, IO._ | |
import com.clarifi.machines._ | |
import argonaut._, Argonaut._ | |
object Sample extends SafeApp { | |
def getUser = jArrayPL.getK >==> (_.headOption) | |
def text = jObjectPL >>> jsonObjectPL("status") >>> jObjectPL >>> jsonObjectPL("text") >>> jStringPL | |
def parse(json: String) = json.parse.fold(Some.apply, getUser >=> text.getK).orZero | |
def lookup = new Driver[IO, String => Any] { | |
val M = Monad[IO] | |
def apply(k: String => Any) = for { | |
name <- readLn | |
url = s"http://api.twitter.com/1/users/lookup.json?screen_name=$name" | |
} yield Try(scala.io.Source.fromURL(url).mkString).toOption | |
} | |
def loop = new Procedure[IO, String] { | |
type K = String => Any | |
val machine = Process(parse) | |
def withDriver[R](k: Driver[IO, K] => IO[R]) = k(lookup) | |
} | |
override def runc = loop foreach putStrLn | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment