SlideShare a Scribd company logo
1 of 55
Download to read offline
in
Scala Fukuoka 2019
https://goo.gl/iKyEKs
•
Scala 

Scala ?
?
•Scala
•
•
Scala ?
•
•
•
•
•
•
•
•
f(x) = x + 1
Scala
• = ( )
•
Which
is
better?
def f(x: Int) {x + 1}
def f(x: Int) = x + 1
def f(x: Int) = return x + 1
※ : 2
( )
• (mutable)
•
•
?
int sum = 0;
for (int i = 0; i < array.length; i++) {
sum += array[i];
}
return sum;
https://goo.gl/iKyEKs
• 1 n
•
int f(int n) {
int total = 0;
for (int i = 1; i <= n; i++) {
total += i;
}
return total;
}
f(1) = 1
f(2) = 1 + 2
f(3) = 1 + 2 + 3
...
f(n) = 1 + 2 + 3 + ... + n
f(1) = 1
f(2) = f(1) + 2
f(3) = f(2) + 3
...
f(n) = f(n - 1) + n
f(1) = 1
f(n) = f(n - 1) + n
def f(n: Int): Int =
if (n == 1) 1
else f(n - 1) + n
f(1) = 1
f(n) = f(n - 1) + n
f(0) = 1
f(1) = 1
f(2) = 2 * 1
f(3) = 3 * 2 * 1
...
f(n) = n * ... * 3 * 2 * 1
f(0) = 1
f(1) = 1 * f(0)
f(2) = 2 * f(1)
f(3) = 3 * f(2)
...
f(n) = n * f(n - 1)
f(0) = 1
f(n) = n * f(n - 1)
def f(n: Int): Int =
if (n == 0) 1
else n * f(n - 1)
n
?
1, 1, 2, 3, 5, 8, 13, …
f(0) = 0
f(1) = 1
f(2) = 0 + 1
f(3) = 1 + 1
f(4) = 1 + 2
f(5) = 2 + 3
...
f(0) = 0
f(1) = 1
f(2) = f(0) + f(1)
f(3) = f(1) + f(2)
f(4) = f(2) + f(3)
f(5) = f(3) + f(4)
...
f(n) = f(n - 2) + f(n - 1)
f(0) = 0
f(1) = 1
f(n) = f(n - 2) + f(n - 1)
def f(n: Int): Int =
if (n == 0) 0
else if (n == 1) 1
else f(n - 2) + f(n - 1)
sum
def sum(ints: List[Int]): Int
• Nil
•head tail
head :: tail
3
Nil
Nil::
3 :: Nil2 ::
2 :: 3 :: Nil1 ::
•
•


Nil
Nil::8
8 :: Nil::2
2 :: 8 :: Nil::1
1 :: 2 :: 8 :: Nil::5
5 :: 1 :: 2 :: 8 :: Nil
sum(5 :: 1 :: 2 :: 8 :: Nil)
sum( )

= + sum( )
sum( ) = + sum( )
sum( ) = + sum( )
sum( ) = + sum( )
sum( ) = 0Nil
8 :: Nil
2 :: 8 :: Nil
5 :: 1 :: 2 :: 8 :: Nil
Nil8
8 :: Nil2
2 :: 8 :: Nil1
5 1 :: 2 :: 8 :: Nil
1 :: 2 :: 8 :: Nil
sum( ) = + sum( )
sum( ) = + sum( )
sum( ) = + sum( )
sum( ) = 0Nil
8 :: Nil
2 :: 8 :: Nil
Nil8
8 :: Nil22 :: 8 :: Nil
1
head
head tail
head tail
head tail head :: tail
1 :: 2 :: 8 :: Nil
tail
sum(Nil) = 0
sum(head :: tail) = head + sum(tail)
def sum(list: List[Int]): Int =
if (list.isEmpty) 0
else list.head + sum(list.tail)
def sum(list: List[Int]): Int = list match {
case Nil => 0
case head :: tail => head + sum(tail)
}
product
def product(ints: List[Int]): Int
max
def max(ints: List[Int]): Int
reverse
def reverse(ints: List[Int]): List[Int]
length
def length(ints: List[Int]): Int
sum
def sum(tree: Tree): Int
•Node Empty Tree
•Node Tree
•Empty Tree
8
6 10
4 7 E 12
E E E E E E
Tree
Node
Tree Tree
8
6 10
4 7 E 12
E E E E E E
8
6 10
4 7 E 12
E E E E E E
sum
8
6 10
4 7 E 12
E E E E E E
sum sum
+
sum(tree)
tree
8
6 10
4 7 E 12
E E E E E E
sum(tree) = node.value

+ sum(leftTree) + sum(rightTree)
leftTree rightTree
+
node.value
8
6 10
4 7 E 12
E E E E E E
sum(empty) = 0
sum(tree) = node.value + sum(left) + sum(right)
trait Tree
case class Node(value: Int,
left: Tree,
right: Tree) extends Tree
case object Empty extends Tree
def sum(tree: Tree): Int = tree match {
case Empty => 0
case n: Node =>
n.value + sum(n.left) + sum(n.right)
}
max
def max(tree: Tree): Int
Node
size
def size(tree: Tree): Int
Node
depth
def depth(tree: Tree): Int
def sum(list: List[Int]): Int = list match {
case Nil => 0
case head :: tail => head + sum(tail)
}
def sum(list: List[Int]): Int = {
def loop(acc: Int, l: List[Int]): Int = l match {
case Nil => acc
case head :: tail => loop(acc + head, tail)
}
loop(0, list)
}
• Functional Programming Principles in Scala

Scala Martin Odersky


https://www.coursera.org/course/progfun
• 

OCaml


http://www.amazon.co.jp/dp/4781911609
• Scala & ―
Scalaz


2 

http://www.amazon.co.jp/dp/4844337769

More Related Content

What's hot

NumPyの歴史とPythonの並行処理【PyData.tokyo One-day Conference 2018】
NumPyの歴史とPythonの並行処理【PyData.tokyo One-day Conference 2018】NumPyの歴史とPythonの並行処理【PyData.tokyo One-day Conference 2018】
NumPyの歴史とPythonの並行処理【PyData.tokyo One-day Conference 2018】
Atsuo Ishimoto
 

What's hot (20)

Numpy tutorial(final) 20160303
Numpy tutorial(final) 20160303Numpy tutorial(final) 20160303
Numpy tutorial(final) 20160303
 
Python Basics #1
Python Basics #1Python Basics #1
Python Basics #1
 
Python seaborn cheat_sheet
Python seaborn cheat_sheetPython seaborn cheat_sheet
Python seaborn cheat_sheet
 
NumPyの歴史とPythonの並行処理【PyData.tokyo One-day Conference 2018】
NumPyの歴史とPythonの並行処理【PyData.tokyo One-day Conference 2018】NumPyの歴史とPythonの並行処理【PyData.tokyo One-day Conference 2018】
NumPyの歴史とPythonの並行処理【PyData.tokyo One-day Conference 2018】
 
行列演算とPythonの言語デザイン
行列演算とPythonの言語デザイン行列演算とPythonの言語デザイン
行列演算とPythonの言語デザイン
 
Numpy python cheat_sheet
Numpy python cheat_sheetNumpy python cheat_sheet
Numpy python cheat_sheet
 
Statistics - ArgMax Equation
Statistics - ArgMax EquationStatistics - ArgMax Equation
Statistics - ArgMax Equation
 
2017 biological databasespart2
2017 biological databasespart22017 biological databasespart2
2017 biological databasespart2
 
Functions
FunctionsFunctions
Functions
 
Haskell 101
Haskell 101Haskell 101
Haskell 101
 
Python matplotlib cheat_sheet
Python matplotlib cheat_sheetPython matplotlib cheat_sheet
Python matplotlib cheat_sheet
 
Functional programming from its fundamentals
Functional programming from its fundamentalsFunctional programming from its fundamentals
Functional programming from its fundamentals
 
Hash
HashHash
Hash
 
Pandas pythonfordatascience
Pandas pythonfordatasciencePandas pythonfordatascience
Pandas pythonfordatascience
 
Python bokeh cheat_sheet
Python bokeh cheat_sheet Python bokeh cheat_sheet
Python bokeh cheat_sheet
 
Intoduction to numpy
Intoduction to numpyIntoduction to numpy
Intoduction to numpy
 
Cheat Sheet for Machine Learning in Python: Scikit-learn
Cheat Sheet for Machine Learning in Python: Scikit-learnCheat Sheet for Machine Learning in Python: Scikit-learn
Cheat Sheet for Machine Learning in Python: Scikit-learn
 
06 - 20 Jan - Divide and Conquer
06 - 20 Jan - Divide and Conquer06 - 20 Jan - Divide and Conquer
06 - 20 Jan - Divide and Conquer
 
NumPy Refresher
NumPy RefresherNumPy Refresher
NumPy Refresher
 
Артём Акуляков - F# for Data Analysis
Артём Акуляков - F# for Data AnalysisАртём Акуляков - F# for Data Analysis
Артём Акуляков - F# for Data Analysis
 

Similar to 関数プログラミングことはじめ in 福岡

JDD2015: Functional programing and Event Sourcing - a pair made in heaven - e...
JDD2015: Functional programing and Event Sourcing - a pair made in heaven - e...JDD2015: Functional programing and Event Sourcing - a pair made in heaven - e...
JDD2015: Functional programing and Event Sourcing - a pair made in heaven - e...
PROIDEA
 
02 Arrays And Memory Mapping
02 Arrays And Memory Mapping02 Arrays And Memory Mapping
02 Arrays And Memory Mapping
Qundeel
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meet
Mario Fusco
 
Lesson 01 A Warmup Problem
Lesson 01 A Warmup ProblemLesson 01 A Warmup Problem
Lesson 01 A Warmup Problem
Mitchell Wand
 

Similar to 関数プログラミングことはじめ in 福岡 (20)

関数プログラミングことはじめ revival
関数プログラミングことはじめ revival関数プログラミングことはじめ revival
関数プログラミングことはじめ revival
 
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語
 
JDD2015: Functional programing and Event Sourcing - a pair made in heaven - e...
JDD2015: Functional programing and Event Sourcing - a pair made in heaven - e...JDD2015: Functional programing and Event Sourcing - a pair made in heaven - e...
JDD2015: Functional programing and Event Sourcing - a pair made in heaven - e...
 
02 Arrays And Memory Mapping
02 Arrays And Memory Mapping02 Arrays And Memory Mapping
02 Arrays And Memory Mapping
 
Purely Functional Data Structures in Scala
Purely Functional Data Structures in ScalaPurely Functional Data Structures in Scala
Purely Functional Data Structures in Scala
 
Scalapeno18 - Thinking Less with Scala
Scalapeno18 - Thinking Less with ScalaScalapeno18 - Thinking Less with Scala
Scalapeno18 - Thinking Less with Scala
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meet
 
Elixir -Tolerância a Falhas para Adultos - GDG Campinas
Elixir  -Tolerância a Falhas para Adultos - GDG CampinasElixir  -Tolerância a Falhas para Adultos - GDG Campinas
Elixir -Tolerância a Falhas para Adultos - GDG Campinas
 
Ch01 basic concepts_nosoluiton
Ch01 basic concepts_nosoluitonCh01 basic concepts_nosoluiton
Ch01 basic concepts_nosoluiton
 
Advanced data structure
Advanced data structureAdvanced data structure
Advanced data structure
 
Prelude to halide_public
Prelude to halide_publicPrelude to halide_public
Prelude to halide_public
 
Python 101 language features and functional programming
Python 101 language features and functional programmingPython 101 language features and functional programming
Python 101 language features and functional programming
 
Bottomupparser
BottomupparserBottomupparser
Bottomupparser
 
Bottomupparser
BottomupparserBottomupparser
Bottomupparser
 
Bottomupparser
BottomupparserBottomupparser
Bottomupparser
 
Swift Rocks #2: Going functional
Swift Rocks #2: Going functionalSwift Rocks #2: Going functional
Swift Rocks #2: Going functional
 
Why Haskell Matters
Why Haskell MattersWhy Haskell Matters
Why Haskell Matters
 
ALF 5 - Parser Top-Down (2018)
ALF 5 - Parser Top-Down (2018)ALF 5 - Parser Top-Down (2018)
ALF 5 - Parser Top-Down (2018)
 
Lesson 01 A Warmup Problem
Lesson 01 A Warmup ProblemLesson 01 A Warmup Problem
Lesson 01 A Warmup Problem
 
C Code and the Art of Obfuscation
C Code and the Art of ObfuscationC Code and the Art of Obfuscation
C Code and the Art of Obfuscation
 

More from Naoki Kitora (6)

機械学習とデータ分析プロセス
機械学習とデータ分析プロセス機械学習とデータ分析プロセス
機械学習とデータ分析プロセス
 
Developers summit 2016_kansai
Developers summit 2016_kansaiDevelopers summit 2016_kansai
Developers summit 2016_kansai
 
関数プログラミングことはじめ
関数プログラミングことはじめ関数プログラミングことはじめ
関数プログラミングことはじめ
 
命令プログラミングから関数プログラミングへ
命令プログラミングから関数プログラミングへ命令プログラミングから関数プログラミングへ
命令プログラミングから関数プログラミングへ
 
第2回関数型言語勉強会 大阪
第2回関数型言語勉強会 大阪第2回関数型言語勉強会 大阪
第2回関数型言語勉強会 大阪
 
第一回関数型言語勉強会 大阪
第一回関数型言語勉強会 大阪第一回関数型言語勉強会 大阪
第一回関数型言語勉強会 大阪
 

Recently uploaded

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 

Recently uploaded (20)

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 

関数プログラミングことはじめ in 福岡