ãæ¯æ¥æ¸ãæ®éã®ã³ã¼ãã§ãªãã¹ããã¹ãæ¸ããããããã°ãç°¡åã«ãã¦ãããå¤ãã®æéã楽ããäº(ãã¼ãã¼ãå©ãããæ±ç¨é¢æ°ä½ã£ãããªãã¡ã¯ã¿ããã)ã«è²»ããããã¦æè¿å®è·µãã¦ãããã¨ãã¡ã¢ãã¾ããä¸ã¤åã®çãã¦ãã¨ã³ããªã®è£è¶³(ä¸å¤æåã®å©ç¹ã®èª¬æ)ãå ¼ãã¦ããã®ã§è©±ããã£ã¡ãã£ã¡ã¶ãã¦ãã¾ãã¾ããã
é çªã¸ã®ä¾åãæ¸ãã
ãXãå®è¡ãã¦ããYãå®è¡ããã¨ãããããªãã*1ã¨ããYãå®å ¨ã«å®è¡ããåã«ã¯å¿ ãZãå®è¡ããªããã°ãããªãã*2ã¨ãã¯çµ¶å¯¾ã«ãã¹ãã®ã§ãªãã¹ããªãããå¾è ã«ã¤ãã¦ã¯ã©ãããä½ã£ã¦ãã®ä¸ã§ã¢ãããã¯ã«å®è¡ããã¨ãã§è§£æ±ºã§ãããã©ãåè ã¯ãã¥ã¼ã¿ãã«ãªãã®(ãªãã¸ã§ã¯ã)ã使ã£ã¦ããéãè³ãæã§çºç¾ããæ ¹æ¬çãªè§£æ±ºã¯å°é£ã以éã®ã»ã¯ã·ã§ã³ã§é¢æ°åã®èãæ¹ãåãå ¥ãã¦ããã解決ãã¦ããã¾ãã
ç¶æ ãæ¸ãã
ãå¤ãã®ããã°ã©ã ã¯ç¶æ ãæã¡ã¾ããããé¢æ°ã®ãããªãã®ã«ããã°ã©ã ä¸ãã¾ãã¾ãªå ´æã§åãå¼æ°ãé©ç¨ããã¨ãã«éãçµæã帰ã£ã¦ããå¯è½æ§ããããªãããã®é¢æ°ã®ãããªãã®ã¯ç¶æ ãå å ãã¦ãã¾ãããªãã¸ã§ã¯ãã¯ããããã"ç¶æ ã®ã«ãã»ã«å"ã§ããã¹ãããã®æ¸ãæããè¡ãã¨ããäºã¯ã¡ã½ãããå¼ãã æã®çµæãå¤ãããã¨ã«ç´çµãã¾ããã°ãã¼ãã«å¤æ°ãç¶æ ã§ãã
ãé¢æ°åã®èãæ¹(ä¸å¤æå)ã§ããã°ã©ãã³ã°ãè¡ããªãã¨ãããããç¶æ ãå å ããä½ããããã°ã©ã ä¸ã«æ£ä¹±ãã¾ããä¾ãã°MVCã§ã¯ã¢ãã«ããã¥ã¼ãç¶æ ãæã£ããªãã¸ã§ã¯ãã§ãã¢ãã«ããã¥ã¼ãããããè¤æ°ããã®ã§ãç¶æ å°çã§ãã
ç¶æ ãå ¥ãè¾¼ãã¨ãé çªã¸ã®ä¾åãæ¿ãããªããã¹ãå¢ãã¾ãã
var xs = [1,2,3,4,5,6,7,8,9]; some_function1(xs); xs.some_method(); some_function3(xs); test("sum up xs", function () { strictEqual(_.reduce(xs, _['+']), 45, "1+2+3+4+5+6+7+8+9 = 45"); })
ããã®ã³ã¼ãã§ãxsãå¤ã§ãsome_functionãsome_methodãé¢æ°ãªããã¹ãã¯éãã¯ãã§ãããããxsã¯ãªãã¸ã§ã¯ããªã®ã§ãsome_functionãsome_methodã®ä¸ã§ä½ãããããããããããã¹ããéããã¨ãä¿è¨¼ã§ãã¾ãããsome_functionãsome_methodãç¶æ ãæã£ã¦ããããªããããªã«ãèµ·ããããããã¾ãããç¶æ ãè¤æ°æã¤ããã°ã©ã ã§é¼»æã¡ãªããªããã¨ãèµ·ãã£ãæã¯ãã³ã¼ãããä½ãèµ·ããããäºæ¸¬ã§ããªãã®ã§ãããã¬ãéãã¦ãã¬ã¼ã¯ãã¤ã³ããããããè²¼ã£ã¦å®éã«å®è¡ãã¦å¤æ°ã®å¤ã確èªããå¿ è¦ãããã¾ãã
ãç¶æ ã¯1ã¤ä»¥ä¸ã«æããã¨ã³ã¼ãã®åé¢ããèµ·ããããç¶æ³ã®äºæ¸¬ããããããªããèããäºãæ¸ã£ã¦ãã¹ãæ¸ãã¾ããç¶æ ã1ã¤ä»¥ä¸ã«ããã«ã¯ãä¸ã¤é ãåé ç·ããªããã°ãããªããã¨ãããã¾ãã次ã®ã»ã¯ã·ã§ã³ã§ãããè¦ã¦ã¿ã¾ãããã
ã¢ã¤ãã³ãã£ãã£ã¨ç¶æ ã®åé¢
ããªãã¸ã§ã¯ãæåãæ§é åããã°ã©ãã³ã°ã§ãå¤æ°ããªãã¸ã§ã¯ãã¨ããå½¢ã§ç¶æ å°çãåºæ¥ä¸ãã£ã¦ãã¾ãã®ã¯ããããã®ä»çµã¿ãã2ã¤ã®å ¨ãå¥ã®ã³ã³ã»ããããã£ã¤ãã¦ãã¾ã£ã¦ããããã§ããå¥ã«ãã£ã¤ãããã¨æã£ã¦ãã£ã¤ããããã§ã¯ãªãããã¤ã³ã¿ã§ã¡ã¢ãªãç´æ¥å¼ã£ã¦ããæ代ã®éºç£ã延ã ã¨å¼ãç¶ããã¦ããçµæã¨ãã¦å¤ãã®è¨èªã«ããã¦åéããå¼·å¶ãããç¶æ³ã«ãªã£ã¦ãã¾ãããã®2ã¤ã¨ã¯ãã¢ã¤ãã³ãã£ãã£ã¨ç¶æ ã§ãã
ãç¶æ ã«ã¤ãã¦ã¯æ¢ã«è§¦ãã¾ãããã¢ã¤ãã³ãã£ãã£ã¯æ¥æ¬èªã«ããã¨åä¸æ§ã§ããããã¼ã¿xã¨ãããã¼ã¿yãåãç©ã§ãããã®åºæºã«ãªãã¾ããidentityã¨ããåé¢ããæ¨å¯ãããéãããã¼ã¿ãã¼ã¹ã¨ãWeb APIã¨ãã使ãã¨ãã«å¿ ãç®ã«ããidã®ä»²éã§ãã
ããªãã¸ã§ã¯ãæåã§ã¯ã¢ã¤ãã³ãã£ãã£ã¨ãã¦ãã¤ã³ã¿ã使ããã¦ãã¾ããã¤ã¾ããæ å ±ã¨ãã¦åãç©ã§ããããã¡ã¢ãªä¸ã§åãã¢ãã¬ã¹ã«ãããå¦ãã§ããèå¥ã§ããªããã§ãããããããã§ãããããªãã¸ã§ã¯ãã®ä¸èº«ã¯ã©ãã©ãå¤ãã£ã¦ããã¾ãããä»ã«åä¸ããå¤æããæ¹æ³ãªãã¦ãªãã§ããããäºå®(æ å ±)ã¨ããã®ã¯ãã¡ã¢ãªã¨ããå ´æã«çµã³ã¤ãã¦ãã¦ãããã®å 容ãå¤ããã°äºå®ãå¤ããã¾ãã
ãThat's just bullshit!!!
//å±±ç°ããããã¾ããå±±ç°ããã®ã¡ã¼ã«ã¢ãã¬ã¹ã¯2013å¹´6æ13æ¥ã®ä»æ¥ã[email protected]ã§ãã yamada = new Human({name: "Yamada", email: "[email protected]"}); util.inspect(yamada); //=> {name: "Yamada", email: "[email protected]"} //2013å¹´6æ20æ¥ãå±±ç°ãããæ°ããã±ã¼ã¿ã¤ãè²·ãã¾ãããã¤ãã§ã«ã¡ã¢ããå¤æ´ããããã§ããæ°ããã¡ã¢ãã¯[email protected]ã ããã§ãã yamada.setEmail("[email protected]"); util.inspect(yamada); //=> {name: "Yamada", email: "[email protected]"} /* WHAAAAT THE HELL HAD JUST HAPPEND??????????????? */
ã6æ13æ¥ãã6æ20æ¥ã¾ã§ã®ã·ã³ãã«ãªã¡ã¼ã«ã¢ãã¬ã¹ã使ã£ã¦ããå±±ç°ããã¯ã©ãã«ãã£ããã§ããã???? ãããå®ä¸ç*3ã«å½ã¦ã¯ãã¦ã¿ãã¨ä¸èªç¶ãã«æ°ä»ãã¾ããå®ä¸çã§ãåãã¯ãããªãå¤ãªã¡ã¼ã«ã¢ãã¬ã¹ã使ãå§ããå±±ç°ãããè¦ã¦ç¬ãã¯ãã§ããåãããããç¬ããã®ã¯åã®ã¡ã¼ã«ã¢ãã¬ã¹ãç¥ã£ã¦ããããã§ããå±±ç°ãããçé¢ç®ãªã¡ã¼ã«ã¢ãã¬ã¹ã使ã£ã¦ããã¨ããäºå®ãå¤ãããªãããã§ãããªãã¸ã§ã¯ãã®ä¸çã§ã¯é·æè¨æ¶ãç´æ¥æ¸ãå¤ãã£ã¦ãã¾ãã®ã§åãã¯å±±ç°ããã®æ°ããã¡ã¼ã«ã¢ãã¬ã¹ãè¦ã¦ããã®ã®ã£ãããç¬ããªãã®ã§ãããã®äºå®ã®æ¸ãå¤ãããé çªã¸ã®ä¾åãæ·±å»åããããã¹ãçã¿åºãåå ã§ãã
ãå°ãèéã«ããã¾ããããå®ä¸çã®è©±ãæãã ãã¨ã§ã¯ã£ãããã¾ãããåãã¯ã¢ã¤ãã³ãã£ãã£ã¨ç¶æ ã¯ãããããå¥ã®ç©ã ã¨ç¥ã£ã¦ãããã§ããäºå®ã¯å¤ããã¾ãããäºå®ã¯æéã«çµã³ã¤ãã¦ãã¾ããç¶æ ã¯ãæéä¸ã®ä¸ç¹ã§ã®ææ°ã®äºå®ã§ããã¢ã¤ãã³ãã£ãã£ã¯äºå®ãé£ãªã£ãã¿ã¤ã ã©ã¤ã³ã§ãã
ããã®æ£ããã¢ãã«ãå®éã«ããã°ã©ã ã§ä½¿ãã«ã¯ã©ãããã°ããã®ã§ãããããããã¨æãã¤ãã®ã¯å帰ã使ãæ¹æ³ã§ããå®éãå¤ãã®é¢æ°åè¨èªã§ä½ãããã²ã¼ã ã¯ãã²ã¼ã ã«ã¼ããç¶æ ã®å¤ãåãåã£ã¦ãããã¨ã¦ã¼ã¶ã®ã¤ã³ãããããã¨ã«æ°ããç¶æ ãä½ã£ã¦èªèº«ãå帰å¼ã³åºãããé¢æ°ã¨ãã¦å®è£ ãã¦ãã¾ããã²ã¼ã ã«ã¼ãã®ä¾ã®ãããªå帰ã§è¡¨ç¾ãè¾ãå ´é¢ã§ã¯ãã¢ã¤ãã³ãã£ãã£ã ãã管çããå¤æ°Stateãä¸ã¤ã ãç¨æãã¦ãããã«å®è¡ä¸ã«æ´æ°ããããå¤ãå ¨ã¦ä¿æããã¤ãã¥ã¼ã¿ãã«ãªè¾æ¸ãæããã¦ãç¶æ ãå¤ãã度ã«ãã®è¾æ¸ãçæãã¦Stateã®åç §ããã®æ°ããè¾æ¸ã«å¼µãæ¿ãããã¨ã§æ£ãã表ç¾ãããã¨ãã§ãã¾ããWebFUIãPastaã¯ãã®æ¹æ³ãçºå±ããã¦ä½¿ã£ã¦ãã¾ããClojureã®atomã¨ãrefã¨ãããããã使ãæ¹ã§ããã以éãã®ç¶æ ã®è¾æ¸ã®ãã¨ãStateã¨å¼ã³ã¾ãã
/* å¿ è¦æå°éã®ç¶æ 管çAPIãããå°ã使ããããç©ã¯Pastaãåç §ã®ãã¨ã */ function App (initial_state, watcher) { var state = initial_state; return { patch: function (patch) { var new_state = Object.freeze(_.conj(state, patch)); watcher(new_state, state); state = new_state;//Stateã®åç §ãå¼µãæ¿ãããStateã®ä¸èº«ãç´æ¥ãããããã§ã¯ãªãï¼ }, deref: function () { return state; } }; }; var app = App({yamada: {name: "Yamada", email: "[email protected]"}}, function (newState, oldState) { //Stateãå¤ããã¨ãã«å¼ã°ãã //ãã®ä¸ã¯å帰ã®ã¨ãã¨ã»ã¨ãã©ããªã //Stateã¯å¤ãªã®ã§ã³ãã¼ã¨ãããã«ä¿åãã¦ãã¨ã§ä½¿ã£ããã§ãã //ããã§ã¯ã¢ããªã®è¦ãç®ãå¼ã£ãããã. Stateã®ã¿ã«ä¾åãããã¨! }); //å±±ç°ããããã¾ããå±±ç°ããã®ã¡ã¼ã«ã¢ãã¬ã¹ã¯2013å¹´6æ13æ¥ã®ä»æ¥ã[email protected]ã§ãã yamada20130613 = app.deref().yamada; //=> {name: "Yamada", email: "[email protected]"} //2013å¹´6æ20æ¥ãå±±ç°ãããæ°ããã±ã¼ã¿ã¤ãè²·ãã¾ãããã¤ãã§ã«ã¡ã¢ããå¤æ´ããããã§ããæ°ããã¡ã¢ãã¯[email protected]ã ããã§ãã app.patch({ yamada: _.conj(yamada20130613, {email: "[email protected]"}) }); yamada20130620 = app.deref().yamada; console.log("å±±ç°ããã¯6æ13æ¥ã®ã¡ã¢ãã¯" + yamada20130613.email + "ã§ã"); //=> å±±ç°ããã®6æ13æ¥ã®ã¡ã¢ãã¯[email protected]ã§ã console.log("å±±ç°ããã¯6æ20æ¥ã®ã¡ã¢ãã¯" + yamada20130620.email + "ã§ã"); //=> å±±ç°ããã®6æ20æ¥ã®ã¡ã¢ãã¯[email protected]ã§ã
ãã¢ã¤ãã³ãã£ãã£ã¨ç¶æ ãåãã¦èããäºã®å©ç¹ã¯ãããã°ã©ã ä¸èããªããã°ãªããªãç¶æ ãä¸ã¤ã ãã«ã§ããã¨ãããã¨ã§ãããã®å©ç¹ã¯ä»ã®è¨äºãPastaã®READMEã§ãæ£ã 強調ãã¦ãããã¨ãªã®ã§ãããã¢ããªã±ã¼ã·ã§ã³èªä½ãå¤ã¨ãã¦æ±ãããã¨ã§ããã¢ããªã±ã¼ã·ã§ã³ã®Stateããã¢ããªã±ã¼ã·ã§ã³ã®å é¨ç¶æ ã¨è¦ãç®ãå®å ¨ã«åç¾ã§ããããã»ããã¢ãããã¦ããã°ãStateã¯ãã ã®å¤ãªã®ã§jsonã«ã§ãã·ãªã¢ã©ã¤ãºããã°ã»ã¼ã©ãã«ã§ãä¿åã§ãã¦ãã¯ã¤ã¤ã«ä¹ãã¦äººã«éãã¤ããããªãã§ãã§ãã¾ãã
ãã¢ããªã±ã¼ã·ã§ã³ã®ç¶æ ãææ¡ããã«ã¯ãã®Stateãè¦ãã ãã«ãªãã®ã§ãã°ã®åå 究æãç°¡åã«ãªãã¾ããç¶æ ãæ£ãã°ã£ã¦ãããã©ã®ç¶æ ã«ä¾åãã¦ãã®ãã°ãåºãã®ããæ¢ãå¿ è¦ãããã¾ãããã¹ã¯å¿ ãèµ·ããã®ã§ãä¿®æ£ã¯ç°¡åãªæ¹ãè¯ãã§ãã
ãã¥ã¼ã¿ãã«ãªãã¼ã¿æ§é (ãªãã¸ã§ã¯ã)ã使ããªã
ãããã¾ã§è¦ã¦ããäºãç·åããã¨ããã¹ã®å¤ãããã¥ã¼ã¿ãã«ãªãã¼ã¿æ§é (Cã®æ§é ä½ã¨ããªãã¸ã§ã¯ã)ã使ã£ã¦ãããã¨ããçã¾ããã®ã§ãããã使ããªãããã«ãããã¨ãã主張ã«ãªã£ã¦ãããã¨æãã¾ãããªãã¸ã§ã¯ãæåã®ãæ
å ±ãç ´å£ãç¶ããªããããã°ã©ã ãé²è¡ãããã¨ããç¹å¾´ä»¥å¤ã¯è§¦ãã¾ããã§ããããèå³ã®ããããã¯ãç·ç§°é¢æ°ããmultimethodããClojure protocolããåã¯ã©ã¹ãã¨ãã§æ¤ç´¢ãã¦ã¿ãã¨ãªãã¸ã§ã¯ãæåã®å©ç¹ã ã¨èãã¦ãããã¨ããªãã¸ã§ã¯ãæåã«ç¹æã®æ©è½ã§ã¯ãªããã¨ãããããã¨æãã¾ãã
ã
ãªããã»ãã¨ã«æ¸ãããã£ããã¨ãããºã¬ããã¦ãã¤ãã¨åããããªå
容ã«ãªã£ã¦ãã¾ãã¾ãããããå¾¹å¤ãã¦æ¸ãã¦ãã®ã§ä¸åä¿åãã¦å¯ã¦èµ·ãã¦ããè¦ç´ãã¾ãã
*1:ä¾: Xã§é åã«nullãå ¥ãè¾¼ãã§Yã§ãã«ã
*2:ä¾: Yã¯ãã«ãããããZã§é åããnullãåãé¤ã
*3:ãªãã¸ã§ã¯ãæåãã¢ããªã³ã°ãå¾æã¨ããã¨ãã¦ããåéã§ããããã¯