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
C#で関数型プログラミング
Nextbeat Tech Bar:第五回関数型プログラミング(仮)の会でのLT資料です。
内容: C#での関数型プログラミングの要素について
dora56
January 26, 2024
More Decks by dora56
See All by dora56
Azure Container Apps 再入門
dora56
1
680
ASP.NET8 CoreでClean Architecture入門
dora56
1
910
Pulumi de Azure IaC
dora56
0
150
5分でわかった気になるDDD
dora56
0
89
Other Decks in Technology
See All in Technology
小説執筆のハーネスエンジニアリング
yoshitetsu
0
670
Digitization部 紹介資料
sansan33
PRO
1
7.3k
レビューしきれない?それは「全て人力でのレビュー」だからではないでしょうか
amixedcolor
0
320
「SaaSの次の時代」に重要性を増すステークホルダーマネジメントの要諦 ~解像度を圧倒的に高めPdMの価値を最大化させる方法~
kakehashi
PRO
1
180
Choose your own adventure in agentic design patterns
glaforge
0
130
Shipping AI Agents — Lessons from Production
vvatanabe
0
220
Amazon S3 Filesについて
yama3133
2
210
EBS暗号化に失敗してEC2が動かなくなった話
hamaguchimmm
2
200
Revisiting [CLS] and Patch Token Interaction in Vision Transformers
yu4u
0
360
ARIA Notifyについて
ryokatsuse
1
120
「責任あるAIエージェント」こそ自社で開発しよう!
minorun365
9
2k
LLM時代の検索アーキテクチャと技術的意思決定
shibuiwilliam
3
1.2k
Featured
See All Featured
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.4k
Testing 201, or: Great Expectations
jmmastey
46
8.1k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.4k
The Art of Programming - Codeland 2020
erikaheidi
57
14k
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
520
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
180
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
480
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
0
290
Ten Tips & Tricks for a 🌱 transition
stuffmc
0
99
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.4k
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
110
Product Roadmaps are Hard
iamctodd
PRO
55
12k
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") }; 応⽤︖ 関数を返すパターンマッチングで いろいろできそう
もっと知りたい⽅へ 最後に
終