SlideShare ist ein Scribd-Unternehmen logo
1 von 94
Downloaden Sie, um offline zu lesen
Friendly Functional
Programming
Wiem Zine Elabidine
@WiemZin
フレンドリーな関数型プログラミング
Scary Functional
Programming
Wiem Zine Elabidine
@WiemZin
Functor
Monad
怖い関数型プログラミング
Friendly Functional
Programming
Wiem Zine Elabidine
@WiemZin
フレンドリーな関数型プログラミング
Plan
Functional
Programming
Types and Functions Type classes
今日の予定、fp概要、型と関数 型クラスの流れで説明します
Paradigm
Is a style and a way of writing your programs.
Each programming language follow a programming
style.
パラダイムはプログラムを書く作法
Paradigm
Is a style and a way of writing your programs.
Each programming language follow a programming
style.
Scala combines
object-oriented and
functional programming.
Scalaは、オブジェクト指向プログラミングと関数型プログラミングを組み合わせたものです。
Daniel Sophie
Daniel
Sophie
Declarative
Imperative
How I can do it?
Declarative Mindset
What I want to do?
Imperative Mindset
宣言型の考え方と命令型の考え方
Functional programming
A form of declarative programming
関数型プログラミングは宣言型プログラミングの1形態
immutability
03
Data & Functions Pure functions
Composition
01 02
04
Functional programming
大まかに4つに分けて見ていきます
Pure functions
Total Deterministic Free of side
effects
純粋関数は決定的かつ副作用を持たない全域関数
Referential transparency
When the function could be replaced by the result of
its evaluation without affecting the meaning of the
program.
def sum(a: Int, b: Int): Int = a + b
sum(2, 3) == 5
参照透過性は関数をその評価結果と置き換えても プログラムの振る舞いが変わらないこと
● Testability
● Refactoring
● Maintainability
The benefits of
FP
FPの利点はテストのしやすさ メンテナビリティー
Types & Functions
Abstraction
型と関数について
“A data types or a simple type is an attribute of data which tells
the compiler or interpreter how the programmer intends to use
the data.” -- Wikipedia
Data Types
データ型はデータ使用の意図を表す
“A data types or a simple type is an attribute of data which tells
the compiler or interpreter how the programmer intends to use
the data.” -- Wikipedia
Data Types
If it compiles, it
works!
コンパイルすれば動作します!
“Is a type formed by combining other types.” -- Wikipedia
ADT (Algebraic Data Type)
代数的データ型は型の組み合わせ
Value types
Int
String
Double
Unit
Boolean ...
値型
Product types
Setting(true, true)
Setting(false, true)
Setting(false, true)
Setting(false, false)
case class Setting(
audioEnabled: Boolean,
videoEnabled: Boolean
)
直積型
Product types
Setting(true, true)
Setting(false, true)
Setting(false, true)
Setting(false, false)
2 x 2 = 4
4 possible values
case class Setting(
audioEnabled: Boolean,
videoEnabled: Boolean
)
直積型
Sum types
sealed trait Color
case object Purple extends Color
case object Red extends Color
case object Yellow extends Color
case object Green extends Color
?
?
?
?
val color: Color = ???
直和型
Sum types
sealed trait Color
case object Purple extends Color
case object Red extends Color
case object Yellow extends Color
case object Green extends Color
?
?
?
?
val color: Color = ???
4 possible values
直和型
case class Form(
topic: String,
bgcolor: Color
)
Geometry
ADT
?
?
?
?
val form: Form =
Form(topic = ???, color = ???)
∞
sealed trait Topic
case object Art extends Topic
case object History extends Topic
case object Philosophy extends Topic
case object Geography extends Topic
ADT
case class Form(
topic: Topic,
bgcolor: Color
)
Geometry
ADT
?
?
?
?
val form: Form =
Form(topic = ???, color = ???)
?
?
?
case class Form(
topic: Topic,
bgcolor: Color
)
Geometry
ADT
?
?
?
?
val form: Form =
Form(topic = ???, color = ???)
?
?
?
4 x 4 = 16
16 possible values
sealed trait Option[+A]
case object None extends Option[Nothing]
case class Some[A](a: A) extends Option[A]
Parameterized ADT
val choice: Option[Boolean] =
???
Some(true)
Some(false)
None
パラメータ化された代数的データ型
sealed trait Option[+A]
case object None extends Option[Nothing]
case class Some[A](a: A) extends Option[A]
Parameterized ADT
val choice: Option[Int] =
???
Some(1)
Some(157)
None
Some(2)
Some(3)
Some(4)
Some(5)
Some(6)
Some(7)
Some(8)
Some(158)
Some(159)
Some(845)
Some(880)
Some(8810)
Some(81945)
Some(819447)
Some(36841840)
パラメータ化された代数的データ型
FP is the practice of composing programs using pure functions.
Functions
は純粋関数を用いてプログラムを書くための方法
A function takes data as an input and returns a data as an
output
Functions
def isPositive(n: Int): Boolean = n > 0
関数はデータを受け取りデータを返す
A function could be defined:
● As a data type.
First class functions
type Predicate = Int => Boolean
関数そのものもデータ型
A function could be defined:
● As a data type.
● Using a literal syntax.
First class functions
(n: Int) => n > 0
関数リテラル構文を用いて定義できる
Higher order functions accept other functions as input and/or return
functions as output.
Higher-order functions
高階関数は別の関数を受け取るか 関数を戻り値として返す
Higher order functions accept other functions as input and/or return
functions as output.
Higher-order functions
def checkOption(option: Option[Int], p: Int => Boolean): Boolean =
option match {
case Some(v) if p(v) => true
case _ => false
}
Higher order functions accept other functions as input and/or return
functions as output.
Higher-order functions
def checkOption(option: Option[Int], p: Int => Boolean): Boolean =
option match {
case Some(v) if p(v) => true
case _ => false
}
val isPositive: Int => Boolean = n => n > 0
checkOption(Some(14), isPositive)
Higher order functions accept other functions as input and/or return
functions as output.
Higher-order functions
def checkOption(option: Option[Int], p: Int => Boolean): Boolean =
option match {
case Some(v) if p(v) => true
case _ => false
}
val isPositive: Int => Boolean = _ > 0
checkOption(Some(14), isPositive)
Higher order functions accept other functions as input and/or return
functions as output.
Higher-order functions
def checkOption(option: Option[Int], p: Int => Boolean): Boolean =
option match {
case Some(v) if p(v) => true
case _ => false
}
checkOption(Some(14), n => n > 0)
Higher order functions accept other functions as input and/or return
functions as output.
Higher-order functions
def checkOption(option: Option[Int], p: Int => Boolean): Boolean =
option match {
case Some(v) if p(v) => true
case _ => false
}
checkOption(Some(14), _ > 0)
Type classes
Add features to your types
型クラスは型に機能を与える
Type class
Is a way to define certain operations for a given type..
A type class instance for a given type provides an
implementation for these operations.
型に応じた演算を定義する方法 実装は特定の型クラスのインスタンスで定義される
Interface
trait Translator {
def translate(
source: String,
from: String,
to: String): String
}
翻訳インターフェイス
Interface
trait Translator {
def translate(
source: String,
from: String,
to: String): String
}
Feature
Interface
trait Translator {
def translate(
source: String,
from: String,
to: String): String
}
Language
EN JA
Interface
trait Translator {
def translate(
source: String,
from: Language,
to: Language): String
}
Audio
String
Document
WebPage
Discord
?
trait Translator[A] {
def translate(
source: A,
from: Language,
to: Language): A
}
パラメトリックなインターフェイス
Parametric Interface
Use Discord auto-translation bot
class DiscordTranslator extends Translator[DiscordMessage] {
override def translate(text: DiscordMessage,
from: Language, to: Language): DiscordMessage = ???
}
def program(t: DiscordTranslator) =
for {
message <- consumeMessages
(from, to) = detectLanguage(message)
newMsg = t.translate(message,from,to)
_ <- send(newMsg)
} yield ()
Discord の自動翻訳ボットの例
Translate a simple text
class StringTranslator extends Translator[String] {
override def translate(text: String,
from: Language, to: Language): String = ???
}
def program(t: StringTranslator) =
for {
message <- consumeMessages
(from, to) = detectLanguage(message)
newMsg = t.translate(message,from,to)
_ <- send(newMsg)
} yield ()
簡単なテキストの翻訳
What about Documents?
class DocumentTranslator extends Translator[Document] {
override def translate(text: Document,
from: Language, to: Language): Document = ???
}
def program(t: DocumentTranslator) =
for {
message <- consumeMessages
(from, to) = detectLanguage(message)
newMsg = t.translate(message,from,to)
_ <- send(newMsg)
} yield ()
Document ?
文書ファイルはどうしよう?
What about Audios?
class AudioTranslator extends Translator[Audio] {
override def translate(text: Audio,
from: Language, to: Language): Audio = ???
}
def program(t: AudioTranslator) =
for {
message <- consumeMessages
(from, to) = detectLanguage(message)
newMsg = t.translate(message,from,to)
_ <- send(newMsg)
} yield ()
Audio ?
音声はどうしよう?
Polymorphic program
def program[A](t: Translator[A]) =
for {
message <- consumeMessages
(from, to) = detectLanguage(message)
newMsg = t.translate(message,from,to)
_ <- send(newMsg)
} yield ()
多相的プログラム
Parametric Interface
trait Translator[A] {
def translate(
text: A,
from: Language,
to: Language): A
}
パラメトリックなインターフェイス
Type class
trait Translator[A] {
def translate(
text: A,
from: Language,
to: Language): A
}
object Translator {
def apply[A](implicit t: Translator[A]): Translator[A] = t
}
型クラス
Polymorphic program using context bounds
def program[A: Translator] =
for {
message <- consumeMessages
(from, to) = detectLanguage(message)
newMsg = Translator[A].translate(message,from,to)
_ <- send(newMsg)
} yield ()
型クラス
Polymorphic program using context bounds
def program[A: Translator] =
for {
message <- consumeMessages
(from, to) = detectLanguage(message)
newMsg = Translator[A].translate(message,from,to)
_ <- send(newMsg)
} yield ()
def program[A](implicit t: Translator[A])
構文を用いた多相プログラム
Type class instances
implicit val discordTranslator = new Translator[DiscordMessage] { … }
implicit val stringTranslator = new Translator[String] { … }
implicit val documentTranslator = new Translator[Document] { … }
implicit val audioTranslator = new Translator[Audio] { … }
program[DiscordMessage]
program[String]
program[Document]
program[Audio]
型クラスのインスタンス
Algebraic Data Structures
empty
combine
Monoid
combine
Semigroup
map
flatMap
Monad
map
Functor
代数的構造
Semigroup
trait Semigroup[A] {
def combine(a: A, b: A): A
}
object Semigroup {
def apply[A](implicit s: Semigroup[A]): Semigroup[A] = s
}
Semigroup (半群)
Semigroup
trait Semigroup[A] {
def combine(a: A, b: A): A
}
object Semigroup {
def apply[A](implicit s: Semigroup[A]): Semigroup[A] = s
}
Associativity
combine(combine(a, b), c)
==
combine(a, combine(b, c))
Laws
Semigroup則は結合律
Semigroup
trait Semigroup[A] {
def combine(a: A, b: A): A
}
object Semigroup {
def apply[A](implicit s: Semigroup[A]): Semigroup[A] = s
}
Associativity
(1 + 2) + 3
==
1 + (2 + 3)
Int (+)
Laws
Semigroup
trait Semigroup[A] {
def combine(a: A, b: A): A
}
object Semigroup {
def apply[A](implicit s: Semigroup[A]): Semigroup[A] = s
}
Associativity
(1 * 2) * 3
==
1 * (2 * 3)
Int (*)
Laws
Semigroup
trait Semigroup[A] {
def combine(a: A, b: A): A
}
object Semigroup {
def apply[A](implicit s: Semigroup[A]): Semigroup[A] = s
}
Associativity
Int (+, *)
String (concat)
Boolean(||, &&)
Laws
Semigroup
implicit val intSemigroup = new Semigroup[Int] { … }
implicit val stringSemigroup = new Semigroup[String] { … }
implicit val teamSemigroup = new Semigroup[Person] { … }
...
Associativity
Int (+, *)
String (concat)
Boolean(||, &&)
Laws
🐣
🐣
🐼
🐻🧍 🧍♀
🧍 🏻
♀
Monoid
trait Monoid[A] {
def empty: A
def combine(a: A, b: A): A
}
object Monoid {
def apply[A](implicit m: Monoid[A]): Monoid[A] = m
}
モノイド
Monoid
trait Monoid[A] {
def empty: A
def combine(a: A, b: A): A
}
object Monoid {
def apply[A](implicit m: Monoid[A]): Monoid[A] = m
}
Identity
combine(a, empty) == a
combine(empty, a ) == a
Associativity
combine(combine(a, b), c)
==
combine(a, combine(b, c)
Laws
モノイド則は単位元と結合律
Monoid
trait Monoid[A] {
def empty: A
def combine(a: A, b: A): A
}
object Monoid {
def apply[A](implicit m: Monoid[A]): Monoid[A] = m
}
Eg.
empty combinetype
Int 1 *
Int 0 +
String “” concat
Boolean true &&
Boolean false ||
Functor
trait Functor[F[_]] {
def map[A, B](f: A => B): F[A] => F[B]
}
object Functor {
def apply[F[_]](implicit f : Functor[F]) : Functor[F] = f
}
Higher kinded type
ファンクター
Functor
trait Functor[F[_]] {
def map[A, B](f: A => B): F[A] => F[B]
}
object Functor {
def apply[F[_]](implicit f : Functor[F]) : Functor[F] = f
}
Identity ?
composition ?
Laws
単位元? 合成律?
The identity function
def identity[A](a: A): A = a Identity
composition ?
Laws
恒等関数
The identity law
def identity[A](a: A): A = a
//REMINDER: def map[A, B](f: A => B): F[A] => F[B]
map(identity) == identity
Identity
composition ?
Laws
単位元
trait Function1[-B, +C] {
def apply(b: B): C
def compose[A](g: A => B): A => C = a => apply(g(a))
}
Identity
composition
Laws
The compose function
合成関数
trait Function1[-B, +C] {
def apply(b: B): C
def compose[A](g: A => B): A => C = a => apply(g(a))
}
f: A => B
g: B => C
h: A => C
h = a => g(f(a))
===
g compose f
f ° g
The compose function
trait Function1[-B, +C] {
def apply(b: B): C
def compose[A](g: A => B): A => C = a => apply(g(a))
}
//REMINDER: def map[A, B](f: A => B): F[A] => F[B]
map(g compose f) == map(g) compose map(f)
Laws
The composition law
Identity
composition
implicit val optionFunctor: Functor[Option] = new Functor[Option]
{
override def map[A, B](f: A => B): Option[A] => Option[B] = {
case None => None
case Some(a) => Some(f(a))
}
}
Example for a Functor instance
Functor[Option]
ファンクター・インスタンスの例
implicit val optionFunctor: Functor[Option] = new Functor[Option]
{
override def map[A, B](f: A => B): Option[A] => Option[B] = {
case None => None
case Some(a) => Some(f(a))
}
}
Identity law for:
Functor[Option]
f: identity
f: A => A
None => None
Some(a) => Some(a)
identity
Functor[Option] の単位元
implicit val optionFunctor: Functor[Option] = new Functor[Option]
{
override def map[A, B](f: A => B): Option[A] => Option[B] = {
case None => None
case Some(a) => Some(f(a))
}
}
Composition law for:
Functor[Option]
Identity Law
map(identity)(Some(1)) == Some(1)
map(identity)(None) == None
def increment(i: Int): Int
def toString(i: Int): String
map(toString compose increment)
==
map(toString) compose map(increment)
Some(41)
Functor[Option] の合成律
implicit val optionFunctor: Functor[Option] = new Functor[Option]
{
override def map[A, B](f: A => B): Option[A] => Option[B] = {
case None => None
case Some(a) => Some(f(a))
}
}
Composition law for:
Functor[Option]
Identity Law
map(identity)(Some(1)) == Some(1)
map(identity)(None) == None
def increment(i: Int): Int
def toString(i: Int): String
map(toString compose increment)
==
map(toString) compose map(increment)
Some(41)
Some(“42”)
implicit val optionFunctor: Functor[Option] = new Functor[Option]
{
override def map[A, B](f: A => B): Option[A] => Option[B] = {
case None => None
case Some(a) => Some(f(a))
}
}
Composition law for:
Functor[Option]
Identity Law
map(identity)(Some(1)) == Some(1)
map(identity)(None) == None
def increment(i: Int): Int
def toString(i: Int): String
map(toString compose increment)
==
map(toString) compose map(increment)
Some(41)
Some(“42”)
Some(“42”)
Monad
trait Monad[F[_]] {
def map[A, B](f: A => B): F[A] => F[B]
def flatMap[A, B](f: A => F[B]): F[A] => F[B]
}
object Monad {
def apply[F[_]](implicit m: Monad[F]) : Monad[F] = m
}
Higher kinded type
モナド
Monad
trait Monad[F[_]] {
def point[A](a: A): F[A]
def map[A, B](f: A => B): F[A] => F[B]
def flatMap[A, B](f: A => F[B]): F[A] => F[B]
}
object Monad {
def apply[F[_]](implicit m: Monad[F]) : Monad[F] = m
}
モナド
Monad
trait Monad[F[_]] {
def point[A](a: A): F[A]
def map[A, B](f: A => B): F[A] => F[B]
def flatMap[A, B](f: A => F[B]): F[A] => F[B]
}
object Monad {
def apply[F[_]](implicit m: Monad[F]) : Monad[F] = m
}
Left identity ?
Right identity
Associativity ?
Laws
左単位元 、右単位元 、結合律
Left identity
def increment(a: Int): Option[Int] = Some(a + 1)
Some(42).flatMap(a => increment(2))
==
increment(2)
Left identity
Right identity
Associativity ?
Laws
左単位元
Right identity
Some(42).flatMap(Some(_)) == Some(42)
Left identity
Right identity
Associativity ?
Laws
右単位元
Associativity
def increment(i: Int): Option[Int] = Some(i + 1)
def decrement(i: Int): Option[Int] = Some(i - 1)
Some(42).flatMap(increment).flatMap(decrement)
==
Some(42).flatMap(v => increment(v).flatMap(decrement))
Left identity
Right identity
Associativity
Laws
結合律
Monads in real life
IOモナドのコンテキストで演算する Haskeller
You can checkout the Scala functional programming
libraries:
● Cats
Github: https://github.com/typelevel/cats
doc: https://typelevel.org/cats
● Scalaz: https://github.com/scalaz/scalaz
● Zio Prelude: https://github.com/zio/zio-prelude
Talks by:
● John De Goes and Adam Fraser: “Reimagining
Functional Type Classes”
● Jakub Kozlowski: “Fantastic Monads and where to
find them”
Resources
次へのリソース
FP is
AWESOME
FP って最高
CREDITS: This presentation template was created by Slidesgo, including icons by
Flaticon, and infographics & images by Freepik
THANKS!
Please keep this slide for attribution
@WiemZin
ご清聴ありがとうございました

Weitere ähnliche Inhalte

Was ist angesagt?

Blazing Fast, Pure Effects without Monads — LambdaConf 2018
Blazing Fast, Pure Effects without Monads — LambdaConf 2018Blazing Fast, Pure Effects without Monads — LambdaConf 2018
Blazing Fast, Pure Effects without Monads — LambdaConf 2018John De Goes
 
An introduction to property based testing
An introduction to property based testingAn introduction to property based testing
An introduction to property based testingScott Wlaschin
 
Learning Functional Programming Without Growing a Neckbeard
Learning Functional Programming Without Growing a NeckbeardLearning Functional Programming Without Growing a Neckbeard
Learning Functional Programming Without Growing a NeckbeardKelsey Gilmore-Innis
 
Functional solid
Functional solidFunctional solid
Functional solidMatt Stine
 
Scalaz 8 vs Akka Actors
Scalaz 8 vs Akka ActorsScalaz 8 vs Akka Actors
Scalaz 8 vs Akka ActorsJohn De Goes
 
ZIO Schedule: Conquering Flakiness & Recurrence with Pure Functional Programming
ZIO Schedule: Conquering Flakiness & Recurrence with Pure Functional ProgrammingZIO Schedule: Conquering Flakiness & Recurrence with Pure Functional Programming
ZIO Schedule: Conquering Flakiness & Recurrence with Pure Functional ProgrammingJohn De Goes
 
One Monad to Rule Them All
One Monad to Rule Them AllOne Monad to Rule Them All
One Monad to Rule Them AllJohn De Goes
 
Implementing the IO Monad in Scala
Implementing the IO Monad in ScalaImplementing the IO Monad in Scala
Implementing the IO Monad in ScalaHermann Hueck
 
The lazy programmer's guide to writing thousands of tests
The lazy programmer's guide to writing thousands of testsThe lazy programmer's guide to writing thousands of tests
The lazy programmer's guide to writing thousands of testsScott Wlaschin
 
SeneJug java_8_prez_122015
SeneJug java_8_prez_122015SeneJug java_8_prez_122015
SeneJug java_8_prez_122015senejug
 
Twins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingTwins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingRichardWarburton
 
Dr Frankenfunctor and the Monadster
Dr Frankenfunctor and the MonadsterDr Frankenfunctor and the Monadster
Dr Frankenfunctor and the MonadsterScott Wlaschin
 
An Introduction to Property Based Testing
An Introduction to Property Based TestingAn Introduction to Property Based Testing
An Introduction to Property Based TestingC4Media
 
Functional Programming in JavaScript by Luis Atencio
Functional Programming in JavaScript by Luis AtencioFunctional Programming in JavaScript by Luis Atencio
Functional Programming in JavaScript by Luis AtencioLuis Atencio
 
Functor, Apply, Applicative And Monad
Functor, Apply, Applicative And MonadFunctor, Apply, Applicative And Monad
Functor, Apply, Applicative And MonadOliver Daff
 
Functions, Types, Programs and Effects
Functions, Types, Programs and EffectsFunctions, Types, Programs and Effects
Functions, Types, Programs and EffectsRaymond Roestenburg
 
Monad Transformers In The Wild
Monad Transformers In The WildMonad Transformers In The Wild
Monad Transformers In The WildStackMob Inc
 
Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)Scott Wlaschin
 

Was ist angesagt? (20)

Blazing Fast, Pure Effects without Monads — LambdaConf 2018
Blazing Fast, Pure Effects without Monads — LambdaConf 2018Blazing Fast, Pure Effects without Monads — LambdaConf 2018
Blazing Fast, Pure Effects without Monads — LambdaConf 2018
 
An introduction to property based testing
An introduction to property based testingAn introduction to property based testing
An introduction to property based testing
 
Learning Functional Programming Without Growing a Neckbeard
Learning Functional Programming Without Growing a NeckbeardLearning Functional Programming Without Growing a Neckbeard
Learning Functional Programming Without Growing a Neckbeard
 
Functional solid
Functional solidFunctional solid
Functional solid
 
Scalaz 8 vs Akka Actors
Scalaz 8 vs Akka ActorsScalaz 8 vs Akka Actors
Scalaz 8 vs Akka Actors
 
ZIO Schedule: Conquering Flakiness & Recurrence with Pure Functional Programming
ZIO Schedule: Conquering Flakiness & Recurrence with Pure Functional ProgrammingZIO Schedule: Conquering Flakiness & Recurrence with Pure Functional Programming
ZIO Schedule: Conquering Flakiness & Recurrence with Pure Functional Programming
 
One Monad to Rule Them All
One Monad to Rule Them AllOne Monad to Rule Them All
One Monad to Rule Them All
 
Implementing the IO Monad in Scala
Implementing the IO Monad in ScalaImplementing the IO Monad in Scala
Implementing the IO Monad in Scala
 
The lazy programmer's guide to writing thousands of tests
The lazy programmer's guide to writing thousands of testsThe lazy programmer's guide to writing thousands of tests
The lazy programmer's guide to writing thousands of tests
 
SeneJug java_8_prez_122015
SeneJug java_8_prez_122015SeneJug java_8_prez_122015
SeneJug java_8_prez_122015
 
Twins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingTwins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional Programming
 
Dr Frankenfunctor and the Monadster
Dr Frankenfunctor and the MonadsterDr Frankenfunctor and the Monadster
Dr Frankenfunctor and the Monadster
 
Nalinee java
Nalinee javaNalinee java
Nalinee java
 
An Introduction to Property Based Testing
An Introduction to Property Based TestingAn Introduction to Property Based Testing
An Introduction to Property Based Testing
 
Functional Programming in JavaScript by Luis Atencio
Functional Programming in JavaScript by Luis AtencioFunctional Programming in JavaScript by Luis Atencio
Functional Programming in JavaScript by Luis Atencio
 
Functor, Apply, Applicative And Monad
Functor, Apply, Applicative And MonadFunctor, Apply, Applicative And Monad
Functor, Apply, Applicative And Monad
 
Functions, Types, Programs and Effects
Functions, Types, Programs and EffectsFunctions, Types, Programs and Effects
Functions, Types, Programs and Effects
 
ppopoff
ppopoffppopoff
ppopoff
 
Monad Transformers In The Wild
Monad Transformers In The WildMonad Transformers In The Wild
Monad Transformers In The Wild
 
Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)
 

Ähnlich wie Friendly Functional Programming

Beauty and/or elegance in functional programming
Beauty and/or elegance in functional programmingBeauty and/or elegance in functional programming
Beauty and/or elegance in functional programmingSilviaP24
 
Functional programming with FSharp
Functional programming with FSharpFunctional programming with FSharp
Functional programming with FSharpDaniele Pozzobon
 
conceptsinobjectorientedprogramminglanguages-12659959597745-phpapp02.pdf
conceptsinobjectorientedprogramminglanguages-12659959597745-phpapp02.pdfconceptsinobjectorientedprogramminglanguages-12659959597745-phpapp02.pdf
conceptsinobjectorientedprogramminglanguages-12659959597745-phpapp02.pdfSahajShrimal1
 
Functions In Scala
Functions In Scala Functions In Scala
Functions In Scala Knoldus Inc.
 
Functional Programming In Java
Functional Programming In JavaFunctional Programming In Java
Functional Programming In JavaAndrei Solntsev
 
What's new in PHP 8.0?
What's new in PHP 8.0?What's new in PHP 8.0?
What's new in PHP 8.0?Nikita Popov
 
Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"Fwdays
 
Functions_21_22.pdf
Functions_21_22.pdfFunctions_21_22.pdf
Functions_21_22.pdfpaijitk
 
Scalapeno18 - Thinking Less with Scala
Scalapeno18 - Thinking Less with ScalaScalapeno18 - Thinking Less with Scala
Scalapeno18 - Thinking Less with ScalaDaniel Sebban
 
Intro f# functional_programming
Intro f# functional_programmingIntro f# functional_programming
Intro f# functional_programmingMauro Ghiani
 
Advanced Web Technology ass.pdf
Advanced Web Technology ass.pdfAdvanced Web Technology ass.pdf
Advanced Web Technology ass.pdfsimenehanmut
 
Programming Paradigms
Programming ParadigmsProgramming Paradigms
Programming ParadigmsDirecti Group
 
API first with Swagger and Scala by Slava Schmidt
API first with Swagger and Scala by  Slava SchmidtAPI first with Swagger and Scala by  Slava Schmidt
API first with Swagger and Scala by Slava SchmidtJavaDayUA
 
Writing DSL with Applicative Functors
Writing DSL with Applicative FunctorsWriting DSL with Applicative Functors
Writing DSL with Applicative FunctorsDavid Galichet
 

Ähnlich wie Friendly Functional Programming (20)

Beauty and/or elegance in functional programming
Beauty and/or elegance in functional programmingBeauty and/or elegance in functional programming
Beauty and/or elegance in functional programming
 
Functional programming with FSharp
Functional programming with FSharpFunctional programming with FSharp
Functional programming with FSharp
 
conceptsinobjectorientedprogramminglanguages-12659959597745-phpapp02.pdf
conceptsinobjectorientedprogramminglanguages-12659959597745-phpapp02.pdfconceptsinobjectorientedprogramminglanguages-12659959597745-phpapp02.pdf
conceptsinobjectorientedprogramminglanguages-12659959597745-phpapp02.pdf
 
Functions.pdf
Functions.pdfFunctions.pdf
Functions.pdf
 
Functionscs12 ppt.pdf
Functionscs12 ppt.pdfFunctionscs12 ppt.pdf
Functionscs12 ppt.pdf
 
Functions In Scala
Functions In Scala Functions In Scala
Functions In Scala
 
Functional programming
Functional programmingFunctional programming
Functional programming
 
Functional Programming In Java
Functional Programming In JavaFunctional Programming In Java
Functional Programming In Java
 
Python basic
Python basicPython basic
Python basic
 
What's new in PHP 8.0?
What's new in PHP 8.0?What's new in PHP 8.0?
What's new in PHP 8.0?
 
Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"
 
Functional programming in C++
Functional programming in C++Functional programming in C++
Functional programming in C++
 
Functions_21_22.pdf
Functions_21_22.pdfFunctions_21_22.pdf
Functions_21_22.pdf
 
Scalapeno18 - Thinking Less with Scala
Scalapeno18 - Thinking Less with ScalaScalapeno18 - Thinking Less with Scala
Scalapeno18 - Thinking Less with Scala
 
Intro f# functional_programming
Intro f# functional_programmingIntro f# functional_programming
Intro f# functional_programming
 
Advanced Web Technology ass.pdf
Advanced Web Technology ass.pdfAdvanced Web Technology ass.pdf
Advanced Web Technology ass.pdf
 
Scala best practices
Scala best practicesScala best practices
Scala best practices
 
Programming Paradigms
Programming ParadigmsProgramming Paradigms
Programming Paradigms
 
API first with Swagger and Scala by Slava Schmidt
API first with Swagger and Scala by  Slava SchmidtAPI first with Swagger and Scala by  Slava Schmidt
API first with Swagger and Scala by Slava Schmidt
 
Writing DSL with Applicative Functors
Writing DSL with Applicative FunctorsWriting DSL with Applicative Functors
Writing DSL with Applicative Functors
 

Kürzlich hochgeladen

Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 

Kürzlich hochgeladen (20)

Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 

Friendly Functional Programming

  • 1. Friendly Functional Programming Wiem Zine Elabidine @WiemZin フレンドリーな関数型プログラミング
  • 2. Scary Functional Programming Wiem Zine Elabidine @WiemZin Functor Monad 怖い関数型プログラミング
  • 3. Friendly Functional Programming Wiem Zine Elabidine @WiemZin フレンドリーな関数型プログラミング
  • 4. Plan Functional Programming Types and Functions Type classes 今日の予定、fp概要、型と関数 型クラスの流れで説明します
  • 5. Paradigm Is a style and a way of writing your programs. Each programming language follow a programming style. パラダイムはプログラムを書く作法
  • 6. Paradigm Is a style and a way of writing your programs. Each programming language follow a programming style. Scala combines object-oriented and functional programming. Scalaは、オブジェクト指向プログラミングと関数型プログラミングを組み合わせたものです。
  • 9.
  • 10.
  • 11.
  • 13.
  • 14.
  • 15. Declarative Imperative How I can do it? Declarative Mindset What I want to do? Imperative Mindset 宣言型の考え方と命令型の考え方
  • 16. Functional programming A form of declarative programming 関数型プログラミングは宣言型プログラミングの1形態
  • 17. immutability 03 Data & Functions Pure functions Composition 01 02 04 Functional programming 大まかに4つに分けて見ていきます
  • 18. Pure functions Total Deterministic Free of side effects 純粋関数は決定的かつ副作用を持たない全域関数
  • 19. Referential transparency When the function could be replaced by the result of its evaluation without affecting the meaning of the program. def sum(a: Int, b: Int): Int = a + b sum(2, 3) == 5 参照透過性は関数をその評価結果と置き換えても プログラムの振る舞いが変わらないこと
  • 20. ● Testability ● Refactoring ● Maintainability The benefits of FP FPの利点はテストのしやすさ メンテナビリティー
  • 22. “A data types or a simple type is an attribute of data which tells the compiler or interpreter how the programmer intends to use the data.” -- Wikipedia Data Types データ型はデータ使用の意図を表す
  • 23. “A data types or a simple type is an attribute of data which tells the compiler or interpreter how the programmer intends to use the data.” -- Wikipedia Data Types If it compiles, it works! コンパイルすれば動作します!
  • 24. “Is a type formed by combining other types.” -- Wikipedia ADT (Algebraic Data Type) 代数的データ型は型の組み合わせ
  • 26. Product types Setting(true, true) Setting(false, true) Setting(false, true) Setting(false, false) case class Setting( audioEnabled: Boolean, videoEnabled: Boolean ) 直積型
  • 27. Product types Setting(true, true) Setting(false, true) Setting(false, true) Setting(false, false) 2 x 2 = 4 4 possible values case class Setting( audioEnabled: Boolean, videoEnabled: Boolean ) 直積型
  • 28. Sum types sealed trait Color case object Purple extends Color case object Red extends Color case object Yellow extends Color case object Green extends Color ? ? ? ? val color: Color = ??? 直和型
  • 29. Sum types sealed trait Color case object Purple extends Color case object Red extends Color case object Yellow extends Color case object Green extends Color ? ? ? ? val color: Color = ??? 4 possible values 直和型
  • 30. case class Form( topic: String, bgcolor: Color ) Geometry ADT ? ? ? ? val form: Form = Form(topic = ???, color = ???) ∞
  • 31. sealed trait Topic case object Art extends Topic case object History extends Topic case object Philosophy extends Topic case object Geography extends Topic ADT
  • 32. case class Form( topic: Topic, bgcolor: Color ) Geometry ADT ? ? ? ? val form: Form = Form(topic = ???, color = ???) ? ? ?
  • 33. case class Form( topic: Topic, bgcolor: Color ) Geometry ADT ? ? ? ? val form: Form = Form(topic = ???, color = ???) ? ? ? 4 x 4 = 16 16 possible values
  • 34. sealed trait Option[+A] case object None extends Option[Nothing] case class Some[A](a: A) extends Option[A] Parameterized ADT val choice: Option[Boolean] = ??? Some(true) Some(false) None パラメータ化された代数的データ型
  • 35. sealed trait Option[+A] case object None extends Option[Nothing] case class Some[A](a: A) extends Option[A] Parameterized ADT val choice: Option[Int] = ??? Some(1) Some(157) None Some(2) Some(3) Some(4) Some(5) Some(6) Some(7) Some(8) Some(158) Some(159) Some(845) Some(880) Some(8810) Some(81945) Some(819447) Some(36841840) パラメータ化された代数的データ型
  • 36. FP is the practice of composing programs using pure functions. Functions は純粋関数を用いてプログラムを書くための方法
  • 37. A function takes data as an input and returns a data as an output Functions def isPositive(n: Int): Boolean = n > 0 関数はデータを受け取りデータを返す
  • 38. A function could be defined: ● As a data type. First class functions type Predicate = Int => Boolean 関数そのものもデータ型
  • 39. A function could be defined: ● As a data type. ● Using a literal syntax. First class functions (n: Int) => n > 0 関数リテラル構文を用いて定義できる
  • 40. Higher order functions accept other functions as input and/or return functions as output. Higher-order functions 高階関数は別の関数を受け取るか 関数を戻り値として返す
  • 41. Higher order functions accept other functions as input and/or return functions as output. Higher-order functions def checkOption(option: Option[Int], p: Int => Boolean): Boolean = option match { case Some(v) if p(v) => true case _ => false }
  • 42. Higher order functions accept other functions as input and/or return functions as output. Higher-order functions def checkOption(option: Option[Int], p: Int => Boolean): Boolean = option match { case Some(v) if p(v) => true case _ => false } val isPositive: Int => Boolean = n => n > 0 checkOption(Some(14), isPositive)
  • 43. Higher order functions accept other functions as input and/or return functions as output. Higher-order functions def checkOption(option: Option[Int], p: Int => Boolean): Boolean = option match { case Some(v) if p(v) => true case _ => false } val isPositive: Int => Boolean = _ > 0 checkOption(Some(14), isPositive)
  • 44. Higher order functions accept other functions as input and/or return functions as output. Higher-order functions def checkOption(option: Option[Int], p: Int => Boolean): Boolean = option match { case Some(v) if p(v) => true case _ => false } checkOption(Some(14), n => n > 0)
  • 45. Higher order functions accept other functions as input and/or return functions as output. Higher-order functions def checkOption(option: Option[Int], p: Int => Boolean): Boolean = option match { case Some(v) if p(v) => true case _ => false } checkOption(Some(14), _ > 0)
  • 46. Type classes Add features to your types 型クラスは型に機能を与える
  • 47. Type class Is a way to define certain operations for a given type.. A type class instance for a given type provides an implementation for these operations. 型に応じた演算を定義する方法 実装は特定の型クラスのインスタンスで定義される
  • 48. Interface trait Translator { def translate( source: String, from: String, to: String): String } 翻訳インターフェイス
  • 49. Interface trait Translator { def translate( source: String, from: String, to: String): String } Feature
  • 50. Interface trait Translator { def translate( source: String, from: String, to: String): String } Language EN JA
  • 51. Interface trait Translator { def translate( source: String, from: Language, to: Language): String } Audio String Document WebPage Discord ?
  • 52. trait Translator[A] { def translate( source: A, from: Language, to: Language): A } パラメトリックなインターフェイス Parametric Interface
  • 53. Use Discord auto-translation bot class DiscordTranslator extends Translator[DiscordMessage] { override def translate(text: DiscordMessage, from: Language, to: Language): DiscordMessage = ??? } def program(t: DiscordTranslator) = for { message <- consumeMessages (from, to) = detectLanguage(message) newMsg = t.translate(message,from,to) _ <- send(newMsg) } yield () Discord の自動翻訳ボットの例
  • 54. Translate a simple text class StringTranslator extends Translator[String] { override def translate(text: String, from: Language, to: Language): String = ??? } def program(t: StringTranslator) = for { message <- consumeMessages (from, to) = detectLanguage(message) newMsg = t.translate(message,from,to) _ <- send(newMsg) } yield () 簡単なテキストの翻訳
  • 55. What about Documents? class DocumentTranslator extends Translator[Document] { override def translate(text: Document, from: Language, to: Language): Document = ??? } def program(t: DocumentTranslator) = for { message <- consumeMessages (from, to) = detectLanguage(message) newMsg = t.translate(message,from,to) _ <- send(newMsg) } yield () Document ? 文書ファイルはどうしよう?
  • 56. What about Audios? class AudioTranslator extends Translator[Audio] { override def translate(text: Audio, from: Language, to: Language): Audio = ??? } def program(t: AudioTranslator) = for { message <- consumeMessages (from, to) = detectLanguage(message) newMsg = t.translate(message,from,to) _ <- send(newMsg) } yield () Audio ? 音声はどうしよう?
  • 57. Polymorphic program def program[A](t: Translator[A]) = for { message <- consumeMessages (from, to) = detectLanguage(message) newMsg = t.translate(message,from,to) _ <- send(newMsg) } yield () 多相的プログラム
  • 58. Parametric Interface trait Translator[A] { def translate( text: A, from: Language, to: Language): A } パラメトリックなインターフェイス
  • 59. Type class trait Translator[A] { def translate( text: A, from: Language, to: Language): A } object Translator { def apply[A](implicit t: Translator[A]): Translator[A] = t } 型クラス
  • 60. Polymorphic program using context bounds def program[A: Translator] = for { message <- consumeMessages (from, to) = detectLanguage(message) newMsg = Translator[A].translate(message,from,to) _ <- send(newMsg) } yield () 型クラス
  • 61. Polymorphic program using context bounds def program[A: Translator] = for { message <- consumeMessages (from, to) = detectLanguage(message) newMsg = Translator[A].translate(message,from,to) _ <- send(newMsg) } yield () def program[A](implicit t: Translator[A]) 構文を用いた多相プログラム
  • 62. Type class instances implicit val discordTranslator = new Translator[DiscordMessage] { … } implicit val stringTranslator = new Translator[String] { … } implicit val documentTranslator = new Translator[Document] { … } implicit val audioTranslator = new Translator[Audio] { … } program[DiscordMessage] program[String] program[Document] program[Audio] 型クラスのインスタンス
  • 64. Semigroup trait Semigroup[A] { def combine(a: A, b: A): A } object Semigroup { def apply[A](implicit s: Semigroup[A]): Semigroup[A] = s } Semigroup (半群)
  • 65. Semigroup trait Semigroup[A] { def combine(a: A, b: A): A } object Semigroup { def apply[A](implicit s: Semigroup[A]): Semigroup[A] = s } Associativity combine(combine(a, b), c) == combine(a, combine(b, c)) Laws Semigroup則は結合律
  • 66. Semigroup trait Semigroup[A] { def combine(a: A, b: A): A } object Semigroup { def apply[A](implicit s: Semigroup[A]): Semigroup[A] = s } Associativity (1 + 2) + 3 == 1 + (2 + 3) Int (+) Laws
  • 67. Semigroup trait Semigroup[A] { def combine(a: A, b: A): A } object Semigroup { def apply[A](implicit s: Semigroup[A]): Semigroup[A] = s } Associativity (1 * 2) * 3 == 1 * (2 * 3) Int (*) Laws
  • 68. Semigroup trait Semigroup[A] { def combine(a: A, b: A): A } object Semigroup { def apply[A](implicit s: Semigroup[A]): Semigroup[A] = s } Associativity Int (+, *) String (concat) Boolean(||, &&) Laws
  • 69. Semigroup implicit val intSemigroup = new Semigroup[Int] { … } implicit val stringSemigroup = new Semigroup[String] { … } implicit val teamSemigroup = new Semigroup[Person] { … } ... Associativity Int (+, *) String (concat) Boolean(||, &&) Laws 🐣 🐣 🐼 🐻🧍 🧍♀ 🧍 🏻 ♀
  • 70. Monoid trait Monoid[A] { def empty: A def combine(a: A, b: A): A } object Monoid { def apply[A](implicit m: Monoid[A]): Monoid[A] = m } モノイド
  • 71. Monoid trait Monoid[A] { def empty: A def combine(a: A, b: A): A } object Monoid { def apply[A](implicit m: Monoid[A]): Monoid[A] = m } Identity combine(a, empty) == a combine(empty, a ) == a Associativity combine(combine(a, b), c) == combine(a, combine(b, c) Laws モノイド則は単位元と結合律
  • 72. Monoid trait Monoid[A] { def empty: A def combine(a: A, b: A): A } object Monoid { def apply[A](implicit m: Monoid[A]): Monoid[A] = m } Eg. empty combinetype Int 1 * Int 0 + String “” concat Boolean true && Boolean false ||
  • 73. Functor trait Functor[F[_]] { def map[A, B](f: A => B): F[A] => F[B] } object Functor { def apply[F[_]](implicit f : Functor[F]) : Functor[F] = f } Higher kinded type ファンクター
  • 74. Functor trait Functor[F[_]] { def map[A, B](f: A => B): F[A] => F[B] } object Functor { def apply[F[_]](implicit f : Functor[F]) : Functor[F] = f } Identity ? composition ? Laws 単位元? 合成律?
  • 75. The identity function def identity[A](a: A): A = a Identity composition ? Laws 恒等関数
  • 76. The identity law def identity[A](a: A): A = a //REMINDER: def map[A, B](f: A => B): F[A] => F[B] map(identity) == identity Identity composition ? Laws 単位元
  • 77. trait Function1[-B, +C] { def apply(b: B): C def compose[A](g: A => B): A => C = a => apply(g(a)) } Identity composition Laws The compose function 合成関数
  • 78. trait Function1[-B, +C] { def apply(b: B): C def compose[A](g: A => B): A => C = a => apply(g(a)) } f: A => B g: B => C h: A => C h = a => g(f(a)) === g compose f f ° g The compose function
  • 79. trait Function1[-B, +C] { def apply(b: B): C def compose[A](g: A => B): A => C = a => apply(g(a)) } //REMINDER: def map[A, B](f: A => B): F[A] => F[B] map(g compose f) == map(g) compose map(f) Laws The composition law Identity composition
  • 80. implicit val optionFunctor: Functor[Option] = new Functor[Option] { override def map[A, B](f: A => B): Option[A] => Option[B] = { case None => None case Some(a) => Some(f(a)) } } Example for a Functor instance Functor[Option] ファンクター・インスタンスの例
  • 81. implicit val optionFunctor: Functor[Option] = new Functor[Option] { override def map[A, B](f: A => B): Option[A] => Option[B] = { case None => None case Some(a) => Some(f(a)) } } Identity law for: Functor[Option] f: identity f: A => A None => None Some(a) => Some(a) identity Functor[Option] の単位元
  • 82. implicit val optionFunctor: Functor[Option] = new Functor[Option] { override def map[A, B](f: A => B): Option[A] => Option[B] = { case None => None case Some(a) => Some(f(a)) } } Composition law for: Functor[Option] Identity Law map(identity)(Some(1)) == Some(1) map(identity)(None) == None def increment(i: Int): Int def toString(i: Int): String map(toString compose increment) == map(toString) compose map(increment) Some(41) Functor[Option] の合成律
  • 83. implicit val optionFunctor: Functor[Option] = new Functor[Option] { override def map[A, B](f: A => B): Option[A] => Option[B] = { case None => None case Some(a) => Some(f(a)) } } Composition law for: Functor[Option] Identity Law map(identity)(Some(1)) == Some(1) map(identity)(None) == None def increment(i: Int): Int def toString(i: Int): String map(toString compose increment) == map(toString) compose map(increment) Some(41) Some(“42”)
  • 84. implicit val optionFunctor: Functor[Option] = new Functor[Option] { override def map[A, B](f: A => B): Option[A] => Option[B] = { case None => None case Some(a) => Some(f(a)) } } Composition law for: Functor[Option] Identity Law map(identity)(Some(1)) == Some(1) map(identity)(None) == None def increment(i: Int): Int def toString(i: Int): String map(toString compose increment) == map(toString) compose map(increment) Some(41) Some(“42”) Some(“42”)
  • 85. Monad trait Monad[F[_]] { def map[A, B](f: A => B): F[A] => F[B] def flatMap[A, B](f: A => F[B]): F[A] => F[B] } object Monad { def apply[F[_]](implicit m: Monad[F]) : Monad[F] = m } Higher kinded type モナド
  • 86. Monad trait Monad[F[_]] { def point[A](a: A): F[A] def map[A, B](f: A => B): F[A] => F[B] def flatMap[A, B](f: A => F[B]): F[A] => F[B] } object Monad { def apply[F[_]](implicit m: Monad[F]) : Monad[F] = m } モナド
  • 87. Monad trait Monad[F[_]] { def point[A](a: A): F[A] def map[A, B](f: A => B): F[A] => F[B] def flatMap[A, B](f: A => F[B]): F[A] => F[B] } object Monad { def apply[F[_]](implicit m: Monad[F]) : Monad[F] = m } Left identity ? Right identity Associativity ? Laws 左単位元 、右単位元 、結合律
  • 88. Left identity def increment(a: Int): Option[Int] = Some(a + 1) Some(42).flatMap(a => increment(2)) == increment(2) Left identity Right identity Associativity ? Laws 左単位元
  • 89. Right identity Some(42).flatMap(Some(_)) == Some(42) Left identity Right identity Associativity ? Laws 右単位元
  • 90. Associativity def increment(i: Int): Option[Int] = Some(i + 1) def decrement(i: Int): Option[Int] = Some(i - 1) Some(42).flatMap(increment).flatMap(decrement) == Some(42).flatMap(v => increment(v).flatMap(decrement)) Left identity Right identity Associativity Laws 結合律
  • 91. Monads in real life IOモナドのコンテキストで演算する Haskeller
  • 92. You can checkout the Scala functional programming libraries: ● Cats Github: https://github.com/typelevel/cats doc: https://typelevel.org/cats ● Scalaz: https://github.com/scalaz/scalaz ● Zio Prelude: https://github.com/zio/zio-prelude Talks by: ● John De Goes and Adam Fraser: “Reimagining Functional Type Classes” ● Jakub Kozlowski: “Fantastic Monads and where to find them” Resources 次へのリソース
  • 94. CREDITS: This presentation template was created by Slidesgo, including icons by Flaticon, and infographics & images by Freepik THANKS! Please keep this slide for attribution @WiemZin ご清聴ありがとうございました