Lynda-5
**************************
Function Programming
**************************
Pros.
1. No messy state issue
2. Easier code reuse
3. Safe multithreading
Cons
1. Less efficient
******************
Lambda - also know as Lambda expression or ananymous function or function literal this means name of function define in curly braces and argument not surrounded by paranthesis (). Error separate from the code.
******************
-------------------------------------- ------------------------------------ ----
data class Student(val name : String, val age : Int)
fun getStudentList() : List<Student>{
return listOf(
Student("One", 23),
Student("Two", 19),
Student("Three", 36),
Student("Four", 77),
Student("Five", 16),
Student("Six", 14)
)
}
fun main(args: Array<String>) {
val students = getStudentList()
val combo = students.map { a -> a.name + " : " + a.age }
println("Combos : $combo")
println("The oldest student : ${students.maxBy { it.age }}")
val studentsWithLongNames = students.filter { it.name.length > 4 }
println(studentsWithLongNames)
}
------------------------------------- ------------------------------------ ----
**********************
Closure - when local variable of our function keep alive after the function has ended.
**********************
----------------------------------------------
package Kotlin
fun closureMake() : () -> Unit{
var num = 0
return { println(num++)}
}
fun main(args: Array<String>) {
val myCounter1 = closureMake()
val myCounter2 = closureMake()
myCounter1()
myCounter1()
myCounter1()
myCounter1()
myCounter2()
myCounter2()
myCounter1()
}
==============OUTPUT
0
1
2
3
0
1
4
Note : Each counter have own copy of num variable amd its save value in closure.
---------------------------------------------
************************
METHOD CHAINING
************************
1. A sequences is evaluate the collection, pretty much identical to stream in java 8. Stream can do squences can't - run across multiple CPU.
2. Sequence is not collection.
--------------------------------------------------
fun sequences(){
var students = getStudentList()
val sqStudents = students.drop(1).take(3).toList()
println("Sequenced Students : " + sqStudents)
val numbers = generateSequence (100){ it + 1 }
println("Numbers : "+numbers.drop(5).take(20).toList())
val square = generateSequence (1){it+1}.map { it*it }
val evenSequences = square.filter { it % 2 == 0 }
println("Even Squares : " + evenSequences.take(5).toList() )
val fib = generateSequence (1 to 1){it.second to it.first + it.second}.map { it.first }
println("Fibonacci : "+fib.take(15).toList())
}
fun main(args: Array<String>) {
sequences()
}
-------------------------------------------------
22
Function Programming
**************************
Pros.
1. No messy state issue
2. Easier code reuse
3. Safe multithreading
Cons
1. Less efficient
******************
Lambda - also know as Lambda expression or ananymous function or function literal this means name of function define in curly braces and argument not surrounded by paranthesis (). Error separate from the code.
******************
-------------------------------------- ------------------------------------ ----
data class Student(val name : String, val age : Int)
fun getStudentList() : List<Student>{
return listOf(
Student("One", 23),
Student("Two", 19),
Student("Three", 36),
Student("Four", 77),
Student("Five", 16),
Student("Six", 14)
)
}
fun main(args: Array<String>) {
val students = getStudentList()
val combo = students.map { a -> a.name + " : " + a.age }
println("Combos : $combo")
println("The oldest student : ${students.maxBy { it.age }}")
val studentsWithLongNames = students.filter { it.name.length > 4 }
println(studentsWithLongNames)
}
------------------------------------- ------------------------------------ ----
**********************
Closure - when local variable of our function keep alive after the function has ended.
**********************
----------------------------------------------
package Kotlin
fun closureMake() : () -> Unit{
var num = 0
return { println(num++)}
}
fun main(args: Array<String>) {
val myCounter1 = closureMake()
val myCounter2 = closureMake()
myCounter1()
myCounter1()
myCounter1()
myCounter1()
myCounter2()
myCounter2()
myCounter1()
}
==============OUTPUT
0
1
2
3
0
1
4
Note : Each counter have own copy of num variable amd its save value in closure.
---------------------------------------------
************************
METHOD CHAINING
************************
1. A sequences is evaluate the collection, pretty much identical to stream in java 8. Stream can do squences can't - run across multiple CPU.
2. Sequence is not collection.
--------------------------------------------------
fun sequences(){
var students = getStudentList()
val sqStudents = students.drop(1).take(3).toList()
println("Sequenced Students : " + sqStudents)
val numbers = generateSequence (100){ it + 1 }
println("Numbers : "+numbers.drop(5).take(20).toList())
val square = generateSequence (1){it+1}.map { it*it }
val evenSequences = square.filter { it % 2 == 0 }
println("Even Squares : " + evenSequences.take(5).toList() )
val fib = generateSequence (1 to 1){it.second to it.first + it.second}.map { it.first }
println("Fibonacci : "+fib.take(15).toList())
}
fun main(args: Array<String>) {
sequences()
}
-------------------------------------------------
22
Comments
Post a Comment