ä»å¾ã®äºå®
ååã®ãã¯ãã¼ã¸ã£ã¼ã®èª¿æ»ãã§ã¯
ãクロージャーの調査(2) - エレファント・ビジュアライザー調査記録ãã§ã¯
- (è¨èª1) ã¯ãã¼ã¸ã£ã¼ã使ããC#ã®ã³ã¼ãã§è¨è¿°ããè¨èª
- (è¨èª2) ã¯ãã¼ã¸ã£ã¼ã使ããé常ã®è¨èª
- (è¨èª3) ã¯ãã¼ã¸ã£ã¼èª¿æ»ç¨è¨èª
ã«ã¤ãã¦èª¬æãã¦ãã¾ãããè¨èª3ã¯ã¾ã å®è£ ãã¦ãã¾ãããè¨èª2ããã¼ãµã³ã³ããã¼ã¿ã§æ¸ãç´ãã¦ããããæ¹é ãã¦è¨èª3ãå®è£ ãããã¨ãèãã¾ãã
ã中間報告(19) - エレファント・ビジュアライザー調査記録ãã§ã¯ä»¥ä¸ã®ãã®ã« PythonãTypeScript ã使ãã¨æ¸ãã¾ããããã¾ã C# ã§æ¸ãã¦ã¿ããã¨ã«ãã¾ãã
- ã¯ãã¼ã¸ã£ã¼èª¿æ»ç¨ã®è¨èª(è¨èª3)
- ã¤ãã¬ã¼ã¿ã¼ã«ãã£ã¦ãµã¼ãã¼ãè¨è¿°ããããã®è¨èª(è¨èª2)
- è«çããã°ã©ãã³ã°è¨èª
- è«çããã°ã©ãã³ã°ã«ããé¢æ°ããã°ã©ãã³ã°
ã¨æ¸ãã¾ããããã¾ãè¨èª3ã C# ã®ãµãã»ããã¨ãã¦å®ç¾©ãããã¨ãã§ããªããæ¤è¨ãããã¨æãã¾ããC# ã®ã³ã¼ãã¨ãã¦æ¸ããã¨ããããã§ãã¦ä¾¿å©ã ããã§ããã¾ã ChatGPT ã§èª¿ã¹ã¦ã¿ã¾ããã
å ¥å: ç°¡åãªé¢æ°åããã°ã©ãã³ã°è¨èªã®ä»æ§ããã§ããã ã C# ã®ãµãã»ããã«ãªãããã«ä½ã£ã¦ãã ãã
çµæ:
以ä¸ã«ãC# ã®ãµãã»ããã¨ãªãç°¡åãªé¢æ°åããã°ã©ãã³ã°è¨èªã®ä»æ§ã示ãã¾ãããã®è¨èªã¯ãç´ç²é¢æ°åã®ç¹å¾´ãæã¡ãªãããC# ã®æ§æã¨äºææ§ãããã¾ãã
è¨èªå: FunCSharp
1. åºæ¬è¦ç´
- å: int, bool, string, Unit (空ã®åãé¢æ°ãå¤ãè¿ããªãå ´åã«ä½¿ç¨)
- å¤ã®ã¤ãã¥ã¼ã¿ããªãã£: ãã¹ã¦ã®å¤æ°ã¨ãã¼ã¿æ§é ã¯ä¸å¤ã
- é¢æ°: ä¸ç´ãªãã¸ã§ã¯ããé¢æ°èªä½ãå¼æ°ãæ»ãå¤ã«ä½¿ç¨å¯è½ã
- å¼ã®ã¿: æã§ã¯ãªãããã¹ã¦ãå¼ã¨ãã¦è©ä¾¡ãããã
2. ææ³
宣è¨
let x = 42; // å®æ°å®£è¨ let add = (a, b) => a + b; // é¢æ°å®£è¨é¢æ°å¼ã³åºã
let result = add(5, 10);æ¡ä»¶åå²
let max = (a, b) => a > b ? a : b;å帰
let factorial = n => n == 0 ? 1 : n * factorial(n - 1);ãã¿ã¼ã³ããã
let describe = x => x switch { 0 => "zero", 1 => "one", _ => "many" };ãªã¹ãæä½
ãªã¹ãåã¯çµã¿è¾¼ã¿ã§æä¾ãããæä½ã¯é¢æ°åã®æ¹æ³ã§è¡ãã¾ãã
let list = [1, 2, 3]; let doubled = list.Select(x => x * 2); let filtered = list.Where(x => x > 1);3. å¶ç´
- ãã¥ã¼ã¿ãã«å¤æ°ãªã: var ãã¼ã¯ã¼ãã¯ä½¿ç¨ä¸å¯ã
- å¯ä½ç¨ãªã: é¢æ°ã¯å¤é¨ç¶æ ãå¤æ´ã§ããªãã
- å¶å¾¡æ§æã®å¶é: for, while ã¯ä½¿ç¨ä¸å¯ã代ããã«å帰ãé«éé¢æ°ãå©ç¨ã
4. æ¨æºã©ã¤ãã©ãª
- é«éé¢æ°: map, filter, reduce ãªã©ã
- ãã¼ã¿æ§é : ãªã¹ããã¿ãã«ããªãã·ã§ã³å (Some, None)ã
- çµã¿è¾¼ã¿é¢æ°: print, length, sum ãªã©ã
5. ãµã³ãã«ããã°ã©ã
ãã£ããããæ°å
let fib = n => n switch { 0 => 0, 1 => 1, _ => fib(n - 1) + fib(n - 2) }; let result = fib(10);ãªã¹ãå¦ç
let numbers = [1, 2, 3, 4, 5]; let squares = numbers.Select(x => x * x); let evenSquares = squares.Where(x => x % 2 == 0); print(evenSquares); // [4, 16]ãã®ä»æ§ã§ã¯ãC# ã®æ§æãæ´»ç¨ããªãããç´ç²é¢æ°åããã°ã©ãã³ã°ã®æ¦å¿µãã·ã³ãã«ã«å¦ã¹ã¾ããå¿ è¦ã«å¿ãã¦ã追å æ©è½ãæ¡å¼µã§ãã¾ãã