Skip to content

Instantly share code, notes, and snippets.

View gzip-sample.scala
import java.io._
import java.util.zip._
val pi = new PipedInputStream
val po = new PipedOutputStream(pi)
val zo = new GZIPOutputStream(po)
val zi = new GZIPInputStream(pi)
val w = new PrintWriter(zo)
val r = new BufferedReader(new InputStreamReader(zi))
View blog-ScalaAPIDocument-implicit-parameter.scala
case class TracerId(tid:String)
object TestApp {
implicit val tracerId = TracerId(java.util.UUID.randomUUID().toString) // tracerId can be defined and retrieved from HTTP request header.
def run(){
methodA(1)
methodB
}
def methodA(i:Int)(implicit tracerId:TracerId) = {
View blog-Scala-unapply-1.scala
case class GetArticleRequest(id:String,country:String,language:String)
def parseRequest(queryStr:String) = {
// parse arguments
val args = queryStr.split('&') map { str =>
val pair = str.split('=')
(pair(0) -> pair(1))
} toMap
// get 'id','country','language' and gen GetArticleRequest object
val resp = for{
View blog-Scala-jcwutil-ObjFormatter-datetime-sample.scala
import com.joecwu.jcwutil.ObjFormatter._
object TestApp {
def run = {
implicit def timeZone = TIMEZONE_UTC
println(System.currentTimeMillis().toStringRFC3339)
println(System.currentTimeMillis().toStringISO8601)
}
}
View blog-Scala-scalaz-either-1.scala
case class Error(code:Int, msg:String)
case class User(id:String,name:String)
object UserRepo{
def find(id:String) : Option[User] = {None}
def getUser(id:String) : Either[Error,User] = {
find(id).map(Right(_)).getOrElse(Left(Error(404,s"User not found. id:[$id]")))
}
}
View blog-Scala-scalaz-validation-1.scala
case class InvalidArgumentException(msg:String) extends Exception(msg)
case class SearchUsersRequest(keyword: Option[String], userType: Option[Int], maxNum: Option[Int], returnFields: List[String]) {
def getKeywork() = keyword | {throw InvalidArgumentException("keyword cannot be empyt.")}
def getUserType() = {
userType.map{ tp => (1 until 10 contains tp) ? tp | {throw InvalidArgumentException("userType must between 1 to 10.")}} |{ throw InvalidArgumentException("userType cannot be empyt.")}
}
def getMaxNum() = maxNum.getOrElse(10)
def getReturnFields() = (returnFields.length > 0) ? returnFields | {throw InvalidArgumentException("must provide returnFields.")}
}
View blog-publish-own-library-to-sonatype.md

Publish Artifacts to Sonatype Nexus Repository by using SBT

Install PGP plugin Reference

For sbt 0.13.5+: Add the following to your ~/.sbt/0.13/plugins/gpg.sbt file:

addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.0.0")

PGP Gen Key Reference

View blog-Scala-Scalactic.scala
case class HttpRequest(path:String, queryStrings:Map[String,List[String]]) {
def getPath() : String Or String = if(path.length>0) Good(path) else Bad("path cannot be empty.")
def getQueryStrings() : ((List[String], Int), String) Or Every[String] = {
validateKeyword(queryString.get("keyword")) zip validateNumber(queryString.get("number")) zip validateGender(queryString.get("gender"))
}
def validateKeyword(value:Option[List[String]]) : List[String] Or One[String] = {
if(value.isDefined && value.get.length>0) Good(value.get) else Bad(One("No keyword."))
}
def validateNumber(value:Option[List[String]]) : Int Or One[String] = {
try{
View blog-Scala-Monad-1.scala
val a : Option[String] = Some("abc")
if(a.isEmpty)
println("do something if a has value "+a.get)
else
println("do something if a is empty")