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の関数参照 #Kotlin_Sansan
Search
Taro Nagasawa
April 07, 2017
Programming
1
690
Kotlinの関数参照 #Kotlin_Sansan
Qiita記事をそのままスライド化。
http://qiita.com/ngsw_taro/items/f2f70296828ee75491a2
Taro Nagasawa
April 07, 2017
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
840
Androidからサーバーサイドまで!プログラミング言語 Kotlinの魅力 #devboost
ntaro
5
2.3k
Kotlin Contracts #m3kt
ntaro
4
3.4k
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
Rubyでたのしむクリエイティブコーディング/Enjoy Creative coding with Ruby
chobishiba
1
180
Ruby Function Composition
bkuhlmann
1
330
PHPはいつから死んでいるかの調査
chiroruxx
1
380
try!Swift Tokyo 2024 参加報告 LT
akidon0000
1
220
Designing for tomorrow's programming workflows
honnibal
PRO
2
120
What We Can Learn From OSS
inouehi
0
420
Behind VS Code Extensions for JavaScript / TypeScript Linnting and Formatting
unvalley
5
890
Node.js v22 で変わること
yosuke_furukawa
PRO
4
1.2k
Azure OpenAI Serviceのプロンプトエンジニアリング入門
tomokusaba
3
670
TYPO3 v13 – The road to LTS: What's new and new APIs
luisasofie_xoxo
0
200
Semantic search with Django and pgvector
pauloxnet
0
240
Changed Rules: Architectures with Lightweight Stores
manfredsteyer
PRO
0
240
Featured
See All Featured
GraphQLとの向き合い方2022年版
quramy
32
12k
The Power of CSS Pseudo Elements
geoffreycrofte
60
5k
Designing Experiences People Love
moore
136
23k
Fantastic passwords and where to find them - at NoRuKo
philnash
37
2.5k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
9
8.3k
Pencils Down: Stop Designing & Start Developing
hursman
117
11k
Build your cross-platform service in a week with App Engine
jlugia
225
17k
Mobile First: as difficult as doing things right
swwweet
216
8.6k
No one is an island. Learnings from fostering a developers community.
thoeni
16
2.1k
Faster Mobile Websites
deanohume
299
30k
Building Flexible Design Systems
yeseniaperezcruz
319
37k
YesSQL, Process and Tooling at Scale
rocio
164
13k
Transcript
Kotlinの関数参照 2017-04-04 Kotlin勉強会@Sansan 長澤 太郎
自己紹介 • たろう @ngsw_taro • エムスリー株式会社のAndroidエンジニア • Kotlinエバンジェリスト(自称) • 著書「Kotlinスタートブック」
もくじ 1. 関数参照 2. レシーバが未定のメソッド参照 3. レシーバが確定のメソッド参照 4. プロパティも関数
1. 関数参照
関数オブジェクト • Kotlinでは定義済みの関数のオブジェクトを得ること ができる • 関数オブジェクトを変数に代入したり、別の関数の引 数に渡したり、持ち回して便利に使える。 • 必ずしもオブジェクトではない(ヒント: インライン関
数)
コード例 // 普通の関数 fun succ(n: Int): Int = n +
1 // 関数オブジェクトを取得し、変数に代入 val myFunc: (Int)->Int = ::succ // ^^^^^^^^^^ 関数型 // メソッド呼び出しによる元の関数の計算を実行 myFunc.invoke(5) //=> 6 // 演算子オーバロードによる関数らしい記述 myFunc(7) //=> 8
応用例 // 高階関数の引数に「関数参照」を渡す listOf(1, 2, 3).map(::succ) //=> [2, 3, 4]
// ラムダ式を使えば同じ listOf(1, 2, 3).map { succ(it) } //=> [2, 3, 4] • ラムダ式は、文を手続き的に記述していく • 関数参照は、宣言的な記述になる
2. レシーバが未定のメソッド参照
メソッド参照 • メソッドのオブジェクトも取得できる • この場合は、レシーバが未定 // 「型::メソッド名」という書式で取得 val foo: String.()->String
= String::reversed // ^^^^^^^^^^^^^^^^^ 関数型 // メソッドっぽく呼び出せる "Kotlin".foo() //=> niltoK
拡張関数もOK fun String.bold(): String = "**$this**" val baz: String.()->String =
String::bold "Sansan".baz() //=> **Sansan**
普通の関数型にもなるよ • A.(B)->Cという型は、(A, B)->Cと同じっぽい fun String.bold(): String = "**$this**" //
型どっちでもOK val aaa: String.()->String = String::bold val bbb: (String)->String = aaa // 呼び出し方どっちでもOK "hoge".aaa() //=> **hoge** aaa("fuga") //=> **fuga**
高階関数で効果を発揮 "Hello".let { it.bold() } "Hello".let(String::bold) もしString.()->Stringが(String)->Stringと みなせなかったら、2行目の書き方がNGです。
3. レシーバが確定のメソッド参照
Bound Function Reference • bound = 束縛された → レシーバが確定してるメソッド参照 •
Kotlin 1.1から使用できる
コード例 // 「オブジェクト::メソッド名」の書式で取得 val succ: (Int)->Int = 1::plus succ(5) //=>
6 userRepository.findById(123L) ?.let { user -> user.copy(blocked = true) } ?.let(userRepository::update)
4. プロパティも関数
プロパティも関数として扱える val prop: KProperty1<String, Int> = String::length prop.get("Taro") //=> 4
val func: (String) -> Int = prop func("Taro") //=> 4
高階関数で効果を発揮 listOf("foo", "wa").map { it.length } // => [3, 2]
listOf("foo", "wa").map(String::length) // => [3, 2] もしKProperty1<String, Int>が (String)->Stringとみなせなかったら、2行目の書き 方がNGです。
まとめ • 関数オブジェクトを使うと宣言的な記述ができる • ::hoge: 関数参照 • Foo::bar: メソッド参照 •
foo::bar: レシーバ固定メソッド参照 (Bound Function Reference) • プロパティも関数オブジェクトになれる