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.3k
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
690
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
990
Androidからサーバーサイドまで!プログラミング言語 Kotlinの魅力 #devboost
ntaro
5
2.7k
Kotlin Contracts #m3kt
ntaro
4
4.1k
How_to_Test_Server-side_Kotlin.pdf
ntaro
1
500
Other Decks in Programming
See All in Programming
PHPで始める振る舞い駆動開発(Behaviour-Driven Development)
ohmori_yusuke
2
350
PHPでWebSocketサーバーを実装しよう2025
kubotak
0
280
High-Level Programming Languages in AI Era -Human Thought and Mind-
hayat01sh1da
PRO
0
760
0626 Findy Product Manager LT Night_高田スライド_speaker deck用
mana_takada
0
160
レベル1の開発生産性向上に取り組む − 日々の作業の効率化・自動化を通じた改善活動
kesoji
0
120
Railsアプリケーションと パフォーマンスチューニング ー 秒間5万リクエストの モバイルオーダーシステムを支える事例 ー Rubyセミナー 大阪
falcon8823
5
1.1k
効率的な開発手段として VRTを活用する
ishkawa
0
130
Team operations that are not burdened by SRE
kazatohiei
1
310
ふつうの技術スタックでアート作品を作ってみる
akira888
1
490
Code as Context 〜 1にコードで 2にリンタ 34がなくて 5にルール? 〜
yodakeisuke
0
120
ペアプロ × 生成AI 現場での実践と課題について / generative-ai-in-pair-programming
codmoninc
1
16k
Quand Symfony, ApiPlatform, OpenAI et LangChain s'allient pour exploiter vos PDF : de la théorie à la production…
ahmedbhs123
0
150
Featured
See All Featured
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
107
19k
The Cost Of JavaScript in 2023
addyosmani
51
8.5k
A Tale of Four Properties
chriscoyier
160
23k
Measuring & Analyzing Core Web Vitals
bluesmoon
7
510
Building an army of robots
kneath
306
45k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
8
680
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
10
950
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
48
2.9k
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
20
1.3k
Making the Leap to Tech Lead
cromwellryan
134
9.4k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
26k
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
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠