ããããç¶ç¶ã®åå¼·ãããããªã¨æã£ã¦Rhinoããã¦ã³ãã¼ããæè¿ã®Rhinoã«ã¯ç¶ç¶ã®æ©è½ãçµã¿è¾¼ã¾ãã¦ããã®ã ãLisp風ã®æ§æãè¦æãªäººãç¶ç¶ãåå¼·ãããå ´åã¯è¦ãã§ãã¯ã ã¨æãã
ä»å¹´ã¯ããã£ã¨ãããä½ããã¨æã£ã¦ããã®ã§ãç°¡åãªè§£èª¬ãæ¸ãã¦ã¿ããèªåã¯ç¶ç¶ã«ã¤ãã¦ãã¾ãããããã£ã¦ããªãã®ã§ã使ãæ¹ãééã£ã¦ãããããããªããä½ãããã°ãææãã ããããã£ã¨å
容ãå
å®ããã°ãªãã¸ã§ã¯ãã®åºå ´ã«å¯ç¨¿ããããªã
Continuationãªãã¸ã§ã¯ãã®ä½¿ãæ¹
ç¶ç¶ã®æ©è½ã¯çµã¿è¾¼ã¿ã®Continuationãªãã¸ã§ã¯ãã«éç´ããã¦ãããContinuationãä½æããå¼ã³åºãã ãã§å©ç¨å¯è½ã
- ããé¢æ°å
ã§Contuinuationãªãã¸ã§ã¯ããä½æãã¦ãé©å½ãªå¤æ°ã«ä»£å
¥ããã°ãã®æç¹ã®ç¶ç¶ãä¿åããã
å¤åããã®é¢æ°ãå¼ã³åºãã¾ã§ã®ã³ã¼ã«ã¹ã¿ãã¯ã®æ å ±ãä¿åããããã¨ããã¤ã¡ã¼ã¸ä¿®æ£:ãã®Continuationãçæããé¢æ°ãå®è¡ãã¦returnããå¾ã«è©ä¾¡ãã¹ãå¼(=ç¶ç¶)ãContinuationã¨ãã¦ä¿åãããã¤ã¡ã¼ã¸ã
- ä½æãããContinuationãªãã¸ã§ã¯ãã¯é¢æ°ã¿ããã«å¼ã³åºããããããã¨ãã§ããã
- ãã®ç¬éã«ç¶ç¶ãä¿åããé¢æ°ãå¼ã³åºããç´å¾(ã¡ã¨ãããã¥ãã)ã¾ã§ãã¸ã£ã³ãããã
- Continuationã®å¼ã³åºãæã«å¼æ°ãã¤ããã¨ããã®æ
å ±ãContinuationãä¿æãã¦ããå¤æ°ã«ç½®ãæãã
- ç¶ç¶ãåé?ããã¨ãã«æ å ±ãåã渡ããããªãã ãããã©ããããã¥ããã£ãããããªãå¤æ°ã®ä¸èº«ãå ¥ãæ¿ãã£ã¦ããã¯ãªã
ããã ããã¸ã§ã¸ã§ç´³å£«ã®æ¹ã ã«ãããããã説æããã¨ãç¶ç¶ã¯ç¬¬åé¨ã®ãã¤ãã¡ã»ãã¹ãã®è½åã«ä¼¼ã¦ããã!ãå·å°»æ©äººå½¹ãContinuationãªãã¸ã§ã¯ãã¨ããããããããããã¥ãããã
ä¾
以ä¸ã®ã³ã¼ãã¯Rhino 1.6R2ã§ãã¹ããã¦ãããããã¥ã¡ã³ãã«ããã¨Continuationãªãã¸ã§ã¯ãã®ä»æ§ã¯å°æ¥å¤æ´ããå¯è½æ§ããããããã®ã§æ³¨æãã¡ãªã¿ã«ãã®ãã¼ã¸ã§ã³ã®Rhinoã§ã¯èµ·åæã«ãªãã·ã§ã³æå®ããªãã¦ãç¶ç¶ã®æ©è½ãå©ç¨å¯è½ã«ãªã£ã¦ããã*1
var top = new Continuation(); //ãã®ä¸é£ã®ã¹ã¯ãªãããå®è¡ãã¦ãããããã¬ãã«ã®é¢æ°(ããã¯ä½ã ãã?)ã //å¼ã³åºããå¾ã«å®è¡ãããã¨(ç¶ç¶)ãä¿åãã //å®éã«ã¯é¢æ°å ããè±åºããããã«å®è¡ãã var current; //é¢æ°å ã®ç¶ç¶ãä¿æããå¤æ°ãå®£è¨ function capture() { current = new Continuation(); //ãã®captureé¢æ°ãå¼ã³åºããå¾ã«ããäº(=ç¶ç¶)ãcurrentã«ä¿åãã top(); //é¢æ°ããè±åºãã } function foo() { while(true) { print("haru"); capture(); //capture()å ã§currentã«ç¶ç¶(次ã®è¡ä»¥éã«ãã£ã¦ããã¨)ãä¿åãã //capture()å ã§ã¯top()ãå¼ã³åºããã¦ãããããçµæfooããè±åºãããã¨ã«ãªã print("natu"); capture(); print("aki"); capture(); print("fuyu"); capture(); } }
//å¼ã³åºãã³ã¼ã js> foo() haru js> current() //currentã«ä¿æããç¶ç¶ãå¼ã³åºãã¦ãã natu js> current() aki js> current() fuyu js> current() haru
captureé¢æ°ã§ã¯captureé¢æ°ãå¼ã³åºããã¨ããã¾ã§ã®ã³ã¼ã«ã¹ã¿ãã¯ãcurrentã«ä¿åãããããã¬ãã«ã®ç¶ç¶ã«ã¸ã£ã³ããã¦ãããfoo()å
ã§captureãå¼ã³åºãã¨ããã®å¦çããããªã£ã¦foo()ããéä¸è±åºããããã®å¾ãcurrent()ã¨ãã¦å¼ã³åºãã¨ãcaptureãå¼ã³åºããç´å¾ããåéãã¦æ¬¡ã®capture()ã¾ã§å®è¡ããã
ããã¾ã§æ¸ãã¦ããã¦currentã®å¤æ°åãnextã¨ãã¦ããã°ããã£ããã¨ã¡ãã£ã¨å¾æãååéè¦!
ææ
foo()é¢æ°ã¯ãæ¥ãå¤ãç§ãå¬ã¨ããç¶æ
ãé·ç§»ããããã種ã®ã¹ãã¼ããã·ã¼ã³ã«ãªã£ã¦ããããã®å ´åã¹ãã¼ããç¶ç¶ã«ä¿æãã¦ãããã¨ã«ãªããç¶ç¶ã«ãã£ã¦ãã¹ãã¼ããã·ã¼ã³ãå¿
è¦ã¨ãããã¸ãã¹ãã¸ãã¯ã®ããã¼ãèªç¶ã«è¨è¿°ã§ããããã¡ãã£ã¨DSLçãªè¡¨è¨ãå¯è½ã ã¨æããããã¯ãã¯ãã«ãªæ©è½ã§ãã奥ãã!
âãã¡ãã¯(å³å¯ã«ã¯ç¶ç¶ã§ã¯ç¡ã)Pythonã®ã¸ã§ãã¬ã¼ã¿ã®ä¾ã ãã©ããã®æã®èãæ¹ããããè¨äºã
ジェネレーターによるステート・マシン
ããã«ãã¦ãjarä¸ã¤ã§æ軽ã«æ±ããRhinoã¯ãããã¼ãJava6ã«çµã¿è¾¼ã¾ããããåå¼·ãã¦ããã¦æã¯ç¡ãæãã
*1:æã¯ãªãã·ã§ã³æå®ãå¿ è¦ã ã£ãã¿ãã