diff --git a/.idea/jpa-buddy.xml b/.idea/jpa-buddy.xml new file mode 100644 index 0000000..966d5f5 --- /dev/null +++ b/.idea/jpa-buddy.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml index 8226e53..36b41ec 100644 --- a/.idea/kotlinc.xml +++ b/.idea/kotlinc.xml @@ -1,10 +1,6 @@ - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 03f397c..f8ed9c1 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -3,4 +3,7 @@ + + \ No newline at end of file diff --git a/src/Main.kt b/src/Main.kt index a7a5e00..d788a8e 100644 --- a/src/Main.kt +++ b/src/Main.kt @@ -1,3 +1,85 @@ +import javax.swing.text.StyledEditorKit.BoldAction + fun main() { - println("Hello World!") + + //Part 1 + val numbers = mutableListOf(1,2,3,4,5,6,7,8,9,10) + val numbersDoubled = numbers.map{it*2}; + println(numbersDoubled) + + //Part 2 + val names = mutableListOf("Alice", "Bob", "Amir", "Charlie", "Annie", "David"); + println(filterWithA("alice")) + println(filterNames(names,filterWithA)) + + //Part 3 + val fruits = mutableListOf("apple", "banana", "kiwi", "strawberry", "grape") + println(sortedList(fruits,findWordLength)) + + //Part 4 + println(customFilter(listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)) { it > 5 }) + //or + println(customFilter(listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),biggerThanFive)) + + + println(customFilter(listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)) { it % 2 == 0 }) + //or + println(customFilter(listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),isEven)) + + + println(customFilter(listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)) { it % 3 == 0 }) + //or + println(customFilter(listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),multiplesOfThree)) + + + //Part 5 + println(processNumbers(listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),lambdaNotEven,lambdaSquare)); + + +} + +//For part 2 +fun filterNames(list:List, filteringFun: (String)->(Boolean)): List{ + return list.filter{filteringFun(it)} + +} + + +val filterWithA: (String) -> Boolean = { + it -> it.startsWith("A") +} + + +//for part 3 +fun sortedList(list:List, filteringFun: (String) -> Int): List{ + return list.sortedByDescending(filteringFun) +} +val findWordLength: (String) -> Int = { + it -> it.length +} + +//for part 4 +fun customFilter(list: List, filteringFun: (Int) -> Boolean): List{ + return list.filter(filteringFun) +} +val biggerThanFive: (Int) -> Boolean = { + it -> it > 5 +} +val isEven: (Int) -> Boolean = { + it -> it % 2 == 0 +} +val multiplesOfThree: (Int) -> Boolean = { + it -> it % 3 == 0 +} + +//for part 5 +//takes two lambdas funs as paramters with List of int and returns a list of int +fun processNumbers(list:List,lambdaNotEven: (Int)->Boolean, lambdaSquare: (Int) -> Int): List{ + return list.filter(lambdaNotEven).map(lambdaSquare); +} +val lambdaNotEven: (Int) -> Boolean = { + it -> it % 2 != 0 +} +val lambdaSquare: (Int) -> Int = { + it -> it * it } \ No newline at end of file