Skip to content

Instantly share code, notes, and snippets.

@roberttaylor426
Created March 6, 2020 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roberttaylor426/94ebacf1f99500054e5eea99c06b8312 to your computer and use it in GitHub Desktop.
Save roberttaylor426/94ebacf1f99500054e5eea99c06b8312 to your computer and use it in GitHub Desktop.
Edenbridge Coders March 5th
import arrow.core.None
import arrow.core.Option
import arrow.core.Some
import org.junit.Test
class ListTest {
@Test
fun `a new list should be empty`() {
assert(length(createNewList()) == 0)
}
@Test
fun `adding an item to an empty list should return a list of length 1`() {
assert(length(add(1, createNewList())) == 1)
}
@Test
fun `adding two items to an empty list should return a list of length 2`() {
assert(length(add(3, add(1, createNewList()))) == 2)
}
@Test
fun `adding three items to an empty list should return a list of length 3`() {
assert(length(add(5, add(3, add(1, createNewList())))) == 3)
}
@Test
fun `given a list of 3 items should be able to get the first item`() {
val list = add(5, add(3, add(1, createNewList())))
assert(get(0, list) == Some(5))
}
@Test
fun `given a list of 3 items should be able to get the second item`() {
val list = add(5, add(3, add(1, createNewList())))
assert(get(1, list) == Some(3))
}
@Test
fun `given an empty list should return none when getting first item`() {
val list = createNewList()
assert(get(0, list) == None)
}
@Test
fun `should return none when fetching an item at an index less than zero`() {
val list = add(1, createNewList())
assert(get(-1, list) == None)
}
@Test
fun `given a list of one item should be able to remove the last`() {
val list = add(1, createNewList())
assert(remove(0, list) == createNewList())
}
@Test
fun `given a list of two items should be able to remove the last`() {
val list = add(3, add(1, createNewList()))
assert(remove(1, list) == add(3, createNewList()))
}
@Test
fun `given a list of three items should be able to remove the last`() {
val list = add(5, add(3, add(1, createNewList())))
assert(remove(2, list) == add(5, add(3, createNewList())))
}
@Test
fun `given a list of three items should be able to remove the second item`() {
val list = add(5, add(3, add(1, createNewList())))
assert(remove(1, list) == add(5, add(1, createNewList())))
}
}
fun remove(i: Int, list: List): List {
return when (list) {
List.Nil -> List.Nil
is List.Cons ->
if (i == 0) list.tail
else add(list.value, remove(i - 1, list.tail))
}
}
fun get(i: Int, list: List): Option<Int> {
return when (list) {
List.Nil -> None
is List.Cons ->
if (i == 0) Some(list.value)
else get(i - 1, list.tail)
}
}
sealed class List {
object Nil : List()
data class Cons(val value: Int, val tail: List) : List()
}
fun add(i: Int, list: List): List {
return List.Cons(i, list)
}
fun length(list: List): Int {
return when (list) {
List.Nil -> 0
is List.Cons -> 1 + length(list.tail)
}
}
fun createNewList(): List {
return List.Nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment