Created
May 17, 2012 21:42
-
-
Save xuwei-k/2721739 to your computer and use it in GitHub Desktop.
java either monad
This file contains 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
libraryDependencies ++= Seq( | |
"org.codehaus.jackson" % "jackson-mapper-asl" % "1.9.7" | |
,"org.functionaljava" % "functionaljava" % "3.1" | |
) |
This file contains 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 java.io.BufferedReader; | |
import java.io.InputStreamReader; | |
import java.io.InputStream; | |
import java.io.IOException; | |
import java.net.URL; | |
import org.codehaus.jackson.JsonNode; | |
import org.codehaus.jackson.map.ObjectMapper; | |
public class MainJava{ | |
public static void main(String[] args){ | |
final String url = "http://search.twitter.com/search.json?q=scala"; | |
try{ | |
final String jsonString = get(url); | |
final JsonNode json = parse(jsonString); | |
System.out.println(json); | |
}catch(IOException e){ | |
e.printStackTrace(); | |
} | |
} | |
static final JsonNode parse(final String jsonString)throws IOException{ | |
return new ObjectMapper().readValue(jsonString,JsonNode.class); | |
}; | |
static final String get(final String urlString)throws IOException{ | |
final URL url = new URL(urlString); | |
final InputStream content = (InputStream)url.getContent(); | |
final BufferedReader bf = new BufferedReader(new InputStreamReader(content)); | |
String line; | |
final StringBuilder sb = new StringBuilder(); | |
while ((line = bf.readLine()) != null){ | |
sb.append(line); | |
} | |
return sb.toString(); | |
} | |
} |
This file contains 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 java.io.BufferedReader | |
import java.io.InputStreamReader | |
import java.io.InputStream | |
import java.net.URL | |
import org.codehaus.jackson.JsonNode | |
import org.codehaus.jackson.map.ObjectMapper | |
object MainScala{ | |
def main(args:Array[String]){ | |
val url = "http://search.twitter.com/search.json?q=scala" | |
get(url).right.flatMap(parse).fold( | |
_.printStackTrace | |
, | |
println _ | |
) | |
} | |
val parse = (jsonString:String) => | |
try{ | |
Right(new ObjectMapper().readValue(jsonString,classOf[JsonNode])) | |
}catch{ | |
case e => Left(e) | |
} | |
val get = (urlString:String) => | |
try{ | |
val url = new URL(urlString) | |
val content = url.getContent().asInstanceOf[InputStream] | |
val bf = new BufferedReader(new InputStreamReader(content)) | |
var line:String = null | |
val sb = new StringBuilder() | |
while ({line = bf.readLine();line} != null){ | |
sb.append(line) | |
} | |
Right(sb.toString) | |
}catch{ | |
case e => Left(e) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment