From fa40b3d89b8de17f44589ad55bd440a757d0e723 Mon Sep 17 00:00:00 2001 From: Dalal Bakhsh Date: Thu, 13 Feb 2025 23:16:20 +0300 Subject: [PATCH] All Done with bonus --- src/CombiningLambdas.kt | 26 ++++++++++++++++++++++++++ src/Custom.kt | 8 ++++++++ src/FilterList.kt | 17 +++++++++++++++++ src/FilterNames.kt | 13 +++++++++++++ src/ListTransformation.kt | 8 ++++++++ src/Main.kt | 3 --- 6 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 src/CombiningLambdas.kt create mode 100644 src/Custom.kt create mode 100644 src/FilterList.kt create mode 100644 src/FilterNames.kt create mode 100644 src/ListTransformation.kt delete mode 100644 src/Main.kt diff --git a/src/CombiningLambdas.kt b/src/CombiningLambdas.kt new file mode 100644 index 0000000..68ae8ae --- /dev/null +++ b/src/CombiningLambdas.kt @@ -0,0 +1,26 @@ +fun main(){ + + val numbers = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) + + val processedNumbers = processNumbers(numbers) + println(processedNumbers) + + //The bonus + + val filterOdd: (Int) -> Boolean = { it % 2 != 0 } + val square: (Int) -> Int = { it * it } + + val processedNumbersBonus = processNumbersBonus(numbers, filterOdd, square) + + println(processedNumbersBonus) + +} + +fun processNumbers(numbers: List): List { + return numbers.filter { it % 2 != 0 }.map { it * it } +} + +//The bonus +fun processNumbersBonus(numbers: List, filter: (Int) -> Boolean, transform: (Int) -> Int): List { + return numbers.filter(filter).map(transform) +} \ No newline at end of file diff --git a/src/Custom.kt b/src/Custom.kt new file mode 100644 index 0000000..ca879f3 --- /dev/null +++ b/src/Custom.kt @@ -0,0 +1,8 @@ +fun main(){ + + val fruits = listOf("apple", "banana", "kiwi", "strawberry", "grape") + + val sortedWords = fruits.sortedByDescending { it.length } + + println(sortedWords) +} \ No newline at end of file diff --git a/src/FilterList.kt b/src/FilterList.kt new file mode 100644 index 0000000..ddc2d1d --- /dev/null +++ b/src/FilterList.kt @@ -0,0 +1,17 @@ +fun main(){ + + val greaterThanFive = customFilter(listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)) { it > 5 } + println(greaterThanFive) + + val evenNumbers = customFilter(listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)) { it % 2 == 0 } + println(evenNumbers) + + val multiplesOfThree = customFilter(listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)) { it % 3 == 0 } + println(multiplesOfThree) + +} + +fun customFilter(numbers: List, filter: (Int) -> Boolean): List{ + return numbers.filter(filter) +} + diff --git a/src/FilterNames.kt b/src/FilterNames.kt new file mode 100644 index 0000000..83a32f4 --- /dev/null +++ b/src/FilterNames.kt @@ -0,0 +1,13 @@ +fun main(){ + + val names = listOf("Alice", "Bob", "Amir", "Charlie", "Annie", "David") + + val filteredNamess = filterNames(names){it.startsWith("A")} + + println(filteredNamess) + +} + +fun filterNames(names: List, filterLogic: (String) -> Boolean): List{ + return names.filter(filterLogic) +} \ No newline at end of file diff --git a/src/ListTransformation.kt b/src/ListTransformation.kt new file mode 100644 index 0000000..ce3543b --- /dev/null +++ b/src/ListTransformation.kt @@ -0,0 +1,8 @@ +fun main() +{ + val numbers = listOf(1,2,3,4,5,6,7,8,9,10) + val doublenumbers = numbers.map { it * 2 } + + println(doublenumbers) + +} \ 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