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..e5da259 --- /dev/null +++ b/lamda collections/src/Main.kt @@ -0,0 +1,44 @@ +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)) +// 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