published | tags | ||||
---|---|---|---|---|---|
true |
|
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._ | |
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)) |
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
val list=List(1,2,3,4,5) | |
//list: List[Int] = List(1, 2, 3, 4, 5) |
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
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) = { |
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
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{ |
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 com.joecwu.jcwutil.ObjFormatter._ | |
object TestApp { | |
def run = { | |
implicit def timeZone = TIMEZONE_UTC | |
println(System.currentTimeMillis().toStringRFC3339) | |
println(System.currentTimeMillis().toStringISO8601) | |
} | |
} |
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
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]"))) | |
} | |
} |
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
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.")} | |
} |
OlderNewer