From ad8944e372361bea8703be3c4f86ade2e7e2a2b9 Mon Sep 17 00:00:00 2001 From: A Date: Thu, 13 Feb 2025 15:40:33 +0300 Subject: [PATCH 1/2] Task: Lambdas with Collections solved by ASMA, the Bonus will be coming shortly after with another pull request --- .idea/modules.xml | 1 + lamda collections/lamda collections.iml | 15 ++++++++++ lamda collections/src/Main.kt | 38 +++++++++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 lamda collections/lamda collections.iml create mode 100644 lamda collections/src/Main.kt diff --git a/.idea/modules.xml b/.idea/modules.xml index 54b9186..987f777 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -3,6 +3,7 @@ + \ No newline at end of file diff --git a/lamda collections/lamda collections.iml b/lamda collections/lamda collections.iml new file mode 100644 index 0000000..4fd7bdb --- /dev/null +++ b/lamda collections/lamda collections.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/lamda collections/src/Main.kt b/lamda collections/src/Main.kt new file mode 100644 index 0000000..713dbef --- /dev/null +++ b/lamda collections/src/Main.kt @@ -0,0 +1,38 @@ +fun main() { //Is it a common practice to write all functions outside main function? and anything else (val,lists,..etc) inside main? + val numbersList = listOf(1,2,3,4,5,6,7,8,9,10) + val doubledNumbers = numbersList.map { it * 2 } + println(doubledNumbers) +// + val names = listOf("Alice", "Bob", "Amir", "Charlie", "Annie", "David") + fun filterNames(names: List, logic: (List) -> List): List { + return logic(names) + } + val logic: (List) -> List = { namesList -> namesList.filter { it.startsWith("A") }} + println(filterNames(names, logic)) //Q? Can i do this in a simpler way? the question is stupidly written +// + val words = listOf("apple", "banana", "kiwi", "strawberry", "grape") + val sorting = {list: List -> list.sortedByDescending { it.length }} //Q! this one is used for any list? + //val sorting = words.sortedByDescending { it.length } // while this one is used for this specific list? + println(sorting(words)) +// + fun customFilter(numbers: List, filter: (Int) -> Boolean): List { + return numbers.filter(filter) } + + // Instead of writing in-line Lambdas in the 2nd parameter, I can write full lambda funcions + // that I can use later on: + // val greaterThanFive: (Int) -> Boolean = { it > 5 } + //val evenNumbers: (Int) -> Boolean = { it % 2 == 0 } + // val multiplesOfThree: (Int) -> Boolean = { it % 3 == 0 } + + println(customFilter(numbersList) { it > 5 }) + println(customFilter(numbersList) { it % 2 == 0 }) + println(customFilter(numbersList) { it % 3 == 0 }) + +// + fun processNumbers(numbers: List): List { + val filterEven = { number: Int -> number % 2 !=0 } + val square = { number: Int -> number * number } + return numbers.filter(filterEven).map(square) } + + println(processNumbers(numbersList)) +} \ No newline at end of file From dd9c3609a508478c33e333e84df3f0b4acd719f6 Mon Sep 17 00:00:00 2001 From: A Date: Thu, 13 Feb 2025 16:06:34 +0300 Subject: [PATCH 2/2] Asma solved the bonus, thanks --- lamda collections/src/Main.kt | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lamda collections/src/Main.kt b/lamda collections/src/Main.kt index 713dbef..e5da259 100644 --- a/lamda collections/src/Main.kt +++ b/lamda collections/src/Main.kt @@ -1,4 +1,5 @@ -fun main() { //Is it a common practice to write all functions outside main function? and anything else (val,lists,..etc) inside main? +fun main() { //Is it a common practice to write all functions outside main function? + // and anything else (val,lists,..etc) inside main? val numbersList = listOf(1,2,3,4,5,6,7,8,9,10) val doubledNumbers = numbersList.map { it * 2 } println(doubledNumbers) @@ -35,4 +36,9 @@ fun main() { //Is it a common practice to write all functions outside main funct return numbers.filter(filterEven).map(square) } println(processNumbers(numbersList)) +// BONUS + fun processNumbers2(numbers: List, filter: (Int) -> Boolean, square: (Int) -> Int): List { + return numbers.filter(filter).map(square) +} + println(processNumbers2(numbersList, { it % 2 != 0}, { it * it })) } \ No newline at end of file