Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Kotlin Nullable型をモナドっぽくしてみた
Search
Taro Nagasawa
April 05, 2014
Programming
0
5k
Kotlin Nullable型をモナドっぽくしてみた
拡張関数を用いてNullable型を扱いやすくしてみました。
Taro Nagasawa
April 05, 2014
Tweet
Share
More Decks by Taro Nagasawa
See All by Taro Nagasawa
Kotlin 最新動向2022 #tfcon #techfeed
ntaro
1
2k
#Ubie 狂気の認知施策と選考設計
ntaro
13
12k
UbieにおけるサーバサイドKotlin活用事例
ntaro
1
1k
KotlinでSpring 完全理解ガイド #jsug
ntaro
6
3.2k
Kotlinでサーバサイドを始めよう!
ntaro
1
850
Androidからサーバーサイドまで!プログラミング言語 Kotlinの魅力 #devboost
ntaro
5
2.3k
Kotlin Contracts #m3kt
ntaro
4
3.5k
How_to_Test_Server-side_Kotlin.pdf
ntaro
1
390
Kotlin Fest 2018 - Opening session
ntaro
0
4.2k
Other Decks in Programming
See All in Programming
見た目から始める生産性向上
ikumatadokoro
7
840
MetricKitで予期せぬ終了を検知する話 / Detect unexpected termination with MetricKit
nekowen
1
190
Komplexe Oberflächen mit SVG und der Web Animation API
joergneumann
0
670
CA.swift19 恋するAIアプリ開発の裏側
oskmr
0
360
Micro Frontends for Java Microservices - Devnexus 2024
mraible
PRO
0
490
Changed Rules: Architectures with Lightweight Stores
manfredsteyer
PRO
0
240
PHPの次期バージョンはこの時期どうなっているのか - Internalsの開発体制について - PHPカンファレンス小田原
youkidearitai
PRO
1
190
新宿ダンジョンを可視化してみた
satoshi7190
2
250
GitHub Copilotのススメ
marcy731
1
200
『Railsオワコン』と言われる時代に、なぜブルーモ証券はRailsを選ぶのか
free_world21
0
190
Ruby Pattern Matching
bkuhlmann
0
930
Blue/Greenデプロイの導入による 運用フローの改善
kudoas
1
370
Featured
See All Featured
In The Pink: A Labor of Love
frogandcode
138
21k
Teambox: Starting and Learning
jrom
128
8.4k
Into the Great Unknown - MozCon
thekraken
10
990
How to Ace a Technical Interview
jacobian
272
22k
Music & Morning Musume
bryan
41
5.6k
No one is an island. Learnings from fostering a developers community.
thoeni
16
2.1k
Building a Modern Day E-commerce SEO Strategy
aleyda
17
6.4k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
244
20k
Designing on Purpose - Digital PM Summit 2013
jponch
110
6.5k
VelocityConf: Rendering Performance Case Studies
addyosmani
320
23k
Six Lessons from altMBA
skipperchong
21
3k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
187
16k
Transcript
Kotlin NullableܕΛϞφυͬΆͯ͘͠Έͨ 2014-04-05 ୈޒ.ޒճ #ौ୩java ᖒ ଠ @ngsw_taro
ॕʂJava8
Optional • ʮܭࢉͷࣦഊʯʮͷෆࡏʯͷՄೳੑΛදݱ͢Δ • nullͷΘΓ • (্ख͑͘) ͵ΔΆΒͳ͍
Optional static Optional<Integer> toInt(String str) {! ! // Integerʹมͯ͠ฦ͢! }!
————————————————————————————————————————————————! Optional<String> a = getA();! Optional<Integer> b = ! a.flatMap(Sample::toInt);
NPEͷढറ͔ΒಀΕΒΕͳ͍ Optional<String> str = null;
KotlinͷΞϓϩʔν (ࢿྉ) KotlinͷNull-Safetyػߏʹ͍ͭͯ https://speakerdeck.com/ntaro/kotlin-null-safetyji-gou-nituite
Nullableܕ≠Ϟφυ • NullableܕมnullΛೖՄೳͳ͚ͩ • ͑Δϝιου͕૿͑ΔΘ͚Ͱͳ͍ • NullableܕͷNullableܕͱ͔ೖΕࢠͰ͖ͳ͍
nullνΣοΫͯ͠߹͚ fun toInt(str: String): Int? = ...! ——————————————————————————————————————————————! val a:
String? = getA()! val b: Int? = if(a != null) toInt(a) else null
NullableܕΛϞφυͬΆͯ͘͠Έͨ
͜ΕͰ0, fun <T,R> T.bind(f:(T)->R):R = f(this)
NotNull→NullableͷؔΛద༻Մ fun divide(a: Int, b: Int): Int?! ! ! !
! ! ! = if(b == 0) null else (a / b)! val partial = (::divide).curried()(100)! ! 2.nullable()?.bind(partial) // => 50! 0.nullable()?.bind(partial) // => null! null?.bind(partial) // => null
NotNull→NotNullͷؔΛద༻Մ fun square(i: Int): Int = i * i! !
4.nullable()?.bind(::square) // => 16! null?.bind(::square) // => null
if-else ΛഉআͰ͖ͨʂ // before! val b: Int? = if(a !=
null) toInt(a) else null! ! // after! val b: Int? = a?.bind(::toInt)
Nullableͳؔμϝ fun next(i: Int) = i + 1! val nullableNext
= ::next.nullable()! ! val a: Int? = 5! nullableNext?.invoke(a) // ܕ͕߹Θͳ͍! a?.bind(nullableNext) // ܕ͕߹Θͳ͍
վྑ fun <T, R> T.bind(f: ((T) -> R)?): R?! =
f?.invoke(this)
Nullableͳؔద༻Մ fun next(i: Int) = i + 1! val nullableNext
= ::next.nullable()! ! val a: Int? = 5! a?.bind(nullableNext) // => 6! a?.bind(null) // => null
Applicative Functor fun minus(a: Int, b: Int) = a –
b! val minus = ::minus.curried()! ! val a: Int? = 5! val b: Int? = 3! b?.bind(a?.bind(minus)) // => 2
͜ΜͳؔΛಋೖͯ͠ΈΔ fun <T, R>! Function1<T, R>.apply(nullable: T?): R?! ! !
= nullable?.bind(this)
ΑΓ3FBEBCMFͳελΠϧ // before! b?.bind(a?.bind(minus))! ! // after! minus.apply(a)?.apply(b)
Ռ Ҿ͕NotNullͷؔʹNullableΛ ৯ΘͤΒΕΔΑ͏ʹͳͬͨ ! ! ʮNullνΣοΫͯ͠ذʯΛഉআ͠ɺ ಡΈ͍͢ίʔυ͕ॻ͚Δ
NullableɺͦΜͳʹ໘͡Όͳ͍ʂ
curried() FunKTionale https://github.com/MarioAriasC/funKTionale
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠