ã¸ã§ãã¬ã¼ã¿ 㨠ã³ã«ã¼ãã³
ã¸ã§ãã¬ã¼ã¿
ã¸ã§ãã¬ã¼ã¿(Generator)ã¨ã¯ãã¤ãã¬ã¼ã¿ã³ã³ãããªã¤ã³ã¿ã¼ãã§ã¤ã¹ãæã¤ãã©ã æãã¹ãã³ã¬ã¯ã·ã§ã³ãããããã§ããªãããã®ããã³ã« å¤ãä½ãåºãã¦è¿ããããªã¢ããä½ãã¢ããããã¾ãã
Python ã§ã¯ãyield å¥ãå«ã é¢æ°ã¯ ã¸ã§ãã¬ã¼ã¿ã¨ãªãã¾ãã
def myGenerator: yield 1 yield 2 yield 3 gen = myGenerator() for i in gen: print i
ä¸è¨ã³ã¼ãä¸ã® gen ã¯ã¤ãã¬ã¼ã¿ã¨åãã¤ã³ã¿ã¼ãã§ã¤ã¹ãæã£ã¦ãã¦ã next() ãå¼ã³åºãã°æ¬¡ã®yield ãè¿ã gen ã®æå¾ã¾ã§å®è¡ãã㨠StopIteration ä¾å¤ãçºçããã¾ãã
ã¤ã¾ã for æã使ããªãå ´åã¯ä»¥ä¸ã®ããã«ãªãã¾ãã
print gen.next() # "1" print gen.next() # "2" print gen.next() # "3" print gen.next() # ãã㧠StopIteration ä¾å¤ãçºç
ä½ããã¸ã§ãã¬ã¼ã¿ã¯ã«ã¼ãã§åãäºãåæã¨ãã¦ã¤ã³ã¿ã¼ãã§ã¤ã¹ãæ´ãããã¦ãã¾ãã ä¸çªæåã®ã³ã¼ããã説æã®ãããå¤ã次ã ã¨è¿ãã¢ãããä¸åº¦å¤æ°ã«æç¸ãã¦ãã¾ããã ããã¦ãã¯ãããªé¢¨ã«ã
def myGenerator: yield 1 yield 2 yield 3 for i in myGenerator(): print i
ç´æ¥ ã¤ãã¬ã¼ã¿ãè¦æ±ããã¨ããã§å¼ã³åºãã¾ãã ã¨ãããã¾ã§æ¸ãã° Pythoner ããã¤ããä¸è©±ã«ãªã£ã¦ãã range()é¢æ°ãããä¼¼ã¦ãããã¨æ°ãä»ãã¨æãã¾ãã*1
ã¸ã§ãã¬ã¼ã¿è¡¨ç¾
ååãªã¹ãå å 表ç¾ã§æ¸ãã Sumãæ±ããå¦ç
sum( [ ord(i) for i in "hogehogehoge" ] ) & 0xFF
ã¯ããªã¹ãå å 表ç¾ã§ç¤ºãããæ°ããªãªã¹ããä¸åº¦çæãã¦ããã ãã®ãªã¹ãã«å¯¾ã㦠sum() ãè¡ãã¾ãããããã¯ã¡ã¢ãªã¨CPU ã®ç¡é§é£ãã ãã¨ããããã§ã ãªã¹ãã§ã¯ãªãã¸ã§ãã¬ã¼ã¿ãçæãã ã¸ã§ãã¬ã¼ã¿è¡¨ç¾ ã Python 2.4 ããå°å ¥ããã¾ãããã¸ã§ãã¬ã¼ã¿è¡¨ç¾ã§ä¸è¨ã³ã¼ããæ¸ãç´ãã¨ã
sum( ord(i) for i in "hogehogehoge" ) & 0xFF
ã¨ãªãã¾ããå®ã¯ 丸ã«ãã³ã®ä¸ã« ãªã¹ãå å 表ç¾ã®ãããªè¨è¿°ãããã¨ã ç¡åã®ã¸ã§ãã¬ã¼ã¿ãä½ããããã³ã¼ã«ããããã«ãªãã¾ãã
ã¸ã§ãã¬ã¼ã¿è¡¨ç¾ã¯ãªã«ãé¢æ°ã®å¼æ°ãªã¹ãã®ä¸èº«ã ãã«éå®ãããããã§ã¯ããã¾ããã 丸ã«ãã³ã§å²ã¾ãã¦ããã°ããã®ã§ãã ãªã®ã§ã以ä¸ã®ããã«çæããã ã¸ã§ãã¬ã¼ã¿ãå¤æ°ã«æç¸ãããã¨ãã§ãã¾ãã ï¼ãã¡ã¼ã¹ãã¯ã©ã¹ã®ãªãã¸ã§ã¯ãã ã¨ãããã¨ã§ãï¼
>>> gen = (i for i in range(0,16)) >>> gen.next() 0 >>> gen.next() 1 >>> gen.next() 2 >>> gen.next() 3 ã» ã» ã» >>> gen.next() 15 >>> gen.next() Traceback (most recent call last): File "<pyshell#19>", line 1, in -toplevel- gen.next() StopIteration
ã¤ã¾ãã gen = (i for i in range(0,16)) ã¯
def myGenerator(): for i in range(0,16): yeild i gen = myGenerator()
ã¨å義ã§ãã ã¸ã§ãã¬ã¼ã¿è¡¨ç¾ã¯ ç¡åã®ã¸ã§ãã¬ã¼ã¿ãã¾ãã¯ã¸ã§ãã¬ã¼ã¿ã®ãªãã©ã«è¡¨è¨ã¨è¦ããã¨ãåºæ¥ã¾ãã
ã³ã«ã¼ãã³
ã¸ã§ãã¬ã¼ã¿ãä½ããå¤ã次ã ã«è¿ãã¢ããã¯ãå³ä»ãããã¤ãã¬ã¼ã¿é¢¨ã§ããã æ¬è³ªçã«ã¯ ã³ã«ã¼ãã³(coroutine) ã§ãã ã³ã«ã¼ãã³ã¨ã¯ããå®è¡ã®éä¸ã§ãªã¿ã¼ã³ã§ãã次åã³ã¼ã«æã«ã¯ããããå¦çãåéãããã¨ãåºæ¥ãã¢ããã§*2ã ãã¡ã¤ã³ã¨ãµããã¨ããé¢ä¿ãæããªãã«ã¼ãã³ã示ããã¾ãã
Python ã®ã¸ã§ãã¬ã¼ã¿ãä½ãã¢ãã¯ãã³ã¼ã«åºæ¥ãªã(.__call__() ã§ã¯ãªãã.next() ãå¼ã¶ï¼ã®ã§ã ç´æçã« ã³ã«ã¼ãã³ ããã¦è¦ãã¾ããã
ãªããPythonãç´æ¥ã³ã«ã¼ãã³ãå®ç¾©ããã§ã¯ãªããããã®ãã¡ã¯ããªãå®ç¾©ããããã¨ããã¨ã ããããã¨ã³ã«ã¼ãã³ãå¼æ°ãæã£ãã¨ãã®æåã ç´æçã§ãªããªãããã»ã»ã¨ããã®ãããã¨æãã¾ãã
http://mono.kmc.gr.jp/~oxy/w/hiki.cgi?Coroutineããã®ä¾ãã®ã¾ã¾ã®åç¨ã§ããããããPythonãç´æ¥ ãå¤ã次ã ã«è¿ãã¢ãããå®ç¾©åºæ¥ãå ´åã 以ä¸ã®ã³ã¼ãã¯ã©ãåããã¯ãç´æçã«ã¯ãããã¾ããã
''' Python ã¨ãã¦æå¹ãªã³ã¼ãã§ã¯ãªãã ''' def myCoroutine( initval ): yield initval yield initval + 1 yield initval + 2 print myCoroutine( 5 ) # "5" print myCoroutine( 3 ) # "6"? ããã¨ã "4"? print myCoroutine( 10 ) # "7"? ããã¨ã "12"?
Python ã® ã¸ã§ãã¬ã¼ã¿ã§ä½ãããã³ã«ã¼ãã³ã¯ï¼.__call__(...) ã§ã¯ãªã .next() ã§å¼ã°ããã®ã§ï¼ å¼æ°ãç¡ãã¢ãã§ã¯ç¡ãã¦ã¯ãªããªãã¨ããå¶ç´ãããã¾ãããããã§ãã®æ··ä¹±ããç¡ç¸ã§ãã ã¸ã§ãã¬ã¼ã¿ã«å¼æ°ã渡ãã¨ãããã¯ã³ã«ã¼ãã³ã®çææå¼æ°ã§ããããã¾ããã
Python ã®ã¸ã§ãã¬ã¼ã¿ã¯ ã³ã«ã¼ãã³ãã¡ã¯ããªã§ãããã¸ã§ãã¬ã¼ã¿ãã«ãªã¼åãã¦å¼æ°ãã¼ãã«ãããã®ã ã³ã«ã¼ãã³ ã¨ãè¨ããããï¼ç«ã®é¢æ°è¨èªã®ç¥èã¯ããªãã¡ãã£ã¦ããªã®ã§æå¾æ¥ã®æ¹åãªãã¨ãè¨ã£ã¦ãããã»ã»ï¼
以ä¸ããªãã ãã¢ã¤ãã¦ãã Python ã® ã¸ã§ãã¬ã¼ã¿ã«ã¤ãã¦ãèªååãã«æ©åãããã¾ã¨ãã¦ã¿ã次第ã§ãã
ä½è«
Python ã®ã¸ã§ãã¬ã¼ã¿è¡¨ç¾ã¯ãæ¸ãã¦ããã¨ãã¯ç´æçã§æ°æã¡ããã®ã§ãããå°ããã£ã¦ããè¦ç´ãã¨çµæ§èªã¿ã¥ããæãã¾ãããªã¹ãå å 表ç¾ã®ã¨ãã¯ããã¯æãã¾ããã§ããã
ãªãã§ããªããã¨èããã®ã§ããããªã¹ãå å 表ç¾ãã¸ã§ãã¬ã¼ã¿è¡¨ç¾ã ä»ã®Python ã®ã·ã³ã¿ãã¯ã¹ã¨ã¯ã¡ãã£ã¨ç°è³ªã§ãé ã®ä¸ã§ææ³ãã¹ã¤ãããã¦èªãã§ãããããªã®ã§ããããªã¹ãå å 表ç¾ã®å ´å㯠è§ã«ãã³å ã® for ã¨ã if ãæªç®ç«ã¡ãã¦ãã®ã§(ç¬)ããããããªã¬ã«è³ã¹ã¤ãããåãæ¿ããã®ã§ãããã¸ã§ãã¬ã¼ã¿è¡¨ç¾ã¯ä¸è¦èªç¶ã«é¦´æãã§ãã¾ãã®ã§ èªã¿ã¥ããæããããã§ãã
ããã¯æ £ããããã
*1:ã§ã rangeé¢æ°ã¯ ã¸ã§ãã¬ã¼ã¿ã§ã¯ãªããæ¬å½ã«ãã®ç¯å²ã®å¤ãå ¨ã¦æã¤ãªã¹ããã¤ãã£ã¦è¿ããããã¾ãã ã ããä¾ãã° for i in range(0,999999999): ãªãã¦åºæ¥ã¾ãããï¼MemoryError ä¾å¤ãçºçãã¾ãï¼ããããã¨ã㯠xrangeé¢æ°ã使ãã¾ãã xrangeã¯ã¸ã§ãã¬ã¼ã¿ã§ãï¼ãã¶ãï¼
*2:å¤åãééã£ã¦ããã