ã¯ãã¼ã¸ã£ã¨ã³ã«ã¼ãã³ï¼ï¼ï¼ï¼ç¶æ é·ç§»é¢æ°ãã¤ãã£ã¦ã¿ã
ã¯ããã«
æè¿ã
ã°ãã¼ãã«å¤æ°ã«é ¼ããã¨ãªããé¢æ°åç¬ã§ç¶æ
é·ç§»ãå®ç¾ã§ããé¢æ°ããä½ã£ã¦éãã§ãã¾ãã
å ·ä½çã«ã¯æ¬¡ã®ãããªã®ã
- é åãä¸ããã¨ããããç°ç¶é åï¼å·¦ç«¯ã¨å³ç«¯ãã¤ãªãã£ã¦ãããªã³ã°ç¶é åããã©ã¯ã¨ã®ã¯ã¼ã«ãããããã¤ã¡ã¼ã¸ããã¨ããããï¼ã¨è§£éããå®è¡ãããã³ã«ãã®ç°ç¶é åãå³ãã¾ãã¯å·¦ã«é ã«è¾¿ã£ã¦åç §ã§ããé¢æ°ãè¿ãé¢æ°ã
--initRingã¯ãç¶æ é·ç§»é¢æ°ãä½æããé¢æ° --å ã¨ãªãé åã¨åæåç §ä½ç½®ãä¸ãã ring = initRing({'a', 'b', 'c'}, 1) -- ç¶æ é·ç§»é¢æ°ã«ä¸ããå¼æ°ã§åç §ä½ç½®ãåãã -- æ£ã®æ°ãªãã°åç §ä½ç½®ããã®å¤ã ãå³ã«ãããããã¤ãã¹ã¯å·¦ã«ããã -- ç°ç¶é åãªã®ã§'c'ã®æ¬¡ã¯'a'ã'a'ã®åã¯'c'ã¨ãªã ring(0) =>a ring(1) =>b ring(1) =>c ring(1) =>a ring(-1) =>c
é¢æ°èªèº«ãç¾å¨ã®é·ç§»ç¶æ ãä¿æãã¦ããã®ã§ãã·ã³ãã«ã«ä½¿ããã¨ãããç¹å¾´ã§ããã
ãã¨ãã°é¢æ°ã®å¤å´ã«ç¶æ
ä¿æç¨ã®å¤æ°ï¼ãã¨ãã°ãpos = 1ãªã©ï¼ãä½ã£ã¦é¢æ°å
ã§ãããæä½ããã°åããã¨ãå®ç¾ã§ãã¾ãããä»ã®å ´æã§å¤æ°posã«ç´æ¥ã¢ã¯ã»ã¹ããã¦æ¸ãæããããã¨å°ããã§ãããã
ãã¼ã¿ã®é è½ã¯è¿ä»£ããã°ã©ãã³ã°ä½æ³ã®åºæ¬ã§ããï¼å¤§ããï¼ã
ããã«ããã¨ãã°ç¶æ
é·ç§»é¢æ°ã10åä½æï¼ring1ï½ring10ï¼ããããç¶æ
ä¿æç¨ã®å¤æ°ã10å管çï¼pos1ï½10ï¼ããªãã¦ã¯ãªããã管çãé¢åã§ããã
ç¶æ é·ç§»é¢æ°ã®å®è£ ï¼ã¯ãã¼ã¸ã£ç
å®è£
æ¹æ³ã¨ãã¦ã¯ãã¯ãã¼ã¸ã£ã¼ã使ãæ¹æ³ã¨ã³ã«ã¼ãã³ã使ãæ¹æ³ã®2éããããã®ã§ããã
ã¨ãããã両æ¹ä¸¦ã¹ã¦è¦ã¾ãã
ã½ã¼ã¹ã³ã¼ã
ã¯ãã¼ã¸ã£ã使ã£ã¦ä½ã£ãã®ããã¡ãã
function initRing1(array, i) -- iã¯åç §ããåæä½ç½® return function (x) i= i+x return array[((i-1)%#array)+1] end end cl = initRing1({'a','b','c'}, 1) print(cl(0)) print(cl(1)) print(cl(1)) print(cl(1)) print(cl(-1))
é¢æ°initRing1()ã¯ãç¡åé¢æ°ãä½æããé¢æ°ã§ãã
ãããä½æãããç¡åé¢æ°ã¯ãarrayã¨iã¨ãã2ã¤ã®ç¹æ®ãªå¤æ°ãæã£ã¦ãã¾ãã
ã¯ãã¼ã¸ã£ã«ã¤ãã¦ãã£ã¨è©³ããï¼
ããã ãããä½è¨ã£ã¦ãããããããªãã§ããããã¤ã¾ãã¶ã£ã¡ããã¦è¨ãã¾ãã¨ã
ä¸è¨ã§initRing1ã«ãã£ã¦ä½æãããé¢æ°ãclãã¯ãå®è³ªçã«ã以ä¸ã®doï½endã®ã¹ã³ã¼ãå
ã®è¨è¿°ã¨åããããªåããããé¢æ°ã§ãã
do local array = {'a','b','c'} local i = 1 return function(x) i= i+x return array[((i-1)%#array)+1] end end
initRing1()ãå®è¡ãããã³ã«ãä¸è¨doï½endã¹ã³ã¼ãã®éã«ãããããªããç¹æ®ãªãã¼ã«ã«å¤æ°2ã¤ãã¨ãç¡åé¢æ°ããã»ããã§ã¾ã¨ããé¢æ°ãä½æãã¾ãã
ã¡ãªã¿ã«ãã®ãã¼ã«ã«å¤æ°ã®ä½ãç¹æ®ãã¨è¨ãã¨ãinitRing1()ã§æ°ããé¢æ°ãä½æãããã³ã«ããã®é¢æ°ã«ä»å±ããå½¢ã§ãã¼ã«ã«å¤æ°ãä½æããããã¨ã§ãã
- a = initRing1({'a', 'b', 'c'}, 1)
- é¢æ°aã¯ããã¼ã«ã«å¤æ°Aã¨ç¡åé¢æ°Aãå«ã
- b = initRing1({'a', 'b', 'c'}, 1)
- é¢æ°bã¯ããã¼ã«ã«å¤æ°Bã¨ç¡åé¢æ°Bãå«ã
- c = initRing1({'a', 'b', 'c'}, 1)
- é¢æ°cã¯ããã¼ã«ã«å¤æ°Cã¨ç¡åé¢æ°Cãå«ã
ãã¨ãã°ãä¸è¨ã®ããã«3åã®é¢æ°ãaããbããcããä½ã£ãã¨ãã«ãããããã«å«ã¾ãããã¼ã«ã«å¤æ°A,B,Cã¯å ¨ã¦åå¥ã«ä½ãããç¬ç«ãã¦ãã¾ã*1ããã®ãããé¢æ°a,b,cã¯ãã¹ã¦ç¬ç«ãã¦åãããã¨ãã§ããã®ã§ãã
Luaã§ã¯ããã®ãããªç¹æ®ãªãã¼ã«ã«å¤æ°ã®ãã¨ããä¸ä½å¤ãã¨å¼ãã§ãã¾ãã
ããã¦ãã®ãããªãä¸ä½å¤ãã¨ãç¡åé¢æ°ããã»ããã«ããé¢æ°ãä½æãããæ©è½ãã®ãã¨ãã¯ãã¼ã¸ã£ã¨å¼ãã§ãã¾ã*2ã
ãã®ã¯ãã¼ã¸ã£ã¯ã¨ã¦ãå¼·åãªæ©è½ã§ãã¦ãé¢æ°åè¨èªã§ã¯ããã¼ã¿ã®é è½ããå帰é¢æ°ã«ãããç¶æ
ä¿æããªã©ã®ç¨éã§ä½¿ããã¾ãã
ä»åã¯ãããã¼ã¿é è½ãã®ããã«ã¯ãã¼ã¸ã£ã使ããç¶æ
é·ç§»é¢æ°ãä½æããããã§ãã
è£è¶³
ä¸è¨ã³ã¼ãå ã§æå·ã®ããã«è¦ãã以ä¸ã®è¨ç®å¼ã¯ãå¤æ°iã®å¤ã«ãã£ã¦æå°å¤1ããæ大å¤ãé åã®è¦ç´ æ°ï¼#arrayï¼ã¨ããç¯å²ã®æ´æ°ãè¿ãã¾ãã
((i-1) % #array)+1
ãµã³ãã«ã³ã¼ãã®ä¾ã§ã¯ãä¸ããé
å{'a','b','c'}ã®è¦ç´ æ°ã3ãªã®ã§ãiã®å¤ãä½ã§ãã£ã¦ãã1ï½3ã¾ã§ã®å¤ãè¿ãã¾ã*3ã
array[4]ã§ã¯å¤ãããã¾ããããããarray[4]ãarray[1]ã«å¤æãããã¨ã§ã循ç°ãã¦ããããã«è¦ãããã¦ããããã§ãã
ç¶æ é·ç§»é¢æ°ã®å®è£ ï¼ã³ã«ã¼ãã³ç
ã³ã«ã¼ãã³ã使ã£ã¦ä½ã£ãã®ããã¡ãã
ãããã«èª¬æããã¹ãã¼ã¹ããªãã®ã§ãä»åã¯è§£èª¬ãçç¥ãã¾ãã
ã³ã«ã¼ãã³ã«ã¤ãã¦ã¯ããã®ãã¡ãã£ããã¨è§£èª¬ãã¾ãããã
function initRing2(array,i) -- iã¯åç §ããåæä½ç½® return coroutine.wrap(function() while true do i= i + coroutine.yield(array[((i-1)%#array)+1]) end end) end -- funcã¯ãä¸æããã¹ã¬ãããåéãããé¢æ° func = initRing2({'a','b','c'}, 1) func() --é¢æ°å®è¡ã¹ã¿ã¼ã --以ä¸ãå®è¡ã®ãã³ã«ä¸æããã¹ã¬ãããåé --å¼æ°ã¯åç §ä½ç½®ããããããã®å¤ print(func(0)) print(func(1)) print(func(1)) print(func(1)) print(func(-1))
coroutine.wrap()ã¯ä¾¿å©ã ãã©ãä½ãããé¢æ°funcã®æåã・・・・・・ã¡ãã£ã¨ã ããããï½
é¢æ°å®è¡ã¹ã¿ã¼ãããã¨ãï¼ååï¼ã¨ãä¸æããã¹ã¬ãããåéããã¨ãï¼äºåç®ä»¥éï¼ã§ãå¼æ°ã®æå³ãéã£ã¦ããã¨ãã・・・・・・ã
coroutine.create()ãcoroutine.resume()ã使ã£ãæ¹ãç解ããããã³ã¼ãã«ãªãã¨ã¯æãããã©ãä»åã¯ãããæ¬é¡ã§ã¯ãªãã®ã§çç¥ã
ã¾ã¨ã
Luaå ¬å¼æ¬ã«ããã¨ãããã©ã¼ãã³ã¹ï¼å®è¡ã¹ãã¼ãï¼ã¯coroutineåéã¨é¢æ°å¼ã³åºãã§å·®ã¯ã»ã¨ãã©ãªãã¨ã®ãã¨ãªã®ã§ãã©ã¡ããæ¡ç¨ãããã¯å¥½ã¿æ¬¡ç¬¬ã§ããããã
ã·ã³ãã«ãã§ã¯ãã¯ãã¯ãã¼ã¸ã£ã¼ããªã
coroutineãå«ãã§ã¯ãªããã©ããããã°ãé¢åãããã®ã§ï½
ãï¼ãã®ãããªç¶æ é·ç§»é¢æ°ãä½ã«ä½¿ããã§ãã£ã¦ï¼
以åä½ã£ããturtle.selectNext()é¢æ°ãæ¸ãæããããããªã¨æãã¾ãã¦ã
【補足2】既存の関数を上書きして拡張しよう - Minecraftとタートルと僕
以åã®å®è£
ã ã¨ãç¾å¨ã®é¸æã¹ãããçªå·ãä¿æããé ããã©ã¡ã¼ã¿ï¼ã°ãã¼ãã«å¤æ°ï¼ãå¿
è¦ã¨ãã¦ããããã©ã
ãã®ãããªç¶æ
é·ç§»é¢æ°ãããã°é ããã©ã¡ã¼ã¿ãå¿
è¦ãªããªãã¾ãããããã¼ã¿é è½ããã¼ã¿é è½ã
å ·ä½çãªã³ã¼ãã«ã¤ãã¦ã¯ã次åç´¹ä»ãã¾ãããã
- ï¼2014.01.31追è¨ï¼
*1:ãªãã¸ã§ã¯ãæåè¨èªããããªãã§ãã人ã«ã¯ãé¢æ°ã®ã¤ã³ã¹ã¿ã³ã¹ãã¨ã«ãã¼ã«ã«å¤æ°ãå²ãå½ã¦ããããã¨è¨ã£ãæ¹ãéããããããããã¾ãã
*2:æ £ç¿çã«ããã®æ©è½ã使ã£ã¦ä½ã£ãã»ããé¢æ°èªä½ãã¯ãã¼ã¸ã£ã¨å¼ã¶ãã¨ãããã¾ã
*3:Luaã®é åæ·»ãåæå°ã1ã¨ããç¸ããã¡ãã£ã¨æ°æã¡æªããCãJavaãªã0ãªã®ã«ãã