這個議程會使用 TypeScript 介紹 functional programming 中的常見概念與在 web front-end 中的應用。選用 TypeScript 是因為它的 type system 雖然不夠好,但已經足夠讓我們以 type 為出發點思考問題。同時它也有 JavaScript 的靈活性。
Sum Type 和 Product Type
在像 Haskell 或是 OCaml 這種 ML 系的 FP 語言中,常會看到由 sum type 和 product type 組合而成的 algebraic data type ,例如:
data List a = Nil | Cons a (List a)
在 JS 裡我們沒有 sum type 和 product type ,但我們可以模擬一下,並用 TypeScript 描述它們:
interface Nil {