From dfc3e10086ea9ab61a9907fc7718536ed8fec725 Mon Sep 17 00:00:00 2001 From: BarrakBM Date: Thu, 13 Feb 2025 14:21:08 +0300 Subject: [PATCH 1/3] Task 1 and 2 are done --- src/Main.kt | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Main.kt b/src/Main.kt index a7a5e00..6dae446 100644 --- a/src/Main.kt +++ b/src/Main.kt @@ -1,3 +1,20 @@ +// Part 1 +// The function have 2 parameters operation and lambda +// the lambda function will take 1 argument in this case String +// and return a Boolean of if it's true or false, however the return type of the lambda is List +// when we apply the lambda function to the list it will return List +// Hence, it will return everything (List) that's true in our case names starts with A +fun filterNames(names: List, operation: (String) -> Boolean):List{ + return names.filter(operation) +} + fun main() { - println("Hello World!") + // Part 1: Lambda with List Transformation + val numbers = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) + println("Part 1: ${numbers.map { it * 2 }}") + + // Part 2: Filtering Strings with Lambda + val names = listOf("Alice", "Bob", "Amir", "Charlie", "Annie", "David") + val namesWithA = filterNames(names) { it.contains('A') } + println(namesWithA) // print final result } \ No newline at end of file From 01ba705c0a38c9d9aa4d58329b85bf63c410c19e Mon Sep 17 00:00:00 2001 From: BarrakBM Date: Thu, 13 Feb 2025 14:49:00 +0300 Subject: [PATCH 2/3] Task 3 is completed --- src/Main.kt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Main.kt b/src/Main.kt index 6dae446..9e60dc8 100644 --- a/src/Main.kt +++ b/src/Main.kt @@ -16,5 +16,10 @@ fun main() { // Part 2: Filtering Strings with Lambda val names = listOf("Alice", "Bob", "Amir", "Charlie", "Annie", "David") val namesWithA = filterNames(names) { it.contains('A') } - println(namesWithA) // print final result + println("Part 2: $namesWithA") // print final result + + //Part 3: Sorting with Custom Lambda + val fruits = listOf("apple", "banana", "kiwi", "strawberry", "grape") + val fruitSortedByLength = fruits.sortedByDescending {it.length} + println("Part 3: $fruitSortedByLength") } \ No newline at end of file From d7c175b16c6175ec2e3451cf4bbcfe11130cecf7 Mon Sep 17 00:00:00 2001 From: BarrakBM Date: Thu, 13 Feb 2025 16:50:37 +0300 Subject: [PATCH 3/3] Kotlin Lambda collections completed --- src/Main.kt | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/Main.kt b/src/Main.kt index 9e60dc8..1f6c93b 100644 --- a/src/Main.kt +++ b/src/Main.kt @@ -8,6 +8,16 @@ fun filterNames(names: List, operation: (String) -> Boolean):List, filter: (Int) -> Boolean): List{ + return numbers.filter(filter) +} + +// Part 5: +fun processNumbers(numbers: List, filter: (Int) -> Boolean): List{ + return numbers.filter(filter) +} + fun main() { // Part 1: Lambda with List Transformation val numbers = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) @@ -22,4 +32,22 @@ fun main() { val fruits = listOf("apple", "banana", "kiwi", "strawberry", "grape") val fruitSortedByLength = fruits.sortedByDescending {it.length} println("Part 3: $fruitSortedByLength") + + // Part 4: + // Creating a variable to reduce code redundancy. + val numbersList = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) + println("Part 4: ${customFilter(numbersList){ it > 5 }}") + println("Part 4: ${customFilter(numbersList){ it % 2 == 0}}") + println("Part 4: ${customFilter(numbersList){ it % 3 == 0 }}") + + // Part 5: Combining Lambdas + val processedNums = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) + val evenNums = processNumbers(processedNums){ it % 2 != 0} // filter out the even numbers + val squaredEvenNums = evenNums.map { it * it } + println("Part 5: $squaredEvenNums") + + + + + } \ No newline at end of file