Skip to content

Instantly share code, notes, and snippets.

@humorless
Last active November 5, 2022 09:42
Show Gist options
  • Save humorless/a48cfec3c11f55c037884820f5e80bb8 to your computer and use it in GitHub Desktop.
Save humorless/a48cfec3c11f55c037884820f5e80bb8 to your computer and use it in GitHub Desktop.
Clojure 速成

為什麼要用 Clojure 語言來開發軟體?

在 Clojure 社群的一個說法:「同樣的工程師使用 Java 來開發軟體、使用 Nodejs 來開發軟體、使用 Clojure 來開發同樣軟體所花費的時間,大約是:5:3:1。」

Part 1: Basic

lesson 1: syntax

  1. truth, math, string

  2. Intros to list, vector, map, set

  3. conj: conj list ,conj vector ,conj map ,conj set

  4. Intros to sequence, function

lesson 2: 函數與控制結構

  1. function 的練習 double down https://4clojure.oxal.org/#/problem/15

  2. if 的練習 https://4clojure.oxal.org/#/problem/162 判斷 nil value https://4clojure.oxal.org/#/problem/134

  3. 教 map/filter/reduce

繪圖來說明、函數是如何運作的。 reduce 要使用「魚骨圖」

lesson 3: 區域變數

  1. 教 let https://4clojure.oxal.org/#/problem/36 https://4clojure.oxal.org/#/problem/35

  2. 教 vector destructuring https://4clojure.oxal.org/#/problem/52

  3. 教 map destructuring

  4. 教 non-destructuring 的方式取得 collection 的內容

    • nth, get
    • keyword as function

lesson 4: recursive

  1. vocabulary: when / dec /inc
  2. simple recursive
    https://4clojure.oxal.org/#/problem/57
  3. 條件判斷: empty?, zero?, nil?, contains?
  4. 容器操作: concat, merge
  5. 寫遞迴函數三原則:
    1. 要去想,什麼是終止條件
    2. 要去想,怎麼遞迴 
    3. 在做 informal reasoning 的時候,要切換 purpose view
  6. recursive 與「數學歸納法」的關系~
  7. 函數命名原則
  8. 費波納西 & memoize

相關練習:
Last
https://4clojure.oxal.org/#/problem/19

Penultimate
https://4clojure.oxal.org/#/problem/20

Nth
https://4clojure.oxal.org/#/problem/21

Count a sequence
https://4clojure.oxal.org/#/problem/22

Reverse a sequence
https://4clojure.oxal.org/#/problem/23

lesson 5 sequence

  • 解釋解題的原則
    1. Solve the question by remembering the Clojure function name. For example,
      frequencies.
    2. Solve the question by using some sequence questions: map, filter
    3. Solve the question by using advanced sequence skill: dec dimension (mapcat), inc dimension (range)
    4. Solve the question by using reduce.
    5. Solve the question by using recursion.
    6. Solve the question by using mutual recursion. (interpreter)
  • Abstraction level: 以 BFS 為例,考慮以下列三種不同的方式來實作
    • Imperative Programming
    • Functional Programming - immutable collection/sequence abstraction & library
    • Declarative Programming - treewalk interpreter & DSL/ leverage embedded SQL
  • 解釋 seq abstraction 的重要性, 比較 lodash library

Part 2: Local Environment

lesson 6 開發環境

  1. git
  2. neovim
  3. tmux
  4. clj-kondo
  5. cljfmt
  6. babashka
  7. sdkman (用來安裝 java)
  8. Clojure CLI

lesson 7 REPL-driven development

  1. Conjure
  2. vim-sexp
  3. go to definition

Part 3: Real World

lesson 8 program structure & side effects

  1. namespace, require, def, defn
  2. 何謂 side effects: slurp, print
  3. Dependency Injection
  4. lazy evaluation 與 dorun

;; write a hello world using babashka

lesson 9 literals, format, and hash-map

  1. EDN format, edn/read-string
  2. string, keyword, symbol
  3. the manipulation of hash-map assoc, dissoc, merge, empty, into, zipmap

;; write a CLI program reading config.edn

lesson 10 idioms & skills

  1. variable naming: x, xs, coll, m, ms, f
  2. Tree manipulation: postwalk
  3. Functional programming 常用函數: mapcat, partition, group-by, apply

;; write a CLI program using above skill

lesson 11 debug

  1. Clojure error message
  2. How to debug

lesson 12 a real world case study

Reference

@humorless
Copy link
Author

Online REPL for quick start

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment