From 9887055ff48a5911d87d3547fe49629f04a73153 Mon Sep 17 00:00:00 2001 From: wadha almarshed Date: Sat, 15 Feb 2025 00:24:16 +0300 Subject: [PATCH] Task done + Bonus --- Task7/.gitignore | 32 ++++++++++ Task7/.idea/.gitignore | 3 + .../inspectionProfiles/Project_Default.xml | 6 ++ Task7/.idea/kotlinc.xml | 10 ++++ Task7/.idea/libraries/KotlinJavaRuntime.xml | 17 ++++++ Task7/.idea/misc.xml | 6 ++ Task7/.idea/modules.xml | 8 +++ Task7/.idea/vcs.xml | 6 ++ Task7/Task7.iml | 15 +++++ Task7/src/Main.kt | 3 + Task7/src/lamndacollections.kt | 60 +++++++++++++++++++ 11 files changed, 166 insertions(+) create mode 100644 Task7/.gitignore create mode 100644 Task7/.idea/.gitignore create mode 100644 Task7/.idea/inspectionProfiles/Project_Default.xml create mode 100644 Task7/.idea/kotlinc.xml create mode 100644 Task7/.idea/libraries/KotlinJavaRuntime.xml create mode 100644 Task7/.idea/misc.xml create mode 100644 Task7/.idea/modules.xml create mode 100644 Task7/.idea/vcs.xml create mode 100644 Task7/Task7.iml create mode 100644 Task7/src/Main.kt create mode 100644 Task7/src/lamndacollections.kt diff --git a/Task7/.gitignore b/Task7/.gitignore new file mode 100644 index 0000000..3ddbf4c --- /dev/null +++ b/Task7/.gitignore @@ -0,0 +1,32 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Kotlin ### +.kotlin + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/Task7/.idea/.gitignore b/Task7/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/Task7/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/Task7/.idea/inspectionProfiles/Project_Default.xml b/Task7/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..df543e3 --- /dev/null +++ b/Task7/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/Task7/.idea/kotlinc.xml b/Task7/.idea/kotlinc.xml new file mode 100644 index 0000000..cba7a76 --- /dev/null +++ b/Task7/.idea/kotlinc.xml @@ -0,0 +1,10 @@ + + + + + + + \ No newline at end of file diff --git a/Task7/.idea/libraries/KotlinJavaRuntime.xml b/Task7/.idea/libraries/KotlinJavaRuntime.xml new file mode 100644 index 0000000..3e6dc71 --- /dev/null +++ b/Task7/.idea/libraries/KotlinJavaRuntime.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Task7/.idea/misc.xml b/Task7/.idea/misc.xml new file mode 100644 index 0000000..eeb80f7 --- /dev/null +++ b/Task7/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Task7/.idea/modules.xml b/Task7/.idea/modules.xml new file mode 100644 index 0000000..f7bdceb --- /dev/null +++ b/Task7/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Task7/.idea/vcs.xml b/Task7/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/Task7/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Task7/Task7.iml b/Task7/Task7.iml new file mode 100644 index 0000000..43dd653 --- /dev/null +++ b/Task7/Task7.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Task7/src/Main.kt b/Task7/src/Main.kt new file mode 100644 index 0000000..a7a5e00 --- /dev/null +++ b/Task7/src/Main.kt @@ -0,0 +1,3 @@ +fun main() { + println("Hello World!") +} \ No newline at end of file diff --git a/Task7/src/lamndacollections.kt b/Task7/src/lamndacollections.kt new file mode 100644 index 0000000..5c7ceb3 --- /dev/null +++ b/Task7/src/lamndacollections.kt @@ -0,0 +1,60 @@ +import java.nio.file.DirectoryStream.Filter +//Part 2: Filtering Strings with Lambda +fun filterNames(names: List, filtered: (String) -> Boolean): List{ + return names.filter(filtered) +} + +//Part 4: Customizing the Filter of a List +fun customFilter(numbers: List, filter: (Int) -> Boolean): List{ + return numbers.filter(filter) +} + +//Part 5: Combining Lambdas +fun processNumbers(numbers: List,filter: (Int) -> Boolean ): List{ + return numbers.filter(filter).filter(filter) +} + +//Bonus +fun processNumbers(numbers: List,filter: (Int) -> Boolean, transform: (Int) -> Int ): List{ + return numbers.filter(filter).map(transform) +} + + + +fun main() { + + //Part 1: Lambda with List Transformation + val listOfNumbers = listOf(1,2,3,4,5,6,7,8,9,10) + val doubledNumbers = listOfNumbers.map { it * 2 } + println(doubledNumbers) + + //Part 2: Filtering Strings with Lambda + val names = listOf("Alice", "Bob", "Amir", "Charlie", "Annie", "David") + val filteredNames = filterNames(names) { it.startsWith('A') } + println(filteredNames) + + //Part 3: Sorting with Custom Lambda + val words = listOf("apple", "banana", "kiwi", "strawberry", "grape") + val sortedList = words.sortedByDescending { it.length } + println(sortedList) + + //Part 4: Customizing the Filter of a List + val numbers = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) + val greaterThan5 = customFilter(numbers) {it > 5} + val evenNumbers = customFilter(numbers) {it % 2 == 0} + val multiplesOf3 = customFilter(numbers) {it % 3 ==0} + + println(greaterThan5) + println(evenNumbers) + println(multiplesOf3) + + //Part 5: Combining Lambdas + // I'm reusing numbers parameter from part 4 + val processedNumbers = processNumbers(numbers) {it % 2 != 0}.map { it * it } + println(processedNumbers) + + //Bonus + val processedNumbersBonus = processNumbers(numbers, { it % 2 != 0 }, { it * it }) + println(processedNumbers) + +}