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
530
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
3.9k
How_to_Test_Server-side_Kotlin.pdf
ntaro
1
470
Other Decks in Programming
See All in Programming
お前もAI鬼にならないか?👹Bolt & Cursor & Supabase & Vercelで人間をやめるぞ、ジョジョー!👺
taishiyade
7
4.1k
PHP ステートレス VS ステートフル 状態管理と並行性 / php-stateless-stateful
ytake
0
110
もう少しテストを書きたいんじゃ〜 #phpstudy
o0h
PRO
2
100
プログラミング言語学習のススメ / why-do-i-learn-programming-language
yashi8484
0
150
PHPのバージョンアップ時にも役立ったAST
matsuo_atsushi
0
210
color-scheme: light dark; を完全に理解する
uhyo
7
480
Rubyで始める関数型ドメインモデリング
shogo_tksk
0
130
Rails アプリ地図考 Flush Cut
makicamel
1
130
Better Code Design in PHP
afilina
0
150
ARA Ansible for the teams
kksat
0
170
GAEログのコスト削減
mot_techtalk
0
120
Datadog Workflow Automation で圧倒的価値提供
showwin
1
100
Featured
See All Featured
Understanding Cognitive Biases in Performance Measurement
bluesmoon
27
1.6k
Building Adaptive Systems
keathley
40
2.4k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
4
430
What's in a price? How to price your products and services
michaelherold
244
12k
Building an army of robots
kneath
303
45k
Git: the NoSQL Database
bkeepers
PRO
427
65k
GraphQLとの向き合い方2022年版
quramy
44
13k
Designing for Performance
lara
604
68k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
49
2.3k
Java REST API Framework Comparison - PWX 2021
mraible
29
8.4k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
100
18k
How STYLIGHT went responsive
nonsquared
98
5.4k
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
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠