JavaScriptã§ã½ããã¦ã§ã¢ã®æ£ãããæ°å¦çå³å¯ã«è¨¼æãã¦ã¿ã
ç¾å¨ãShibuya.js ãéå¬ä¸ã§ãï¼Ustream 㧠http://www.ustream.tv/channel/shibuyajs ã«ã¦æ¾éããã¦ãã¾ããããããããã®ããã°ã®å 容ãããã¹ãã¾ãï¼
ä»åããã¹ããããã¼ããªãããShibuya.js ã¯ãå½¹ã«ç«ã¤è©±æ å½ãããã¿æ å½ãã«åããã¦ãã¦ãåã¯ããã¿æ å½ããªãã§ããï¼ç¬ï¼ããã¤ãéããã¿ã§ããã§ããé ãæªæ¥ã®å½¹ç«ã¤ãã¿ã§ãï¼
ãã£ããã¯ãid:t-wada ããã«ãGUIã®èªåãã¹ãé¢ä¿ã®è³ªåãããããåããããã¨ãæãã¦ãããã¾ããã
å ¨ã¦ã®ãã¹ãã¯ãµã³ããªã³ã°ãã¹ãã§ãã
ï¼å°ã表ç¾éã£ããããããªããï¼è©±ã®æµãã§ãé¨å±ã移åããªãã¦ãããªãã¦ããããããããã話ãã§ããªãã£ãã®ã§ãããè¦ããã«ããµã³ããªã³ã°ãã¹ãã§ãã以ä¸ãå ¨ã¦ã®å ¥åãã¿ã¼ã³ããã¹ããããã¨ã¯ä¸å¯è½ã§ãããã§ãããã¨ã¯ãéãããã³ã¹ãã®ä¸ã§ãå¹çãããã¹ãããå¿ è¦ããããã¨ããæå³ã ã¨æãã¾ããã ãããæåãã¹ã vs èªåãã¹ããã©ã£ã¡ãè¯ãã®ãã¯ããã®æèã§ã®å¹çæ§ã§æ±ºã¾ãã¨ããã趣æ¨ã®çºè¨ã ã£ãã¨ãåã¯ç解ãã¾ãããï¼ã¡ãã£ã¨ããä¼è©±ã§ããªãã£ãã®ã§ãã»ã¨ãã©æ¨æ¸¬ã§ããt-wada ããã®æå³ã¯ãå ¨ç¶éã£ãããã¦â¦ï¼
ãã®åè¨ã¯ãæ¬å½ã«åãããåè¨ã ã¨æããã§ãããShibuya.js ã®ããã¿æ å½ãã¨ãã¦ã¯ãããï¼å ¨ã¦ï¼ã¨å¼ã£ãããã¾ãã (^^⪠ãä¾å¤ã®ãªãè¦åã¯ãªããã¨ãããã¨ãããããã¾ãããï¼ãã®ããã¨ããã¯èªå·±çç¾ãã¦ãããã§ããâ¦ï¼ç¬ï¼
èªå証æ
ã¨ããããã§ãããã°ã©ã ãä¸ãããã¦ããã®ããã°ã©ã ã®ãã¹ãããã°ã©ã ãä¸ãããã¦ãæ°å¦çã«ãã®æ£ãããå¼å¤å½¢ãç¹°ãè¿ãã¦ãæ£ããã証æããããã°ã©ã ãä½ãã¾ããï¼
ã¿ã¼ã²ããã¯ãä¸çªãããããããªãæ¿å ¥ã½ã¼ãããé¸ã³ã¾ãããï¼ç³ã訳ãªãã§ãããæ¿å ¥ã½ã¼ãã¨ã¯ä½ãã¯ãæ¤ç´¢ãã¦ãã ãããææãããããªããï¼
ããã°ã©ã ã¯ä»¥ä¸ã®éãã§ãã
function sortInner(ary) { if (ary.length < 2) { return ary; } else { if (head(ary) < head(tail(ary))) { return ary; } else { return append(head(tail(ary)), sortInner(tail(swap(ary))); } } }
æ¿å ¥ã½ã¼ãã¯2éã«ã¼ããªã®ã§ãããã«ã¼ãã®å å´ã®é¨åã ãã§ããã¤ã¾ããary[1] 以éã¯ã½ã¼ãæ¸ã¿ã§ãary[0] ãé©åãªä½ç½®ã¾ã§æ¿å ¥ããé¨åãã¿ã¼ã²ããã«ãã¾ããã
head(ary) = ary[0]ãtail(ary) = ary.slice(1)ãswap(ary) 㯠ary[0] 㨠ary[1] ãå ¥ãæ¿ããéç ´å£ã§ï¼æ°ããé åãçæãã¦ï¼è¡ãã¾ãã
ã½ã¼ãã®æ£ããã®å¤å®
æ£ããã½ã¼ãããã¦ãããã¨ã証æããã«ã¯ã以ä¸ã®2ã¤ã証æããå¿ è¦ãããã¾ãã
- ary[i] < ary[i + 1]
- ãã½ã¼ãåããã½ã¼ãå¾ãã§è¦ç´ ã1対1対å¿ãããã¨
æ±ãåé¡ãå°ããããããã«ãä»åã¯ãï¼ã®æ¹ã ããæ±ãã¾ãã
ã¨ããããã§ãå¤å®ããã°ã©ã ã¯ä»¥ä¸ã®éãã§ããï¼isOrdered ã®æ¹ãæ£ç¢ºãªååããããã¾ããï¼
function isSorted(ary) { if (ary.length < 2) { return true; } else { return head(ary) < head(tail(ary)) && isSorted(tail(ary)); } }
証æãããã¨
証æãããã¨ã¯ãisSorted(sortInner(ARY)) = true ã§ããåææ¡ä»¶ã¨ãã¦ãisSorted(tail(ARY)) = true ãæç«ãã¾ããå ãã¦ãæ°å¦ç帰ç´æ³ã使ã証æããã®ã§ï¼ã¾ã ããã®é¨åã¯èªååã§ãã¾ããã§ããï¼isSorted(sortInner(tail(ARY))) = true ãæ¡ä»¶ã«å ãã¾ãã
証ææã«ãå¤æ°ã¨å®æ°ãåºå¥ãã¦ããã®ã§ãããæ°å¦ç帰ç´æ³ã«ããã¦ãé åã®é·ãã¯å¶éãå ãããã¦ãã¦ãä»»æã®é åã¨ããããã§ã¯ãªãã®ã§ã対象ã®é åãå®æ°åããARY ã§è¡¨ç¾ãã¾ããã
ããã°ã©ã ã®è¡¨ç¾æ¹æ³
Haskell ã®ãããªç´ç²é¢æ°åè¨èªã¯é£ãããã¦ã人éã«ã¯æ¸ãã¥ããã¨æãã®ã§ãããèªå証æã«ããã¦ã¯ãåãæ±ãããããæ¡ç¨ãã¾ãããé¢æ°ã¯å ¨ã¦å¯ä½ç¨ãããã¾ãããã¤ã¾ããé åæä½ã¯ã絶対ã«é åãç ´å£ãã¾ããããªã®ã§ãsortInner ã¯ä»¥ä¸ã®å½¢ã«ãªãã¾ãã
sortInner(ary) = if(lt(len(ary), 2), ary, if (lt(head(ary), head(tail(ary)), ary, append(head(tail(ary)), sortInner(tail(swap(ary))))))
DOM & XPath
ãããã²ãããå¼å¤å½¢ãã¾ããä¸è¨ã®éããé¢æ°ã¯æ¨æ§é ã«ãªã£ã¦ããã®ã§ããã¿ã¼ã³ãããã³ã°ã§ã²ãããæ¨æ§é ã®å¤å½¢ã«ãªãã¾ãã
æ¨æ§é ãã©ãæ±ããæ©ãã ãã§ãããæåãæ£è¦è¡¨ç¾ã使ãããã¨æã£ããã§ãããPerl ã®æ£è¦è¡¨ç¾ã¨ã¯éã£ã¦ JavaScript ã®æ£è¦è¡¨ç¾ã¯å帰ãæ±ããªãã®ã§ãæ¨æ§é ãæ±ãã¥ãããJavaScript (HTML) 㯠DOM ã使ãã¦ãæ¨æ§é ã®ããã® API ãããã®ã§ãé¢æ°ã DOM ã«æ¼ãè¾¼ã¿ã¾ããï¼ãã®ä¸ã§ãå¼å¤å½¢ã®ãã¿ã¼ã³ãããã³ã°ã XPath ã使ã£ã¦æ¤ç´¢ããããã«ãã¾ãããFirebug ãªã©ã使ãã¨ãæ¨æ§é ã®å¤å½¢ã追ããããã便å©ã§ããï¼
å®ç¾©ï¼å ¬ç
ãã®ã»ãã使ãå®ç¾©ãå ¬çã¯ä»¥ä¸ã®éãã
head(tail(swap(ary))) = head(ary) tail(tail(swap(ary))) = tail(tail(ary)) tail(append(ary, v)) = ary head(append(ary, v)) = v if(true, a, b) = a if(false, a, b) = b if(a, b, b) = b a && true = a true && a = a
ã¡ãã£ã¨ãºã«ãªãã§ãããå®ç¾©ã®ãããªå®çã
len(append(ary, v)) < 2 = false
é£çµããã¨ãé·ãã2以ä¸ã«ãªããã¨è¨ã件ã§ããlen 㨠append ã®é¢ä¿æ§ãææããã¾ããã
ä¸æãæ±ããªãã£ãæ°å¦ç帰ç´æ³
æ¨æ¥ã»ä»æ¥ã§ãã®æºåããã¦ãã¦ãçºè¡¨ã¾ã§ã«éã«åããªãã£ããæ°å¦ç帰ç´æ³ã®åãæ±ãã§ãã以ä¸ã®2ã¤ã¯åç¬ã§è¨¼æã§ããã¨æãã®ã§ããããã¾ããèªå証æã®æµãã®ä¸ã§è¨¼æã§ããªãã£ãã®ã§ãå®çã«å ãã¦ãã¾ãã¾ãããããããªããã次ã®èª²é¡ã¨ãã¦ããããã¡ããã¨èªå証æã§ããããã«ãããã§ãï¼
- head(tail(ARY)) < head(tail(tail(ARY)))
- isSorted(sortInner(head(swap(ARY)))) = true
æåã®æ¹ã¯ãtail(ARY) ã¯ã½ã¼ãæ¸ã¿ã ãã tail(ARY)[0] < tail(ARY)[1] ã¨ããäºã§ãã
次ã®æ¹ã¯ãhead(swap(ARY)) ã¯é·ã n – 1 ã ãããæ°å¦ç帰ç´æ³ã«ãã証ææ¸ã¿ ã¨ããäºã§ãã
if æ¡ä»¶ã®ä¼æ¬
ããã«ãç¹æ®ãªå¼å¤å½¢ã2ã¤å¿ è¦ã§ããä¸ã¤ç®ã¯ãif (A) { B } else { C } ã«ããã¦ãB ã®ä¸ã§ A = true ã§ãããã¨ã使ããã¨ãå¿ é ã§ãããããããif æ¡ä»¶ã®ä¼æ¬ãã
if (A, B, C) ã§
- Bã®ä¸ã§Aã使ã£ã¦ããããA = true
- Cã®ä¸ã§Aã使ã£ã¦ããããA = false
å¦å®å½¢ãå¿ è¦ã§ãã
- Bã®ä¸ã§not Aã使ã£ã¦ããããnot A = false
- Cã®ä¸ã§not Aã使ã£ã¦ããããnot A = true
if ã®è¦ªé¢æ°ã®ç¹°ãè¾¼ã¿
2ã¤ç®ã¯ãif ã®è¦ªé¢æ°ã®ç¹°ãè¾¼ã¿ã§ãã f(A ? B : C) = A ? f(B) : f(C) ã§ããfã®å¼æ°ã®æ°ãä»»æã§ãæ±ããã¨è¨ããã¨ãä¸æã表ç¾ããæ¹æ³ãæãã¤ããªãã£ãã®ã§ãå¥æ ã§ã³ã¼ããæ¸ãã¾ããã
証ææ¦ç¥
å¼å¤å½¢ã«ããã¦ã次ã®ä¸æã¯ããããããããææ°é¢æ°çã«å¢ããã¨æã£ãã®ã§ããããªãã¨ãå¼å¤å½¢ã«åªå é ä½ãã¤ããããä¸ç´ç·ã§è¨¼æã§ãã¾ããã
- ãã¼ãæ°ãæ¸ãå¤å½¢ ã if æ¡ä»¶ã®ä¼æ¬ã
- if ã®è¦ªé¢æ°ã®ç¹°ãè¾¼ã¿
- ãã¼ãæ°ãå¢ããå¤å½¢ï¼å®ç¾©ã®ä»£å ¥ï¼
ãã¼ãæ°ãæ¸ãæ¹ãåªå ãã¦ãããããããã¨ããªããªã£ããããã¼ããæ°ãå¢ããå¤å½¢ããã£ã¦ã¿ã¦ãããã«ãã¾ãããã¼ãæ°ãæ¸ããå¤å½¢ãç¹°ãè¿ãã¦ãã¨ããã®ã§ãã§ãã¾ãããã§ããããã¯ãä»åã®ç¹ä¾ã§ãããããã次ã«ã©ãããæãç¹°ãåºããã¯ä¸æãè©ä¾¡é¢æ°ãä½ã£ã¦ã証æãåå²ãã¦ãããã¿ã¼ã³ã¯çµ¶å¯¾ã«å¿ è¦ã ã¨æãã¾ãã
if ã®è¦ªé¢æ°ã®ç¹°ã込㿠ã¯å°ãã ããæ¨æ§é ãè¤éã«ãªãæ¹ã®å¤å½¢ã§ãã
ãã¢
ã¨ããããã§ããã¢ã§ããhttp://yukoba.jp/autotest/sort.html ãããããã ãããåºåã¯ãconsole.log() ã«åºã¾ããInternet Explorer 8ã¯æªå¯¾å¿ã§ãï¼XPath ã®æ±ããéãã¾ãï¼ãç´°ããã¢ã«ã´ãªãºã 㯠http://yukoba.jp/autotest/js/sort.js ãã覧ãã ããã
ã¨ããããã§ãconsole.log() ãã¿ãã¨ãããã®ã§ããããªãã¨38åã®å¼å¤å½¢ã§è¨¼æã§ãã¡ãããã§ãï¼
ãã°ã®åºåã¯ãã証æãã¹ã対象ã®å¼ãã使ããå¤å½¢å¼ããåºåããå¾ãããããå ã¯ãå¼å¤å½¢ãã¨ã«ããã©ã®å¼ã使ã£ã¦å¤å½¢ããããã証æãã¹ã対象ã®å¼ã®å¤åããçµæããç¹°ãè¿ã表示ãã¦ãã¾ãã