SlideShare uma empresa Scribd logo
1 de 26
第二回だか第三回だか
tento.tech定例会
自己紹介
• 鈴木颯介
• tento.tech広報
• 高校一年生
• @tako_programing
前回から一ヶ月間何をやっていたのか
• 一時期収まっていたscala欲が再熱してscalascalaし
ていた。
• 技術書を衝動買いして適当に読み進めては積んでい
た(プログラミングelixirなど)
• フロントエンド界隈の常識が若干ついてきた
• 一般常識は欠陥してきた
前回から一ヶ月間何をやっていたのか
• ReactできないけどReact/ReactNative入門者の会とか
に参加してた。
• scalatraの練習とかでディズニーの待ち時間を返すAPI
を作ったり。(完成してないよ)
• 関数型言語尊いと思うけど関数型っぽい書き方ができ
ない悩みを抱えてます
• あとはガソリンスタンドでガソリン入れたり経由いれ
たり灯油いれたりしてた
scalaはイイぞ
別にscalaにめちゃくちゃ詳しいわけではないけど
やはりscalaが好きなので、scalaの話をします
scalaはイイぞ
オブジェクト指向関数型プログラミング言語
JVMで動作するのでJavaのライブラリが使えます
強い静的型付け(良い)
型推論もあるよ(haskellとかMLよろしくのhindley
milnerアルゴリズムではない)
Javaにも型推論はあるけど使い方がスマートではない
ような気がしてならない
scalaはイイぞ
今回は型推論について少しだけ
サンプルを交えて解説するよ
scalaの型推論
ScalaのListには、2つの要素を比較する
(T, T) => Boolean
の関数を引数にとりソートする関数、
sortWithが定義されています。
簡単なリストのソートの例
scalaの型推論
次にListの外に(T, T)=>Booleanの関数と任意のList[T]
をとってさっきと同じようにソートする関数msortを定義
しますね。これは有名なソートアルゴリズムで、
「scala マージソート」とかでググれば多分定義がでてきます
scalaの型推論
msortでさっきのsortWithと同じことをしようとすると
こういう感じになります。
scalaの型推論
msortの第一引数はなんでこんな長くなるの?
scalaの型推論
msortはカリー化された関数
↓
第一引数に(T, T) => Booleanの関数を受けると
List[T]を引数にとる関数を返す
※カリー化ってなんだよっていう人は
すごいH本とかコップ本とかを読んでください
scalaの型推論
msortの第一引数に((x: Int, y: Int) => x >y)が渡る
↓
型推論器は「お、Intじゃん、了解。」ってなる。
↓
型推論器は「よしじゃあ次に受け取るのはList[Int]ね」
って上手いこと察してくれてList[Int]を引数にとる関数
が返ってくる。
scalaの型推論
msortに(_ > _)が渡る
↓
型推論器は「いやそんないきなり略記法で書かれても
どんな型かわかる訳ないだろ」ってなる
scalaの型推論
ちなみにさっき
って書きましたが
でも推論はしてくれますごめんなさい
scalaの型推論
ちなみにこれ実は、msortの定義の引数の順番を
入れ替えるだけで簡単に解決できます
例えば第一引数でIntのリストが渡されると
推論器は「あ、おっけえInt型ね。」って
(Int, Int)=>Boolean
を引数にとる関数を返してくれるようになる
scalaの型推論
畳み込みの例
scalaの型推論
scalaの右畳み込みです
zがアキュムレータ、xsが畳み込むリスト
opを二引数の関数(演算)として畳み込みます
scalaの型推論
flattenLeftというリストのリストをとって
1つに連結して返す関数を定義します。
↑の関数は正常に動きますが、↓の関数は
期待どおりの挙動をしません
scalaの型推論
なんで?
scalaの型推論
xsは任意の型のリストなので、型はList[A]である
zは別の型Bである可能性がある
つまりopは(A, B)=>Bであるといえる。
scalaの型推論
scalaではList()は型情報が何もないとList[Nothing]
であると判断されてしまう
そのため
における演算op(_ ::: _)は
だと判断されてしまう
scalaの型推論
つまり演算opは、任意の型と空リストをとって確実に
空リストを返す意味の分からない演算になってしまう
これの解決策はList[T]のように型アノテーションを
明示的に示すしかないので残念ですね
第一引数を評価してから第二引数を引数にとる
関数を返すカリー化の本来は便利なはずの原則
がscalaの型推論においてはちょっとアレ
scalaの型推論
Haskellとかのhindley milner型推論アルゴリズム
のほうが視野は広い(らしい)
のでこういった煩わしさはないらしい
ただ、scalaの型推論アルゴリズムは
オブジェクト指向におけるサブ型の概念を穏便に
扱ってくれるんだとか
おしまい
ちなみに今回の内容はだいたいコップ本に書いてある
のでコップ本をよんでください。
おしまい
ぜひ読もう

Mais conteúdo relacionado

Destaque

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)
 

Destaque (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...
 

第3回tento.tech定例会