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
1.3k
C#で関数型プログラミング
Nextbeat Tech Bar:第五回関数型プログラミング(仮)の会でのLT資料です。
内容: C#での関数型プログラミングの要素について
dora56
January 26, 2024
Tweet
Share
More Decks by dora56
See All by dora56
Azure Container Apps 再入門
dora56
1
610
ASP.NET8 CoreでClean Architecture入門
dora56
0
790
Pulumi de Azure IaC
dora56
0
130
5分でわかった気になるDDD
dora56
0
78
Other Decks in Technology
See All in Technology
オブザーバビリティが育むシステム理解と好奇心
maruloop
2
1.3k
Okta Identity Governanceで実現する最小権限の原則 / Implementing the Principle of Least Privilege with Okta Identity Governance
tatsumin39
0
170
Retrospectiveを振り返ろう
nakasho
0
110
20251027_マルチエージェントとは
almondo_event
1
440
パフォーマンスチューニングのために普段からできること/Performance Tuning: Daily Practices
fujiwara3
2
130
SCONE - 動画配信の帯域を最適化する新プロトコル
kazuho
1
380
Building a cloud native business on open source
lizrice
0
180
AI時代におけるデータの重要性 ~データマネジメントの第一歩~
ryoichi_ota
0
720
AI機能プロジェクト炎上の 3つのしくじりと学び
nakawai
0
120
混合雲環境整合異質工作流程工具運行關鍵業務 Job 的經驗分享
yaosiang
0
190
AWS DMS で SQL Server を移行してみた/aws-dms-sql-server-migration
emiki
0
240
dbtとAIエージェントを組み合わせて見えたデータ調査の新しい形
10xinc
2
560
Featured
See All Featured
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
37
2.6k
YesSQL, Process and Tooling at Scale
rocio
173
15k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
230
22k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
658
61k
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.2k
Code Review Best Practice
trishagee
72
19k
Intergalactic Javascript Robots from Outer Space
tanoku
272
27k
Agile that works and the tools we love
rasmusluckow
331
21k
Typedesign – Prime Four
hannesfritz
42
2.8k
Navigating Team Friction
lara
190
15k
Reflections from 52 weeks, 52 projects
jeffersonlam
353
21k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.1k
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") }; 応⽤︖ 関数を返すパターンマッチングで いろいろできそう
もっと知りたい⽅へ 最後に
終