当前位置:网站首页>Kotlin basic learning 16
Kotlin basic learning 16
2022-07-02 03:24:00 【Never stop learning】
Catalog
1.Kotlin Nullable type extension function of language
2.Kotlin Linguistic infix keyword
3.Kotlin Definition extension file of language
4.Kotlin Language rename extended learning
6.Kotlin The transformation function of language map
7.Kotlin The transformation function of language -flatmap
1.Kotlin Nullable type extension function of language
// Yes String?== Nullable type Function extension , And there are backup values
fun String?.outputStringValueFun(defaultValue:String) = println(this ?: defaultValue)
fun String?.outputStringValueFunGet(defaultValue: String) = if(this == null) defaultValue else this
// TODO 118.Kotlin Nullable type extension function of language
fun main(){
val infoValue : String ?= null
infoValue.outputStringValueFun(" I'm the default 1")
val name = "Derry"
name.outputStringValueFun(" I'm the default 2")
println(infoValue.outputStringValueFunGet(" I'm the default 3"))
println(name.outputStringValueFunGet(" I'm the default 4"))
}
2.Kotlin Linguistic infix keyword
private infix fun<C1,C2>C1.gogogo(c2:C2){
println(" Our infix expression , The content of a parameter is :$this")
println(" Our infix expression , The content of the second parameter is :$c2")
}
// TODO 118.Kotlin Linguistic infix keyword
//infix == Infix expression It simplifies the code
fun main(){
// Here we are map Built in infix expression
mapOf(" zero ".to(0))
mapOf(" One " to 1)
mapOf(" Two " to 2)
mapOf(" 3、 ... and " to 3)
// We wrote our own infix expression
123 gogogo ' male '
"Bxb".gogogo('M')
"Bxb2" gogogo 'M'
}
3.Kotlin Definition extension file of language
// Guide pack
import com.bxb.s6.bxb.randomItemValue
import com.bxb.s6.bxb.randomItemValuePrintln
// TODO 120.Kotlin Definition extension file of language
fun main(){
val list : List<String> = listOf(" Zhang San "," Li Si "," Wang Wu ")
val set:Set<Double> = setOf(15.6,456.5,784.6)
// If you don't need to extend the file
println(list.shuffled().first())
println(set.shuffled().first())
// Use extension files
println(list.randomItemValue())
println(set.randomItemValue())
list.randomItemValuePrintln()
set.randomItemValuePrintln()
}
4.Kotlin Language rename extended learning
// Guide pack
import com.bxb.s6.bxb.randomItemValue as g
import com.bxb.s6.bxb.randomItemValuePrintln as f
// TODO 120.Kotlin Language rename extended learning
fun main(){
val list : List<String> = listOf(" Zhang San "," Li Si "," Wang Wu ")
val set:Set<Double> = setOf(15.6,456.5,784.6)
// Use extension files
println(list.g())
println(set.g())
list.f()
set.f()
}
5.Kotlin Linguistic DSL Study
import java.io.File
class Context{
val info = " I am Bxb"
val name = "DDD"
fun toast(str:String) = println("toast:$str")
}
inline fun Context.apply5(lambda:Context.(String) -> Unit):Context{
lambda(info)
return this
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
inline fun File.applyFile(action:(String,String?) -> Unit) : File{
setWritable(true)
setReadable(true)
action(name,readLines()[0])
return this
}
// TODO 123.Kotlin Linguistic DSL Study
fun main(){
val context:Context = Context().apply5 {
//it == String == " I am Bxb"
println(" my it yes $it, my this yes :$this")
toast("success")
toast(it)
toast(name)
true
}.apply5 { }.apply5 { }.apply5 { }
println()
println(" I always output Context In itself :"+context.info)
println()
val file:File = File("D:\\a.txt")
.applyFile { fileName, data ->
println(" Your file name is :$fileName, The data in your file is :$data")
println(" Your file name is :$fileName, The data in your file is :$data")
println(" Your file name is :$fileName, The data in your file is :$data")
}.applyFile { a, b -> }.applyFile { a, b -> }
println(" I always output File In itself :${file.name}")
}
6.Kotlin The transformation function of language map
// TODO 124.Kotlin The transformation function of language map
fun main(){
val list = listOf(" Zhang San "," Li Si "," Wang Wu ")
// principle : Is to take you Anonymous functions The return value of the last line Add a new collection , The generic type of scheming and is R, And return a new set
val list2 : List<Int> = list.map {
"[$it]"
88
}
println(list2)
// purpose : and RxJava The idea of , As like as two peas
val list3 : List<String> = list.map {
" Name is :$it"
}.map {
"$it, The length of the text is :${it.length}"
}.map {
"[$it]"
}
for (s in list3){
print("$s ")
}
println()
list.map {
" Name is :$it"
}.map {
"$it, The length of the text is :${it.length}"
}.map {
"[$it]"
}.map {
print("$it ")
}
}
7.Kotlin The transformation function of language -flatmap
// TODO 125.Kotlin The transformation function of language -flatmap
// TODO flatmap amount to List<List<String>> A collection of collections , There are nested relationships
fun main(){
val list:List<String> = listOf(" Zhang San "," Li Si "," Wang Wu ")
val newList :List<String> = list.map {
" Your name is :$it"
}.map {
"$it, The length of your text is :${it.length}"
}.flatMap {
listOf("$it I'm learning C++","$it I'm learning Java","$it I'm learning Kotlin")
}
println(newList)
println()
val newList2:List<String> = list.flatMap {
listOf("$it I'm learning C++","$it I'm learning Java","$it I'm learning Kotlin")
}
println(newList2)
}
边栏推荐
- aaaaaaaaaaaaa
- Kotlin基础学习 17
- /silicosis/geo/GSE184854_ scRNA-seq_ mouse_ lung_ ccr2/GSE184854_ RAW/GSM5598265_ matrix_ inflection_ demult
- Find duplicates [Abstract binary / fast and slow pointer / binary enumeration]
- Halcon image rectification
- Qualcomm platform wifi-- WPA_ supplicant issue
- Verilog 避免 Latch
- 命名块 verilog
- C shallow copy and deep copy
- Detailed explanation of the difference between Verilog process assignment
猜你喜欢
Which of PMP and software has the highest gold content?
JIT deep analysis
Large screen visualization from bronze to the advanced king, you only need a "component reuse"!
Verilog state machine
跟着CTF-wiki学pwn——ret2shellcode
[HCIA continuous update] overview of dynamic routing protocol
Exchange rate query interface
[JS reverse series] analysis of a customs publicity platform
This article describes the step-by-step process of starting the NFT platform project
[JVM] detailed description of the process of creating objects
随机推荐
Xiaomi, a young engineer, was just going to make soy sauce
Named block Verilog
Global and Chinese markets for ultrasonic probe disinfection systems 2022-2028: Research Report on technology, participants, trends, market size and share
Use blocking or non blocking for streamline
4. Find the median of two positive arrays
Qualcomm platform WiFi -- P2P issue (2)
Find duplicates [Abstract binary / fast and slow pointer / binary enumeration]
Mathematical calculation in real mode addressing
What is the binding path of SAP ui5
OSPF LSA message parsing (under update)
C # joint Halcon's experience of breaking away from Halcon environment and various error reporting solutions
Redis set command line operation (intersection, union and difference, random reading, etc.)
Continuous assignment of Verilog procedure
Cache processing scheme in high concurrency scenario
GB/T-2423. XX environmental test documents, including the latest documents
The capacity is upgraded again, and the new 256gb large capacity specification of Lexar rexa 2000x memory card is added
Global and Chinese markets for hand hygiene monitoring systems 2022-2028: Research Report on technology, participants, trends, market size and share
流线线使用阻塞还是非阻塞
Failed to upgrade schema, error: “file does not exist
verilog REG 寄存器、向量、整数、实数、时间寄存器