From 6a376f396d002b98a46a50b7be128e069525ae48 Mon Sep 17 00:00:00 2001 From: mohammedsheshtar <112146380+mohammedsheshtar@users.noreply.github.com> Date: Fri, 14 Feb 2025 19:33:12 +0300 Subject: [PATCH 1/4] finished part 1: multiplied a list of numbers by two using a lambda function --- .idea/misc.xml | 2 +- src/Lambda_Collections.kt | 7 +++++++ src/Main.kt | 3 --- 3 files changed, 8 insertions(+), 4 deletions(-) create mode 100644 src/Lambda_Collections.kt delete mode 100644 src/Main.kt diff --git a/.idea/misc.xml b/.idea/misc.xml index 03f397c..10a22f9 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/src/Lambda_Collections.kt b/src/Lambda_Collections.kt new file mode 100644 index 0000000..0ebd6bd --- /dev/null +++ b/src/Lambda_Collections.kt @@ -0,0 +1,7 @@ +fun main() { + // part 1 + println("Part 1"); + val nums = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); + println("[*] ${nums.map {it * 2}}"); + +} \ No newline at end of file diff --git a/src/Main.kt b/src/Main.kt deleted file mode 100644 index a7a5e00..0000000 --- a/src/Main.kt +++ /dev/null @@ -1,3 +0,0 @@ -fun main() { - println("Hello World!") -} \ No newline at end of file From ebec63ae715d8c4f9fae6f7c744eb5f220b6c44b Mon Sep 17 00:00:00 2001 From: mohammedsheshtar <112146380+mohammedsheshtar@users.noreply.github.com> Date: Fri, 14 Feb 2025 19:35:50 +0300 Subject: [PATCH 2/4] finished part 2: generated a name list and filtered it to display only names starting with 'A' --- src/Lambda_Collections.kt | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Lambda_Collections.kt b/src/Lambda_Collections.kt index 0ebd6bd..92ab01f 100644 --- a/src/Lambda_Collections.kt +++ b/src/Lambda_Collections.kt @@ -4,4 +4,13 @@ fun main() { val nums = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); println("[*] ${nums.map {it * 2}}"); -} \ No newline at end of file + // part 2 + println("\nPart 2"); + val names = mutableListOf("Alice", "Bob", "Amir", "Charlie", "Annie", "David"); + println("[*] ${filterNames(names) { it.contains("A" )}}"); + +} + +fun filterNames( names: MutableList, filter: (String) -> Boolean ): List{ + return names.filter(filter); +} From 6e2fb8a54072b853f7cfe88ac79daf29e2bb0455 Mon Sep 17 00:00:00 2001 From: mohammedsheshtar <112146380+mohammedsheshtar@users.noreply.github.com> Date: Fri, 14 Feb 2025 19:39:56 +0300 Subject: [PATCH 3/4] finished part 3: created a list of fruit names and sorted it in descending order by name length --- src/Lambda_Collections.kt | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Lambda_Collections.kt b/src/Lambda_Collections.kt index 92ab01f..53c2135 100644 --- a/src/Lambda_Collections.kt +++ b/src/Lambda_Collections.kt @@ -9,6 +9,13 @@ fun main() { val names = mutableListOf("Alice", "Bob", "Amir", "Charlie", "Annie", "David"); println("[*] ${filterNames(names) { it.contains("A" )}}"); + // part 3 + println("\nPart 3"); + val fruits = listOf("apple", "banana", "kiwi", "strawberry", "grape"); + println("[*] ${fruits.sortedByDescending { it.length }}") + + + } fun filterNames( names: MutableList, filter: (String) -> Boolean ): List{ From 8f748732a8aa66657b2761d8e8770f630fa06e07 Mon Sep 17 00:00:00 2001 From: mohammedsheshtar <112146380+mohammedsheshtar@users.noreply.github.com> Date: Fri, 14 Feb 2025 19:52:04 +0300 Subject: [PATCH 4/4] finished part 5: Added a list of numbers filtered by a function that removes evens and squares the rest --- src/Lambda_Collections.kt | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/Lambda_Collections.kt b/src/Lambda_Collections.kt index 53c2135..050c57f 100644 --- a/src/Lambda_Collections.kt +++ b/src/Lambda_Collections.kt @@ -1,19 +1,30 @@ fun main() { // part 1 - println("Part 1"); + println("Part 1:"); val nums = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); println("[*] ${nums.map {it * 2}}"); // part 2 - println("\nPart 2"); + println("\nPart 2:"); val names = mutableListOf("Alice", "Bob", "Amir", "Charlie", "Annie", "David"); println("[*] ${filterNames(names) { it.contains("A" )}}"); // part 3 - println("\nPart 3"); + println("\nPart 3:"); val fruits = listOf("apple", "banana", "kiwi", "strawberry", "grape"); println("[*] ${fruits.sortedByDescending { it.length }}") + // part 4 + println("\nPart 4:"); + val numbers = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); + println("[*] ${customFilter(numbers) { it > 5 }}"); + println("[*] ${customFilter(numbers) { it % 2 == 0 }}"); + println("[*] ${customFilter(numbers) { it % 3 == 0}}"); + + // part 5 + println("\nPart 5:"); + val numberSquare = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); + println("[*] ${processNumbers(numberSquare, {it % 2 != 0}, {it * it})}"); } @@ -21,3 +32,14 @@ fun main() { fun filterNames( names: MutableList, filter: (String) -> Boolean ): List{ return names.filter(filter); } + +fun customFilter(numbers: List, filter: (Int) -> Boolean): List { + return numbers.filter(filter); +} + +fun processNumbers(numbers: List, filterEven: (Int) -> Boolean, filterSquare: (Int) -> (Int)): List { + var numEven = numbers.filter(filterEven); + var numSquare = numEven.map(filterSquare); + return numSquare; + +}