Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
C#で関数型プログラミング
Search
dora56
January 26, 2024
Technology
1.4k
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
C#で関数型プログラミング
Nextbeat Tech Bar:第五回関数型プログラミング(仮)の会でのLT資料です。
内容: C#での関数型プログラミングの要素について
dora56
January 26, 2024
More Decks by dora56
See All by dora56
Azure Container Apps 再入門
dora56
1
710
ASP.NET8 CoreでClean Architecture入門
dora56
1
970
Pulumi de Azure IaC
dora56
0
160
5分でわかった気になるDDD
dora56
0
96
Other Decks in Technology
See All in Technology
キャリアの中で本を作る / Making a Book During Your Career
ak1210
0
110
『AIに負けない』より『AIと遊ぶ』」〜ワクワクが最強のテスト・QA学習戦略_公開用
odan611
2
510
NDIAS CTF 2026 問題解説会資料
bata_24
0
180
人を動かすのは時間ではなく、納得感 〜新任EMが入社3ヶ月、組織を2回変えた話〜
kakehashi
PRO
2
150
product engineering with qa
nealle
0
150
そのタスクオンスケですか?
poropinai1966
0
140
Claude Codeとハーネスについて考えてみる
oikon48
18
8.7k
Empower GenAI with Agile - あなたのアジャイルが生成AIのバフになる仕組み
hageyahhoo
1
130
AIDLC_ヤフーショッピングの取り組み
lycorptech_jp
PRO
0
560
Baseline対応のDOMの型定義を作った
uhyo
3
720
AWS Blocks を触ってみた/first-tach-aws-blocks
fossamagna
2
150
SRE Lounge Hiroshimaへの招待
grimoh
0
380
Featured
See All Featured
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
1
270
First, design no harm
axbom
PRO
2
1.2k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2k
Thoughts on Productivity
jonyablonski
76
5.2k
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.6k
Designing for Timeless Needs
cassininazir
1
270
Product Roadmaps are Hard
iamctodd
55
12k
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
200
Game over? The fight for quality and originality in the time of robots
wayneb77
1
220
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
670
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
240
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
0
190
Transcript
Nextbeat Tech Bar︓第五回関数型プログラミング(仮)の会 C#で関数型プログラミング
/r-hanaoka @dora56 @karyu721 l 花岡 ⿓ l 株式会社 オルターブース l
Azure, GitHub 導⼊⽀援 l 趣味: 読書・ゲーム ⾃⼰紹介
話すこと、話さないこと l 話すこと l C#でできる基本的な関数型プログラミング要素 l 話さないこと l F#の話 l
C#の応⽤的な関数プログラミング
概要 l C#について l C#の関数型プログラミングの要素 l デリゲート型 l LINQ l
record型 l パターンマッチング
C#を知らない⼈のための概要 Microsoft製のプログラミング⾔語 .NETプラットフォームで提供されているプログラミング⾔語の1つ ※他にはF#やVisual Basicがある クロスプラットフォーム Windows, Linux, Macなどでプログラミング可能 マルチパラダイム
⼿続き型、オブジェクト指向、関数型などをサポート C#について
デリゲート(delegate: 代表、移譲、委託) delegate int AddDelegate(int x, int y); static int
StaticAdd(int x, int y) => x + y; static void Main() { var addDelegate = StaticAdd; var reslut = addDelegate(1, 2); Console.WriteLine(reslut); // output: 3 } C#の関数型プログラミングの要素 メソッドを参照する型 処理を他のメソッドに移譲できる CやC++で⾔うと関数ポインタ 実際の値が必要になると実⾏(遅延評価) delegateを複数登録できる
デリゲートの派⽣(Action, Func) static Action<string> PrintAction = x => Console.WriteLine(x); PrintAction(“Hello
World”); // output: Hello World public delegate void Action<in T>(T obj); // C#での定義 C#の関数型プログラミングの要素 Action型 値を返さないdelegate 引数は0~16まで対応 static Func<int, string> IntToStrFunc = x => x.ToString(); Console.WriteLine(IntToStrFunc(1)); // output: 1 public delegate TResult Func<in T,out TResult>(T arg); // 定義 Func型 値を返すdelegate 引数は0~16まで対応
デリゲートの発展 var add = (int x) => (int y) =>
x + y; var add10 = add(10); var ansA = add10(10); var ansB = add10(-10); var ansC = add(10)(90); Console.WriteLine(ansA); // 20 Console.WriteLine(ansB); // 0 Console.WriteLine(ansC); // 100 var add1 = add.Partial(10); // 動作しない add1(10); C#の関数型プログラミングの要素 ラムダ式 デリゲートを簡略化 カリー化と部分適⽤ カリー化も簡単に書ける 部分適応はサポートされていないので、 拡張メソッドという機能を使って実装する
統合⾔語クエリ(LINQ: Language-Integrated Query) var result = Enumerable.Range(1, 5) .Select(x =>
x * x) // 1, 4, 9, 16, 25 .Where(x => x > 10) // 16, 25 .Sum(); Console.WriteLine(result); // output: 29 C#の関数型プログラミングの要素 LINQとは 配列やコレクションを操作できる機 能 SQLのような感覚でクエリを 書ける List<List<int>> input = [[1, 2, 3], [4, 5, 6]]; var result = input.SelectMany(x => x); var output = string.Join(Environment.NewLine, result); Console.WriteLine(output); // output: 1, 2, 3, 4, 5, 6 SelectManyでflatmap相当の処理 が可能
record record Person(string FirstName, string LastName); static void Main() {
Person person = new("Nancy", "Davolio"); Console.WriteLine(person.FirstName); // output: Nancy person.FirstName = "Robert"; // error: 読み取り専⽤ var person2 = person with { FirstName = "Robert" }; Console.WriteLine(person.FirstName); // output: Nancy Console.WriteLine(person2.FirstName); // output: Robert Console.WriteLine(person == person2); // output: False person2 = person with { }; Console.WriteLine(person == person2); // output: True } C#の関数型プログラミングの要素 特徴 不変プロパティを持つ参照型の クラスの修飾⼦ なんらかの値を変更する場合は with 式を使⽤ 同じ型で同じ値を持つなら等しい
パターンマッチング public record Person(string Name, int Age); public string CallAdult(Person
person) => person switch { { Age: > 20 } => $"{person.Name} is an adult", _ => $"{person.Name} is not an adult " }; C#の関数型プログラミングの要素 できること 式を評価してアクションを実⾏ “_”で⼀致しなかった評価を処理 (破棄パターン) public Func<int, int> Dispatcher(string command) => command switch { "add" => Add, "sub" => Sub, "mul" => Mul, "div" => Div, _ => throw new ArgumentException("Invalid command") }; 応⽤︖ 関数を返すパターンマッチングで いろいろできそう
もっと知りたい⽅へ 最後に
終