Skip to content

Commit e39e1fa

Browse files
committed
Functional Kotlin - Lambdas and Higher-Order Functions
1 parent c70d7aa commit e39e1fa

11 files changed

+294
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
/*
3+
EXAMPLE ONE
4+
5+
* 1. Lambda Expression
6+
* 2. Higher-Order Function
7+
* */
8+
fun main(args: Array<String>) {
9+
10+
val program = Program()
11+
12+
program.addTwoNumbers(2, 7) // Simple way... for better understanding
13+
14+
program.addTwoNumbers(2, 7, object : MyInterface { // Using Interface / OOPs way
15+
16+
override fun execute(sum: Int) {
17+
println(sum) // Body
18+
}
19+
})
20+
21+
val test: String = "Hello"
22+
23+
val myLambda: (Int) -> Unit = { s: Int -> println(s)} // Lambda Expression [ Function ]
24+
program.addTwoNumbers(2, 7, myLambda)
25+
}
26+
27+
class Program {
28+
29+
fun addTwoNumbers(a: Int, b: Int, action: (Int) -> Unit) { // High Level Function with Lambda as Parameter
30+
31+
val sum = a + b
32+
action(sum) // println(sum)
33+
// println(sum) // Body
34+
}
35+
36+
fun addTwoNumbers(a: Int, b: Int, action: MyInterface) { // Using Interface / Object Oriented Way
37+
val sum = a + b
38+
action.execute(sum)
39+
}
40+
41+
fun addTwoNumbers(a: Int, b: Int) { // Simple way.. Just for Better Understanding
42+
43+
val sum = a + b
44+
println(sum)
45+
}
46+
}
47+
48+
interface MyInterface {
49+
fun execute(sum: Int)
50+
}

src/36_lambdas_example_two.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
/*
3+
EXAMPLE TWO
4+
5+
* 1. Lambda Expression
6+
* 2. Higher-Order Function
7+
* */
8+
fun main(args: Array<String>) {
9+
10+
val program = MyProgram()
11+
12+
// val myLambda: (Int, Int) -> Int = { x, y -> x + y} // Lambda Expression [ Function ]
13+
// OR,
14+
// program.addTwoNumbers(2, 7, { x, y -> x + y })
15+
// OR,
16+
program.addTwoNumbers(2, 7) {x, y -> x + y}
17+
}
18+
19+
class MyProgram {
20+
21+
fun addTwoNumbers(a: Int, b: Int, action: (Int, Int) -> Int) { // High Level Function with Lambda as Parameter
22+
23+
val result = action(a, b)
24+
println(result)
25+
}
26+
}

src/37_lambdas_closures.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
/*
3+
* 1. Closures
4+
* */
5+
fun main(args: Array<String>) {
6+
7+
val program = TheProgram()
8+
9+
var result = 0
10+
11+
program.addTwoNumbers(2, 7) {x, y -> result = x + y}
12+
13+
println(result)
14+
}
15+
16+
class TheProgram {
17+
18+
fun addTwoNumbers(a: Int, b: Int, action: (Int, Int) -> Unit) { // High Level Function with Lambda as Parameter
19+
20+
action(a, b)
21+
}
22+
}

src/38_it_keyword_lambdas.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
3+
/*
4+
* 1. 'it' keyword
5+
* */
6+
fun main(args: Array<String>) {
7+
8+
val program = Programs()
9+
program.reverseAndDisplay("hello", { it.reversed() })
10+
}
11+
12+
class Programs {
13+
14+
fun reverseAndDisplay(str: String, myFunc: (String) -> String) { // High Level Function with Lambda as Parameter
15+
16+
val result = myFunc(str) // it.reversed() ==> str.reversed() ==> "hello".reversed() = "olleh"
17+
println(result)
18+
}
19+
}

src/39_with_apply_functions.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
/*
3+
* 1. 'with' function
4+
* 2. 'apply' function
5+
* */
6+
fun main(args: Array<String>) {
7+
8+
var person = Perrson()
9+
10+
with(person) {
11+
name = "Steve"
12+
age = 23
13+
}
14+
15+
person.apply {
16+
name = "Steve"
17+
age = 23
18+
}.startRun()
19+
20+
println(person.name)
21+
println(person.age)
22+
}
23+
24+
class Perrson {
25+
26+
var name: String = ""
27+
var age: Int = -1
28+
29+
fun startRun() {
30+
println("Now I am ready to run")
31+
}
32+
}

src/40_arrays.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
/*
3+
* 1. Arrays
4+
* */
5+
fun main(args: Array<String>) {
6+
7+
// Elements : 32 0 0 54 0
8+
// Index : 0 1 2 3 4
9+
10+
var myArray = Array<Int>(5) { 0 } // Mutable. Fixed Size.
11+
myArray[0] = 32
12+
myArray[3] = 54
13+
myArray[1] = 11
14+
15+
for (element in myArray) { // Using individual elements (Objects)
16+
println(element)
17+
}
18+
19+
println()
20+
21+
for (index in 0..myArray.size - 1) {
22+
println(myArray[index])
23+
}
24+
}

src/41_list.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
/*
3+
* 1. List and ArrayList
4+
* */
5+
fun main(args: Array<String>) {
6+
7+
// Elements :
8+
// Index : 0 1 2 3 4
9+
10+
// var list = mutableListOf<String>() // Mutable, No Fixed Size, Can Add or Remove Elements
11+
// var list = arrayListOf<String>() // Mutable, No Fixed Size, Can Add or Remove Elements
12+
var list = ArrayList<String>() // Mutable, No Fixed Size, Can Add or Remove Elements
13+
list.add("Yogi") // 0
14+
list.add("Manmohan") // 1
15+
list.add("Vajpayee") // 2
16+
17+
// list.remove("Manmohan")
18+
// list.add("Vajpayee")
19+
20+
list[1] = "Modi"
21+
22+
for (element in list) { // Using individual elements (Objects)
23+
println(element)
24+
}
25+
}

src/42_map_hashmap.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
/*
3+
* 1. Map and HashMap
4+
* */
5+
fun main(args: Array<String>) {
6+
7+
// Map Tutorial: Key-Value pair
8+
// var myMap = HashMap<Int, String>() // Mutable, READ and WRITE both, No Fixed Size
9+
// var myMap = mutableMapOf<Int, String>() // Mutable, READ and WRITE both, No Fixed Size
10+
var myMap = hashMapOf<Int, String>() // Mutable, READ and WRITE both, No Fixed Size
11+
12+
myMap.put(4, "Yogi")
13+
myMap.put(43, "Manmohan")
14+
myMap.put(7, "Vajpayee")
15+
16+
myMap.put(43, "Modi")
17+
18+
for (key in myMap.keys) {
19+
println("Element at $key = ${myMap[key]}") // myMap.get(key)
20+
}
21+
}

src/43_set_hashset.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
/*
3+
* 1. Set and HashSet
4+
* */
5+
fun main(args: Array<String>) {
6+
7+
// "Set" contains unique elements
8+
// "HashSet" also contains unique elements but sequence is not guaranteed in output
9+
10+
var mySet = mutableSetOf<Int>( 2, 54, 3, 1, 0, 9, 9, 9, 8) // Mutable Set, READ and WRITE both
11+
// var mySet = hashSetOf<Int>( 2, 54, 3, 1, 0, 9, 9, 9, 8) // Mutable Set, READ and WRITE both
12+
13+
mySet.remove(54)
14+
mySet.add(100)
15+
16+
for (element in mySet) {
17+
println(element)
18+
}
19+
}

src/44_filter_map_sorting.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/** FILTER
2+
* Returns a list containing only elements matching the given [predicate]
3+
* */
4+
5+
/** MAP
6+
* Returns a list containing the results of applying the given [transform] function
7+
* to each element in the original collection
8+
* */
9+
10+
fun main(args: Array<String>) {
11+
12+
val myNumbers: List<Int> = listOf(2, 3, 4, 6, 23, 90)
13+
14+
val mySmallNums = myNumbers.filter { it < 10 } // OR { num -> num < 10 }
15+
for (num in mySmallNums) {
16+
println(num)
17+
}
18+
19+
val mySquaredNums = myNumbers.map { it * it } // OR { num -> num * num }
20+
for (num in mySquaredNums) {
21+
println(num)
22+
}
23+
24+
var people = listOf<Pperson>(Pperson(10, "Steve"), Pperson(23, "Annie"), Pperson(17, "Sam"))
25+
var names = people.filter { person ->person.name.startsWith("S") }.map { it.name }
26+
27+
for (name in names) {
28+
println(name)
29+
}
30+
}
31+
32+
class Pperson(var age: Int, var name: String) {
33+
// Some other code..
34+
}

src/45_predicate.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
/**
3+
* PREDICATES
4+
* */
5+
fun main(args: Array<String>) {
6+
7+
val myNumbers = listOf(2, 3, 4, 6, 23, 90)
8+
9+
val myPredicate = { num: Int -> num > 10 }
10+
11+
val check1 = myNumbers.all( myPredicate ) // Are all elements greater than 10?
12+
println(check1)
13+
14+
val check2 = myNumbers.any(myPredicate) // Does any of these elements satisfy the predicate?
15+
println(check2)
16+
17+
val totalCount: Int = myNumbers.count(myPredicate) // Number of elements that satify the predicate.
18+
println(totalCount)
19+
20+
val num = myNumbers.find(myPredicate) // Returns the first number that matches the predicate
21+
println(num)
22+
}

0 commit comments

Comments
 (0)