Skip to content

Instantly share code, notes, and snippets.

@josephj
Last active May 22, 2016 07:51
Show Gist options
  • Save josephj/5581015 to your computer and use it in GitHub Desktop.
Save josephj/5581015 to your computer and use it in GitHub Desktop.

文學編程(咖啡版)

你想要用文學編程來寫程式嗎?我們先來做一個列出 1 至 10 的簡單程式吧!

結構

我們需要做一些宣告、接著以迴圈產出一個數字的陣列,最後才能列印出 1 至 10 的數字。

start = 1
end = 10
step = 1

迴圈

設定一個迴圈,將數字一個個加入到此迴圈中。

numbers = (i for i in [1..10])

輸出

假設我們已經有數字的陣列,可以用 join(",") 的方式組成一個字串、接著輸出!

console.log "這些數字是: #{numbers.join(', ')}"

使用方式

  • 安裝 CoffeeScript(你的環境需要先有 Node.js 安裝好喔)
sudo npm install -g coffee-script
  • 執行此「文學編程」,就可以看到輸出的結果!
coffee literate-coffee.coffee.md
  • 當然你也可以輸出 JavaScript 後再執行:
coffee -c literate-coffee.coffee.md
node literate-coffee.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment