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

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
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
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
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
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 

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