SlideShare a Scribd company logo
1 of 11
コンピュテーション式
ハンズオン
【第二回】 Fun Fan Fsharp 発表資料
自己紹介
京德 優希 (きょうとく ゆうき)
1994年12月20日生まれ(現在26歳)
Twitter : ゆきあん@snowapricot47
2018年、神奈川県立産業技術短期大学校を卒業
卒業研究で関数型プログラミング(F#)を知る
好きなプログラミング言語:F#/Elm
趣味:ボードゲーム
コンピュテーション式とは?
F#の言語機能のひとつ
F#の構文をメソッド呼び出しに置き換えるマクロのよ
うなもの
特定の出来事を解決するための構文を表現すること
に利用されている(DSL)
例:SeqBuilder QueryBuilder AsyncBuilder
// コンピュテーション式
builder {
let! x = 3
let! y = 5
return x + y
}
// 置き換え後
builder.Run(
builder.Delay(fun () ->
builder.Bind(3, fun x ->
builder.Bind(5, fun y ->
builder.Return(x + y)
)
)
)
)
今回のスコープ
1. コンピュテーション式の基本
2. Return 変換
3. Zero 変換
4. Bind 変換
コンピュテーション式の基本
中括弧(「{」と「}」のこと)で囲われた「式」の部分について、「オブジェクト」のメソッド
呼び出しに置き換える
特に決まりはないが、「~Builder」という名前のクラスを作るのが一般的
※以降の説明では、「オブジェクト」を「builder」という変数で表記します
オブジェクト {
式
}
type HogeBuilder() =
...
let builder = HogeBuilder()
Return 変換
「return」キーワードを変換する場合に現れる
「return」の後の値を引数として、Returnメソッ
ドを呼び出される
複数の「return」を並べるのはまた別のお話
// コンピュテーション式
builder { return 1 }
// 置き換え後
builder.Return(1)
Zero 変換
末尾に「return」が存在しない場合に現れる
unitを引数としてZeroメソッドが呼び出される
いくつ並べても、Zeroメソッドが呼ばれるのは
一回だけ
// コンピュテーション式
builder { printfn "Hello" }
// 置き換え後
printfn "Hello"; builder.Zero()
// コンピュテーション式
builder {
printfn "Hello"
printfn "World"
}
// 置き換え後
printfn "Hello"
printfn "World"
builder.Zero()
Bind 変換
「let!」キーワードを変換する場合に現れる
第一引数に束縛する値、第二引数に式全体
を関数にしたもの(右の図ではラムダ式で表
現)を引数としてBindメソッドが呼び出される
比較のために「let」キーワードを変換する場
合も記載 // コンピュテーション式
builder { let! value = 5 in 式 }
// 置き換え後
builder.Bind( 5,
fun value -> builder { 式 }
)
// コンピュテーション式
builder { let value = 3 in 式 }
// 置き換え後
let value = 3 in builder { 式 }
まずはハンズオン!
同じ実行環境を使いたいので、以下のサイトで一緒にやってみましょう
https://tryfsharp.fsbolero.io/
または
「tryfsharp」で検索
WebAssembly F# Compiler
参考になるページ
FSharpSpec-4.1-latest.pdf (67ページ~)
https://fsharp.org/specs/language-spec/4.1/FSharpSpec-4.1-latest.pdf
コンピュテーション式 - F# | Microsoft Docs
https://docs.microsoft.com/ja-jp/dotnet/fsharp/language-
reference/computation-expressions
詳説コンピュテーション式 - ぐるぐる~ (hatenablog.com)
https://bleis-tift.hatenablog.com/entry/computation-expression
Computation expressions: Introduction | F# for fun and profit
(fsharpforfunandprofit.com)
https://fsharpforfunandprofit.com/posts/computation-expressions-intro/
ご清聴ありがとうございました

More Related Content

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

コンピュテーション式ハンズオン