ãªãã¸ã§ã¯ãåæååã¨ååä»ãçç¥å¯è½å¼æ°: top down vs. bottom up (2.5)
id:siokoshou:20080130:p2 ãé¢ç½ãã£ãã®ã§çªå¤ç·¨ï¼
IEnumerator<T> é£éã®åãã spy ããï¼
ãIEnumerator<T> ãå®è£
ãã¦ããã°å¿
ã IDisposable ã§ããçç± - NyaRuRuã®æ¥è¨ãã§è¦ãããã«ï¼IEnumerator<T> ã¯ã¹ãã¼ããã·ã³ã§èª¬æã§ãã¾ãï¼ãããã IEnumerator<T> é£éã¯é£çµã¹ãã¼ããã·ã³ã¨ãããã¨ã«ãªãã¾ããï¼ãã®é£éã«ã¢ãã¿ãªã³ã°å°ç¨ã®ã¹ãã¼ããã·ã³ãã¯ããã§ã¿ããï¼ã¨ãã話ã§ãï¼å®éããã¯æä½ Achiral ã«æ¢ã«çµã¿è¾¼ã¾ãã¦ãã¾ãï¼
å®è£
ã¨ãã¦ã¯ï¼ã¡ããã©éã«æã¾ããããªä½ãããªã IEnumerator<T> ãä½ã£ã¦ï¼ä¸æµã«ã¤ãã³ãããã©ã¯ã¼ãããåå¾ã§ç»é²ãããããªã²ã¼ããã³ã¼ã«ããã¯ããã°è¯ãããã§ãï¼ã§ï¼åé¡ã¯ãã®ã³ã¼ã«ããã¯ã®æ°ã 10 ãããããã¨ããã¨ããã§ï¼ãã¦ã©ããã£ã¦ç»é²ãããã®ãã¨ãã話ã§ãï¼
ãªãã¸ã§ã¯ãåæååãååä»ãçç¥å¯è½å¼æ°ã®ä»£ããã«ä½¿ã
ç¾å®ã«ã¯ 10 åå
¨ã¦ã®ã¤ãã³ããã¢ãã¿ãªã³ã°ãããã¨ãããã¨ã¯ããããããã¾ããï¼ã¨ãªãã¨ï¼ãã㯠IEnumerable<T> ãä½æããã¡ã½ããå¼æ°ãã©ããããã¨ããåé¡ãããã¾ãï¼VisualBasic ã§ããã°ï¼省略可能引数を名前付きで呼び出せば æ¸ã¿ããã§ããï¼C# ã§ãã®æã¯ä½¿ãã¾ããï¼
Achiral ã§ã¯ã©ãããå®è£
ã«ãããã¨ããã¨ï¼ããã§ããªãã¸ã§ã¯ãåæååãã使ãã¾ããï¼
using System; using System.Linq; using Achiral; using Achiral.Extension; static class Program { static void Main(string[] args) { var source = Make.Repeat(new Random()) .Select(rand => rand.Next(0, 100)) .Hook(new EnumeratorHook<int>() { OnBeforeBegin = () => Console.WriteLine("Beginning!"), OnAfterDispose = _ => Console.WriteLine("Disposed!"), }) .Take(10); new[] { source.Sum(), source.Count(), source.Max(), source.Min() }.ConsoleWriteLine(); } }
ã³ã¼ã«ããã¯ã®ç»é²ã¯ EnumeratorHook<T> ã«è¡ãï¼è¨å®ãããªãã£ããã®ã¯ããã©ã«ãã³ã³ã¹ãã©ã¯ã¿ã§ null ã«æå®ããï¼ã¢ãã¿ãªã³ã°ã§ã¯ç¡è¦ãããã¨ããæ±ãã§ãï¼
å®è¡ãã¦ã¿ã¾ãï¼
Beginning! Disposed! Beginning! Disposed! Beginning! Disposed! Beginning! Disposed! 553 10 84 36
ã¨ã¾ããããªæãã§ï¼ãã¤ãã©ã¤ã³ã®æ§ç¯ã¨è§£ä½ã 4 åå®è¡ãããã®ãåããã¾ãï¼
å¼±ç¹ã¨ãã¦ã¯ generic class ã®ã³ã³ã¹ãã©ã¯ã¿å¼ã³åºãã§åæ¨è«ã使ããªãã¨ããã§ï¼ä¸ã®ä¾ã 㨠EnumeratorHook<int> ã® int ãå¿ååã ã£ãå ´åã«æ¸ãããããªãã£ãããããã®ã§ããï¼
C# 4.0 ã§ã¯ï¼ã³ã³ã¹ãã©ã¯ã¿å¼ã³åºã㧠generic åå¼æ°ãæ¨è«ãããä»çµã¿ãä½ã欲ãããããªæ°ããã¾ããï¼