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
680
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
980
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
プロダクト志向なエンジニアがもう一歩先の価値を目指すために意識したこと
nealle
0
100
ruby.wasmで多人数リアルタイム通信ゲームを作ろう
lnit
1
120
生成AIコーディングとの向き合い方、AIと共創するという考え方 / How to deal with generative AI coding and the concept of co-creating with AI
seike460
PRO
1
330
関数型まつりレポート for JuliaTokai #22
antimon2
0
140
データの民主化を支える、透明性のあるデータ利活用への挑戦 2025-06-25 Database Engineering Meetup#7
y_ken
0
300
PHP 8.4の新機能「プロパティフック」から学ぶオブジェクト指向設計とリスコフの置換原則
kentaroutakeda
2
430
Result型で“失敗”を型にするPHPコードの書き方
kajitack
4
200
エンジニア向け採用ピッチ資料
inusan
0
160
Bytecode Manipulation 으로 생산성 높이기
bigstark
2
370
Beyond Portability: Live Migration for Evolving WebAssembly Workloads
chikuwait
0
390
GoのGenericsによるslice操作との付き合い方
syumai
3
680
Deep Dive into ~/.claude/projects
hiragram
5
460
Featured
See All Featured
The Language of Interfaces
destraynor
158
25k
Large-scale JavaScript Application Architecture
addyosmani
512
110k
[RailsConf 2023] Rails as a piece of cake
palkan
55
5.6k
jQuery: Nuts, Bolts and Bling
dougneiner
63
7.8k
Build your cross-platform service in a week with App Engine
jlugia
231
18k
Designing for humans not robots
tammielis
253
25k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
53
2.8k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.3k
Agile that works and the tools we love
rasmusluckow
329
21k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
26k
Six Lessons from altMBA
skipperchong
28
3.8k
Building a Modern Day E-commerce SEO Strategy
aleyda
41
7.3k
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
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠