Apply () : Can be thought of as a configuration function, passing in a receiver, then calling a series of methods to configure it for use, and finally returning the configured receiver

Fun main() {val file1 = File("D://file.txt") file1.setexecutable (true) file1.setreadable (true) File1. setWritable(true) // Use apply to do the above and return apply. // The variable names are omitted when calling each function because apply applies each configuration function to the receiver in lambda expressions, Val apply:File = file1.apply {setExecutable(true) setReadable(true) setWritable(true)}}Copy the code

Let (): The let function applies a variable to its Lambda expression so that it can be referenced by the IT keyword. In contrast to apply, let passes the receiver to Lambda, while Apply passes nothing. When the anonymous function completes, apply returns the current recipient, and let returns the last line of the lambda

Val result = listOf(1, 2, 3).first().let {// return 1*1 it * it}Copy the code
// Combine the null security operator fun formatGreeting(guestName: String?) : String { return guestName? .let { "welcome $it" } ? : "who are you" }Copy the code

Run (): Run does not return the receiver, but the result of the lambda

Val file1 = File("D://file.txt") val run = file1.run {//readText() reads the contents of the File once readText(). Contains (" ABC ")} var STR = "Abcdefghijklmn" str.run {// Determine whether the length is greater than 10 and return length>10}Copy the code

The run function supports chain calls

Var STR = "abcdefghijklmn" var STR = "abcdefghijklmn" Execute isLong //2. Pass the result of step 1 to showMessage //3. Run (::isLong).run(::showMessage).run(::print) fun isLong(name: String) = name.length > 10 fun showMessage(isLong: Boolean): String { return if (isLong) { "Too Long" } else { return "not too long" } }Copy the code

With (): With is a variation of the run function. It is called differently and takes a value argument as its first argument

With (STR){length>10}Copy the code

Also (): The Also function is similar to the let function in that, like let, also passes the receiver as a value argument to the lambda, but with one difference: Also returns the receiver object, while let returns the lambda result

Taking advantage of this difference, also is suitable for the same original object, doing things with side effects, and also returns the recipient object, allowing additional chain calls to be made based on the original recipient object

Var fileContents:List<String> File("D://file.txt").also {// print the File name print(it.name)}.also {// add the String of the File to the collection  fileContents = it.readLines() } println(fileContents)Copy the code

TakeIf ():takeIf needs to judge the conditional expression provided in the lambda, giving true and false results. If true, the receiver object is returned from the takeIf function, and if false, null. TakeIf is useful if you need to determine whether a condition is satisfied and then decide whether you can assign a variable or perform a task

Val readResult:String = File("D://file.txt").takeif {// the File exists and canRead it.exists() &&it.canread () // read the File}? .readText() ? : "null" print(readResult)Copy the code

TakeUnless (): Is an auxiliary function to takeIf, which will return the original recipient object only if the result of your given condition is false

File("D://file.txt"). TakeUnless {// File hidden invisible it. .readText() ? : "null"Copy the code