Skip to content

Instantly share code, notes, and snippets.

@joecwu
joecwu / Blog-ScaVaToScala-Scalaz-Task.md
Last active Aug 29, 2015
[ScaVa->Scala] Scalaz Task 取代Scala Future來進行非同步處理的另一個選擇
View Blog-ScaVaToScala-Scalaz-Task.md
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-ScaVaToScala-Scalaz-Writer-Monad.md
Last active Aug 29, 2015
[ScaVa->Scala] Scalaz Writer Monad
View Blog-ScaVaToScala-Scalaz-Writer-Monad.md
@joecwu
joecwu / Blog-Scala-ReaderMonad-DI.md
Last active Aug 29, 2015
Scala中使用Reader Monad來實現Dependency Injection
View Blog-Scala-ReaderMonad-DI.md
title published tags
Scala中使用Reader Monad來實現Dependency Injection
true
ScaVaToScala
Scala
Scalaz
Programming
shortener

[ScaVa->Scala] Scala中使用Reader Monad來實現Dependency Injection

前言

此篇文章同時發步於BlogGist

View Blog-ScaVa2Scala-Functor-Applicative-Monad-Semigroup-Monoid.md

Category theory(範疇論)之什麼是Functor, Applicative, Monad, Semigroup, Monoid?

之前曾經寫過一篇 什麼是Monad? ,裡面提到了對於Monad的解釋,而今天進一步解釋這些在Functional Programming中常見到的詞:Functor, Applicative, Monad, Semigroup, Monoid。 透過放在一起比較,讓我們更清楚這些之間的差別與關係。

##Functor

在提到Functor之前,請先了解一下scala中map的作用,map可以讓你傳入一個function,而透過這個function是可以從F[A]轉換成F[B],這裡的F不管他(就是代表外面的包裝不會變),而裡面的A型態轉成B型態。

例如:

@joecwu
joecwu / introduction-to-scala-dev.md
Last active Jul 30, 2018
[ScaVa->Scala] Introduction to Scala Development
View introduction-to-scala-dev.md
published tags
true
ScaVaToScala
Scala
Scalaz
Programming

[ScaVa->Scala] Introduction to Scala Development

前言

這件文件是在公司內部向一些新踏入Scala領域的同事們介紹所使用的資料,因此適合有程式開發經驗,並且想Scala領域中的朋友們。 底下列了一些學習Scala的資源、並列出一些我們自己開發常用到的Libraries並適合新手來學習使用的。

View blog-shortener-design-concept.md

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 / 0_reuse_code.js
Last active Aug 29, 2015
Here are some things you can do with Gists in GistBox.
View 0_reuse_code.js
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
View blog-Scala-Dispatch-gzip-helper.scala
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))
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")
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{