diff --git a/kotlin/list-ops/README.md b/kotlin/list-ops/README.md new file mode 100644 index 0000000..2c8fd43 --- /dev/null +++ b/kotlin/list-ops/README.md @@ -0,0 +1,20 @@ +# List Ops + +Implement basic list operations. + +In functional languages list operations like `length`, `map`, and +`reduce` are very common. Implement a series of basic list operations, +without using existing functions. + +## Hints + +The tests for this exercise require you to use extensions, a mechanism for adding new functionality to an existing class whose source you do not directly control without having to subclass it. To learn more about Kotlin's implementations of extensions, check out the [official documentation](https://kotlinlang.org/docs/reference/extensions.html#extensions). + +The `customFoldLeft` and `customFoldRight` methods are "fold" functions, which is a concept well-known in the functional programming world, but less so in the object-oriented one. If you'd like more background information, check out this [fold](https://en.wikipedia.org/wiki/Fold_(higher-order_function)) page. + + + + + +## Submitting Incomplete Solutions +It's possible to submit an incomplete solution so you can see how others have completed the exercise. diff --git a/kotlin/list-ops/src/main/kotlin/ListExt.kt b/kotlin/list-ops/src/main/kotlin/ListExt.kt index 7fb2615..efd277e 100644 --- a/kotlin/list-ops/src/main/kotlin/ListExt.kt +++ b/kotlin/list-ops/src/main/kotlin/ListExt.kt @@ -10,18 +10,28 @@ fun List.customFilter(inpFunc: (T) -> Boolean): List{ return emptyList() } -fun List.customSize() : Int{ - return 0 +fun List.customMap(inpFunc: (T) -> R): List{ + return emptyList() } +val List.customSize: Int + get() { + var size = 0 + for (i in this){ + size++ + } + return size + } + + fun List.customReverse(): List{ return emptyList() } -fun List.customFoldLeft(acc: T, inpFunc: (T, T) -> T): List{ - return emptyList() +fun List.customFoldLeft(acc: R, inpFunc: (R, T) -> R): R { + return inpFunc(acc, acc as T) } -fun List.customFoldRight(acc: T, inpFunc: (T, T) -> T): List{ - return emptyList() +fun List.customFoldRight(acc: R, inpFunc: (T, R) -> R): R{ + return inpFunc(acc as T, acc) } \ No newline at end of file diff --git a/kotlin/list-ops/src/test/kotlin/ListExtensionsTest.kt b/kotlin/list-ops/src/test/kotlin/ListExtensionsTest.kt new file mode 100644 index 0000000..565a8dc --- /dev/null +++ b/kotlin/list-ops/src/test/kotlin/ListExtensionsTest.kt @@ -0,0 +1,155 @@ +import org.junit.Ignore +import org.junit.Test +import kotlin.test.assertEquals + +/* + * version: 1.0.0 + */ +class ListExtensionsTest { + + @Test + fun testAppendingEmptyLists() { + assertEquals( + emptyList(), + emptyList().customAppend(emptyList())) + } + + @Ignore + @Test + fun testAppendingNonEmptyListOnEmptyList() { + assertEquals( + listOf('1', '2', '3', '4'), + emptyList().customAppend(listOf('1', '2', '3', '4'))) + } + + @Ignore + @Test + fun testAppendingNonEmptyListOnNonEmptyList() { + assertEquals( + listOf("1", "2", "2", "3", "4", "5"), + listOf("1", "2").customAppend(listOf("2", "3", "4", "5"))) + } + + @Ignore + @Test + fun testConcatOnEmptyListOfLists() { + assertEquals( + emptyList(), + emptyList>().customConcat()) + } + + @Ignore + @Test + fun testConcatOnNonEmptyListOfLists() { + assertEquals( + listOf('1', '2', '3', '4', '5', '6'), + listOf(listOf('1', '2'), listOf('3'), emptyList(), listOf('4', '5', '6')).customConcat()) + } + + @Ignore + @Test + fun testFilteringEmptyList() { + assertEquals( + emptyList(), + emptyList().customFilter { it % 2 == 1 }) + } + + @Ignore + @Test + fun testFilteringNonEmptyList() { + assertEquals( + listOf(1, 3, 5), + listOf(1, 2, 3, 5).customFilter { it % 2 == 1 }) + } + + @Test + fun testSizeOfEmptyList() { + assertEquals(0, emptyList().customSize) + } + + @Test + fun testSizeOfNonEmptyList() { + assertEquals(4, listOf("one", "two", "three", "four").customSize) + } + + @Ignore + @Test + fun testTransformingEmptyList() { + assertEquals( + emptyList(), + emptyList().customMap { it -> it + 1 }) + } + + @Ignore + @Test + fun testTransformingNonEmptyList() { + assertEquals( + listOf(2, 4, 6, 8), + listOf(1, 3, 5, 7).customMap { it -> it + 1 }) + } + + @Ignore + @Test + fun testFoldLeftOnEmptyList() { + assertEquals( + 2.0, + emptyList().customFoldLeft(2.0, Double::times)) + } + + @Ignore + @Test + fun testFoldLeftWithDirectionIndependentOperationOnNonEmptyList() { + assertEquals( + 15, + listOf(1, 2, 3, 4).customFoldLeft(5, Int::plus)) + } + + @Ignore + @Test + fun testFoldLeftWithDirectionDependentOperationOnNonEmptyList() { + assertEquals( + 0, + listOf(2, 5).customFoldLeft(5, Int::div)) + } + + @Ignore + @Test + fun testFoldRightOnEmptyList() { + assertEquals( + 2.0, + emptyList().customFoldRight(2.0, Double::times)) + } + + @Ignore + @Test + fun testFoldRightWithDirectionIndependentOperationOnNonEmptyList() { + assertEquals( + 15, + listOf(1, 2, 3, 4).customFoldRight(5, Int::plus)) + } + + @Ignore + @Test + fun testFoldRightWithDirectionDependentOperationOnNonEmptyList() { + assertEquals( + 2, + listOf(2, 5).customFoldRight(5, Int::div)) + } + + @Ignore + @Test + fun testReversingEmptyList() { + assertEquals( + emptyList(), + emptyList().customReverse()) + } + + @Ignore + @Test + fun testReversingNonEmptyList() { + assertEquals( + listOf('7', '5', '3', '1'), + listOf('1', '3', '5', '7').customReverse()) + } + +} \ No newline at end of file