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.2k
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
530
ASP.NET8 CoreでClean Architecture入門
dora56
0
620
Pulumi de Azure IaC
dora56
0
120
5分でわかった気になるDDD
dora56
0
70
Other Decks in Technology
See All in Technology
Software Architecture in an AI-Driven World
atty303
42
16k
LLMの開発と社会実装の今と未来 / AI Builders' Community (ABC) vol.2
pfn
PRO
2
160
名単体テスト 禁断の傀儡(モック)
iwamot
PRO
1
280
Coding Agentに値札を付けろ
watany
3
540
Асинхронная коммуникация в Go: от понятного к душному. Дима Некрасов, Otello, 2ГИС
lamodatech
0
2.2k
"発信文化"をどうやって計測する?技術広報のKPI探索記/How do we measure communication culture?
bitkey
4
320
Previewでもここまで追える! Azure AI Foundryで始めるLLMトレース
tomodo_ysys
2
710
分解し、導き、託す ログラスにおける“技術でリードする” 実践の記録
hryushm
0
350
インフラからSREへ
mirakui
13
4.6k
計装を見直してアプリケーションパフォーマンスを改善させた話
donkomura
1
130
20250514 1Passwordを使い倒す道場 vol.1
east_takumi
0
130
テストコードにはテストの意図を込めよう(2025年版) #retechtalk / Put the intent of the test 2025
nihonbuson
PRO
9
1.7k
Featured
See All Featured
Site-Speed That Sticks
csswizardry
6
540
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
120
52k
GraphQLの誤解/rethinking-graphql
sonatard
71
11k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
13
850
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
30
2k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
29
9.5k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
32
2.3k
Product Roadmaps are Hard
iamctodd
PRO
53
11k
Statistics for Hackers
jakevdp
799
220k
Designing Experiences People Love
moore
142
24k
How to Ace a Technical Interview
jacobian
276
23k
RailsConf 2023
tenderlove
30
1.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") }; 応⽤︖ 関数を返すパターンマッチングで いろいろできそう
もっと知りたい⽅へ 最後に
終