Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Intro Functional Programming in JavaScript

chph
August 28, 2015

Intro Functional Programming in JavaScript

chph

August 28, 2015
Tweet

More Decks by chph

Other Decks in Programming

Transcript

  1. 'VODUJPOG *OQVUY 0VUQVUG Y f (x) = x + 3

    g (x) = x2 IUUQTXXXqJDLSDPNQIPUPTKFUPX
  2. 根據 Wikipedia 記載... • In computer science, functional programming is

    a programming paradigm—a style of building the structure and elements of computer programs—that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data. It is a declarative programming paradigm, which means programming is done with expressions.
  3. 幾個⼊入⾨門的核⼼心概念 • Declarative (vs. Imperative) • First Class Functions •

    Higher Order Functions • Composition • Closures • Recursion • Immutability
  4. 篇幅有限,有機會再討論 • Declarative (vs. Imperative) • First Class Functions •

    Higher Order Functions • Composition • Closures • Recursion # 遞迴只應天上有,凡⼈人該當⽤用迴圈? • Immutability
  5. Imperative 範例 // 蒐集每篇⽂文章的標題 var titles = []; for (i

    = 0; i < articles.length; i++) { titles.push(articles[i].title); }
  6. Declarative (宣告式) ! • 告訴電腦⺫⽬目標,⽽而⾮非描述流程 • 演算法是多個函數的結合 (Compositions) • 不可變的資料

    (Immutable data) • 最⼩小的副作⽤用 (Minimal side-effects) • Haskell, SQL, HTML, Clojure, DSLs
  7. ⼀一級函數常⾒見特性
 可以存到陣列 function circleArea(r) { return Math.PI * r *

    r; } ! function rectangleArea(l, w) { return l * w; } ! var areaFuncs = [circleArea, rectangleArea];
  8. ⼀一級函數常⾒見特性
 可以存到物件欄位 function listArticles() {} // … var blogController =

    { read: listArticles, create: createArticle, update: updateArticle, delete: deleteArticle }; console.log(blogController.read === listArticles); // true
  9. ⾼高階函數
 回傳⼀一個函數 function makeAdder(x) { return function (y) { return

    x + y; } } var add1 = makeAdder(1); console.log(add1(100)); ! // 101
  10. 合成函數 如管線般的層層傳遞 f (x) = 2 * x g (x)

    = 3 * x f (g (x)) = (f∘g)(x) = 2(3x) = 6x
  11. Function Composition
 定義合成函數 var toUpperCase = function(x) { /* ..

    */ }; var mention = function(x) { /* .. */ }; ! var compose = function (f, g) { return function (x) { return f(g(x)); } };
  12. Function Composition
 callOut(x) = mention(toUpperCase(x)) var toUpperCase = function(x) {

    return x.toUpperCase(); }; var mention = function(x) { return '@' + x; }; var compose = function (f, g) { return function (x) { return f(g(x)); } }; var callOut = compose(mention, toUpperCase); ! console.log(callOut("PIXNET digital Media”)); // @PIXNET DIGITAL MEDIA
  13. Filter 使⽤用情境
 輸出的集合⻑⾧長度⽐比輸⼊入的少 var isEven = function (n) { return

    n % 2 === 0; }; var list = [1, 2, 3, 4, 5, 6]; console.log(list.filter(isEven)); // [2, 4, 6]
  14. Reduce 使⽤用情境
 輸⼊入集合,輸出單⼀一值 也被稱作 fold var add = function (prev,

    current) { return pref + current; }; var list = [1, 2, 3]; console.log(list.reduce(add)); // 6
  15. References • Functional Programming for Java Developers 讀書摘要, https://ihower.tw/blog/archives/6305 •

    當 全世界的語⾔言 都往 Functional Programming 發展, http://www.codedata.com.tw/social-coding/paradigm-shift-to- functional-programming/ • Professor Frisby's Mostly Adequate Guide to Functional Programming, http://drboolean.gitbooks.io/mostly-adequate-guide/ • Functional Programming with JavaScript, https://speakerdeck.com/debjitbis08/functional-programming-with-javascript • Functional Programming in JavaScript, https://speakerdeck.com/anildigital/functional-programming-in-javascript • Functional programming in javascript (intro), https://speakerdeck.com/geekingfrog/functional-programming-in-javascript- intro • CodeStock: Functional Programming Basics in JavaScript, https://speakerdeck.com/jfairbank/codestock-functional- programming-basics-in-javascript • Functional and Reactive Jetpack for Javascript, https://speakerdeck.com/metrofun/functional-and-reactive-jetpack-for- javascript • Composition of Functions https://www.mathsisfun.com/sets/functions-composition.html