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
5.2k
Kotlin Nullable型をモナドっぽくしてみた
拡張関数を用いてNullable型を扱いやすくしてみました。
Taro Nagasawa
April 05, 2014
Tweet
Share
More Decks by Taro Nagasawa
See All by Taro Nagasawa
Android開発者のための Kotlin Multiplatform入門
ntaro
0
540
Kotlin 最新動向2022 #tfcon #techfeed
ntaro
1
2.2k
#Ubie 狂気の認知施策と選考設計
ntaro
13
13k
UbieにおけるサーバサイドKotlin活用事例
ntaro
1
1.1k
KotlinでSpring 完全理解ガイド #jsug
ntaro
6
3.4k
Kotlinでサーバサイドを始めよう!
ntaro
1
950
Androidからサーバーサイドまで!プログラミング言語 Kotlinの魅力 #devboost
ntaro
5
2.6k
Kotlin Contracts #m3kt
ntaro
4
4k
How_to_Test_Server-side_Kotlin.pdf
ntaro
1
470
Other Decks in Programming
See All in Programming
第3回関東Kaggler会_AtCoderはKaggleの役に立つ
chettub
3
1.2k
DevNexus - Create AI Infused Java Apps with LangChain4j
kdubois
0
120
良いコードレビューとは
danimal141
7
1.6k
ABEMA iOS 大規模プロジェクトにおける段階的な技術刷新 / ABEMA iOS Technology Upgrade
akkyie
1
220
CDK開発におけるコーディング規約の運用
yamanashi_ren01
2
260
React 19アップデートのために必要なこと
uhyo
8
1.5k
はじめての Go * WASM *OCR
sgash708
1
110
CDKを使ったPagerDuty連携インフラのテンプレート化
shibuya_shogo
0
110
Swift Testingのモチベを上げたい
stoticdev
2
140
color-scheme: light dark; を完全に理解する
uhyo
7
500
ファインディLT_ポケモン対戦の定量的分析
fufufukakaka
0
940
メンテが命: PHPフレームワークのコンテナ化とアップグレード戦略
shunta27
0
310
Featured
See All Featured
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
13
1k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
49k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.3k
Building a Scalable Design System with Sketch
lauravandoore
461
33k
The Power of CSS Pseudo Elements
geoffreycrofte
75
5.5k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
193
16k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
46
2.4k
Embracing the Ebb and Flow
colly
84
4.6k
Bootstrapping a Software Product
garrettdimon
PRO
307
110k
Building Applications with DynamoDB
mza
93
6.2k
A designer walks into a library…
pauljervisheath
205
24k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
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
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠