From b43e3f87ff29c3be68c9379c0c20320eccf12869 Mon Sep 17 00:00:00 2001 From: fatmaalnaseeb <157986223+fatmaalnaseeb@users.noreply.github.com> Date: Thu, 13 Feb 2025 01:47:52 +0300 Subject: [PATCH] Add files via upload --- lambdaswithcollectioNEx.kt | 71 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 lambdaswithcollectioNEx.kt diff --git a/lambdaswithcollectioNEx.kt b/lambdaswithcollectioNEx.kt new file mode 100644 index 0000000..8cc266e --- /dev/null +++ b/lambdaswithcollectioNEx.kt @@ -0,0 +1,71 @@ +fun main(){ + + //1 + val listOfInt = listOf(1,2,3,4,5,6,7,8,9,10) + val doubleresult = listOfInt.map {it *2} + println(doubleresult) + + //2 + val names = listOf("Alice", "Bob", "Amir", "Charlie", "Annie", "David") + val filteredResult = filterNames(names) { it.startsWith("A") } + println(filteredResult) + + //3 + val words = listOf("apple", "banana", "kiwi", "strawberry", "grape") + val result = words.sortedByDescending{it.length} + println(result) + + //4 + fun customFilter(numbers : List , filter: (Int)-> Boolean) : List{ + return numbers.filter(filter) + } + val mylist = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) + + val numbersGreaterThanFifteen = customFilter(mylist) { it > 5 } + println(numbersGreaterThanFifteen) + + val evenNumbers = customFilter(mylist) {it % 2 ==0} + println(evenNumbers) + + val oddNumbers = customFilter(mylist) { it % 3 == 0} + println(oddNumbers) + + + //5 + fun processNumbers(num : List): List { + return num + .filter { it % 2 != 0 } + .map { it * it } + } + val mylist2 = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) + val resultOfProcessNumbers = processNumbers(mylist2) + println(resultOfProcessNumbers) + + + + + + + + + + + + + + + + +} +//2 +fun filterNames (names: List, condition: (String) -> Boolean): List { + return names.filter(condition) + +} + + + + + + +