diff --git a/lambda_collections/lambda_collections.iml b/lambda_collections/lambda_collections.iml new file mode 100644 index 0000000..4fd7bdb --- /dev/null +++ b/lambda_collections/lambda_collections.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/lambda_collections/src/combining_lambdas.kt b/lambda_collections/src/combining_lambdas.kt new file mode 100644 index 0000000..77f226b --- /dev/null +++ b/lambda_collections/src/combining_lambdas.kt @@ -0,0 +1,13 @@ +fun processNumbers(numbers: List, filter: (Int) -> Boolean, transform: (Int) -> Int): List { + // for the bonus question, it was a bit harder + // although by adding transform it modifies them by squaring or multiplying + //whereas previously it was just squaring them with it*it only + return numbers.filter(filter).map(transform) +} + +fun main() { + val numbers = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) + val result = processNumbers(numbers, { it % 2 != 0 }, { it * it }) + println(result) +} + diff --git a/lambda_collections/src/custom_filter.kt b/lambda_collections/src/custom_filter.kt new file mode 100644 index 0000000..44190e5 --- /dev/null +++ b/lambda_collections/src/custom_filter.kt @@ -0,0 +1,16 @@ +fun customFilter(numbers: List, filter: (Int) -> Boolean): List { + return numbers.filter(filter) +} + +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) +} + + diff --git a/lambda_collections/src/filtering_strings.kt b/lambda_collections/src/filtering_strings.kt new file mode 100644 index 0000000..4cc5159 --- /dev/null +++ b/lambda_collections/src/filtering_strings.kt @@ -0,0 +1,9 @@ +fun filterNames(names: List, filter: (String) -> Boolean): List { + return names.filter { filter(it) } +} + +fun main() { + val names = listOf("Alice", "Bob", "Amir", "Charlie", "Annie", "David") + val filteredNames = filterNames(names) { it.startsWith("A") } + println(filteredNames) +} diff --git a/lambda_collections/src/lambda_list.kt b/lambda_collections/src/lambda_list.kt new file mode 100644 index 0000000..e9fbee0 --- /dev/null +++ b/lambda_collections/src/lambda_list.kt @@ -0,0 +1,5 @@ +fun main() { + val numbers = (1..10).toList() + val doubledNumbers = numbers.map { it * 2 } + println(doubledNumbers) +} diff --git a/lambda_collections/src/sorting_lambda.kt b/lambda_collections/src/sorting_lambda.kt new file mode 100644 index 0000000..c32f56a --- /dev/null +++ b/lambda_collections/src/sorting_lambda.kt @@ -0,0 +1,5 @@ +fun main() { + val words = listOf("apple", "banana", "kiwi", "strawberry", "grape") + val sortedWords = words.sortedByDescending { it.length } + println(sortedWords) +}