Kotlin (二)

尾递归

data class ListNode(var next: ListNode? = null, val value: Int = 0)
/**
   *tailrec 关键字声明为尾递归 
 * 递归查找单链表
 */
tailrec fun findListNode(head: ListNode?, value: Int): ListNode? {
    head ?: return null
    if (head.value == value) return head
    return findListNode(head = head.next, value = value)
}
//查找链表最后一个节点
tailrec fun findListNode(head: ListNode?): ListNode? {
      head ?: return null
      head.next ?: return head
      return findListNode(head = head.next)
}
//为链表添加一个节点
fun addListNode(head: ListNode, node: ListNode): Boolean {
      val endNode = findListNode(head)
      return if (endNode != null) {
        endNode.next = node
        true
    } else false
 }

//二叉树
data class TreeNode(val value: Int) {
    var left: TreeNode? = null
    var right: TreeNode? = null
}
//遍历二叉树
tailrec fun findTreeNode(head: TreeNode?, value: Int): TreeNode? {
      head ?: return null
      if (head.value == value) return head
      return findTreeNode(head.left!!, value) ?: return findTreeNode(head.right!!, value)
}

高级函数中的block

private fun block() {
  //with(){}
val br = BufferedReader(FileReader("hello.txt"))
with(br) {
    while (true) {
        val readLine = readLine()
        readLine ?: break
        println(readLine)
    }
    close()
}

//apply{}
val br2 = BufferedReader(FileReader("hello.txt"))
br2.apply {
    while (true) {
        val readLine = readLine()
        readLine ?: break
        println(readLine)
    }
    close()
}

//let{}
val br3 = BufferedReader(FileReader("hello.txt"))
br3.let {
    while (true) {
        val readLine = it.readLine()
        readLine ?: break
        println(readLine)
    }
    it.close()
}

 /**
 * IO在use中自动关闭
 */
val br4 = BufferedReader(FileReader("hello.txt"))
br4.use {
    while (true) {
        val readLine = it.readLine()
        readLine ?: break
        println(readLine)
    }
}

val br5 = BufferedReader(FileReader("hello.txt"))
br5.run {
    while (true) {
        val readLine = readLine()
        readLine ?: break
        println(readLine)
    }
}
}

高阶函数之迭代

private fun itarator() {
    val list = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9)
    var list2 = listOf(1..20, 20..50)
    var arrayList = ArrayList()
    list.map {
        it * 2 + 5
    }
//::println 函数引用 ,map 和flatMap变换操作
list.forEach(::println)
list2.flatMap {
    it.map {
    }
}
var ps = listOf(PP("12"), PP("34"), PP("56"))
//转换,类似Rxjava中map
ps.map {
    it.name = "11"
}
ps.forEach {
    println(it.name)
}
println(factorial(20))
}
fun factorial(int: Int): Int {
if (int == 0) return 1
// 积累
return (1..int).reduce { acc, i -> acc * i }
  }

函数引用

private fun base() {
var string = listOf("aaa", "bbbb")
var hello = Hello::world
// filter接收一个参数,所以isNotEmpty亦需要是一个参数
string.filter(String::isNotEmpty)
string.forEach(::println)
val pdfPrintln = PdfPrintln()
string.forEach(pdfPrintln::println)//1.1开始支持
string.forEach {
}
}
class PdfPrintln {
fun println(any: Any) {
    kotlin.io.println(any)
}
}
 class Hello {
fun world(any: Any) {
    println("Hello")
}
}

闭包

1、函数的运行时环境:
2、持有函数的运行状态;
3、函数内部可以定义函数,也可以定义类(不推荐)

fun add(x: Int) = fun(y: Int) = x + y
fun add(x: Long): (Long) -> Long {
      return fun(y: Long): Long {
          return x + y
      }
}
private fun 闭包() {
      val add2 = add(2)
      println(add2(8))
      println(add(5)(8))
}

符合函数

fun log(tag: String) = fun(target: OutputStream) = fun(message: Any?) = target.write("[$tag]$message".toByteArray())

fun  Function3.curried() = fun(p1: P1) fun(p2: P2) = fun(p3: P3) = this(p1, p2, p3)
infix fun  Function1.andThen(function: Function1): Function1 {
return fun(p1: P1): R {
    return function.invoke(this.invoke(p1))
    }
}

infix fun  Function1.compose(function: Function1): Function1 {
return fun(p1: P1): R {
    return this.invoke(function.invoke(p1))
      }
}

学习Kotlin看官方文档很权威

你可能感兴趣的:(Kotlin (二))