4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CoffeeScriptでyieldが使えるようになってた

Last updated at Posted at 2014-09-26

Nodeではオプショナルで使えるようなってるyield(generator)。CoffeeScriptのyield事情ってどうなってるのだろうと調べたら、ちょうど6日前にyield関連のコミットがされてた。

https://github.com/jashkenas/coffeescript/commit/a78cbe78a15175452a640f2d18b8585a397e41fb
https://github.com/jashkenas/coffeescript/pull/3240

なので使ってみようと思う。

#Nodeの準備
最近のNode(v0.11のいくらか)でないとgenerator使えないので入れる。nodebrew使うけどそのインストール手順は省略。

nodebrew install v0.11.14
nodebrew use v0.11.14

#CoffeeScriptの準備

npm install -g cake
git clone https://github.com/jashkenas/coffeescript.git
cd coffeescript
npm install
cake build

#サンプルスクリプト
ちょっとgeneratorの説明に適したコードが思い浮かばないので、雑ですが。function*()みたいな表記をしなくても、yieldが使われてる関数が勝手にgeneratorになるっぽい。

sample.coffee
p = console.log

g = ->
  p "  enter"
  p yield "  yld"
  p "  after yield"
  "  exit"

g1 = g()
p "start"
p g1.next().value
p "after next"
p g1.next("arg").value
p "end"

#使ってみる
Coffee経由でNodeに--harmony-generatorsを渡す。

bin/coffee --nodejs --harmony-generators sample.coffee

#結果
行ったり来たりした独特の挙動がわかると思う。

start
  enter
  yld
after next
arg
  after yield
  exit
end

#まとめ
リリースが楽しみです。

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?