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
570
ASP.NET8 CoreでClean Architecture入門
dora56
0
710
Pulumi de Azure IaC
dora56
0
120
5分でわかった気になるDDD
dora56
0
75
Other Decks in Technology
See All in Technology
夏休みWebアプリパフォーマンス相談室/web-app-performance-on-radio
hachi_eiji
0
190
ユーザー課題を愛し抜く――AI時代のPdM価値
kakehashi
PRO
1
120
Agent Development Kitで始める生成 AI エージェント実践開発
danishi
0
150
JAWS AI/ML #30 AI コーディング IDE "Kiro" を触ってみよう
inariku
3
370
Kiroでインフラ要件定義~テスト を実施してみた
nagisa53
3
360
専門分化が進む分業下でもユーザーが本当に欲しかったものを追求するプロダクトマネジメント/Focus on real user needs despite deep specialization and division of labor
moriyuya
1
1.3k
「Roblox」の開発環境とその効率化 ~DAU9700万人超の巨大プラットフォームの開発 事始め~
keitatanji
0
120
【新卒研修資料】数理最適化 / Mathematical Optimization
brainpadpr
27
13k
アカデミーキャンプ 2025 SuuuuuuMMeR「燃えろ!!ロボコン」 / Academy Camp 2025 SuuuuuuMMeR "Burn the Spirit, Robocon!!" DAY 1
ks91
PRO
0
150
Jamf Connect ZTNAとMDMで実現! 金融ベンチャーにおける「デバイストラスト」実例と軌跡 / Kyash Device Trust
rela1470
1
200
バクラクによるコーポレート業務の自動運転 #BetAIDay
layerx
PRO
1
950
마라톤 끝의 단거리 스퍼트: 2025년의 AI
inureyes
PRO
1
750
Featured
See All Featured
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4k
Testing 201, or: Great Expectations
jmmastey
45
7.6k
Scaling GitHub
holman
461
140k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
18
1.1k
[RailsConf 2023] Rails as a piece of cake
palkan
56
5.8k
The Invisible Side of Design
smashingmag
301
51k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
23
1.4k
Build The Right Thing And Hit Your Dates
maggiecrowley
37
2.8k
Balancing Empowerment & Direction
lara
1
540
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
34
6k
Why You Should Never Use an ORM
jnunemaker
PRO
58
9.5k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
44
2.4k
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") }; 応⽤︖ 関数を返すパターンマッチングで いろいろできそう
もっと知りたい⽅へ 最後に
終