Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

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{
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")
import java.io.{FileOutputStream, ByteArrayOutputStream, ByteArrayInputStream}
import java.util.zip.GZIPInputStream
import java.util.zip.GZIPOutputStream
import java.io._
object GzipHelper {
implicit def StringGzipConvertor(str:String) = new {
def toGzipBytes(charset:String="UTF-8") = compress(str,charset)
def toGzipBytesString(charset:String="UTF-8") = new String(compress(str,charset).map(_.toChar))
@joecwu
joecwu / 0_reuse_code.js
Last active August 29, 2015 14:26
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console

Shortener Design Concept

Shortener

Shortener trait is an interface that defines core functions of shortener which is shorter & taller.

trait Shortener {
  def shorter(url:String)(implicit dbClient : DBClient, tracerInfo: TracerInfo) : String Or BaseException
  def taller(short:String)(implicit dbClient : DBClient, tracerInfo: TracerInfo) : String Or BaseException

}

@joecwu
joecwu / Blog-ScaVaToScala-Scalaz-Writer-Monad.md
Last active August 29, 2015 14:27
[ScaVa->Scala] Scalaz Writer Monad
@joecwu
joecwu / Blog-ScaVaToScala-Scalaz-Task.md
Last active August 29, 2015 14:27
[ScaVa->Scala] Scalaz Task 取代Scala Future來進行非同步處理的另一個選擇
published tags
true
ScaVaToScala
Scala
Scalaz
Programming

[ScaVa->Scala] Scalaz Task 取代Scala Future來進行非同步處理的另一個選擇

這篇同步發佈在我的BlogGist

Scala的Future用起來有什麼問題?

在Scala中,我們要處理asynchronous computations的時候應該都知道有Future這個好用的東西,但Future monad常讓我在進行error handling的時候有點困擾,我本來的回傳值若是有"成功"或"失敗"兩種case的時候,我們可以用Scalaz的disjuction或是Scalactic的Or來處理,但是加上Future後的結果會變成:

@joecwu
joecwu / Blog-Docker-Push-Error.md
Created November 21, 2015 16:50
[Docker] Push Error
published tags
true
Docker

[Docker] Push Error

這篇同步發佈在我的BlogGist

背景

@joecwu
joecwu / Blog-ScalaInDepth-Scala-Case-Classes.md
Last active December 19, 2016 06:13
[Scala In Depth] Scala Case Classes
published tags
true
ScalaInDepth
Scala
Programming

[Scala In Depth] Scala Case Classes

這篇同步發佈在我的BlogGist

這篇文章,基本上是參考這篇Scala Case Classes In Depth來寫的,閱讀吸收後以我的例子與描述方式以中文寫下。

@joecwu
joecwu / mongodb-s3-backup.sh
Created July 6, 2017 06:18 — forked from eladnava/mongodb-s3-backup.sh
Automatically backup a MongoDB database to S3 using mongodump, tar, and awscli (Ubuntu 14.04 LTS)
#!/bin/sh
# Make sure to:
# 1) Name this file `backup.sh` and place it in /home/ubuntu
# 2) Run sudo apt-get install awscli to install the AWSCLI
# 3) Run aws configure (enter s3-authorized IAM user and specify region)
# 4) Fill in DB host + name
# 5) Create S3 bucket for the backups and fill it in below (set a lifecycle rule to expire files older than X days in the bucket)
# 6) Run chmod +x backup.sh
# 7) Test it out via ./backup.sh