Skip to content

Commit

Permalink
Merge pull request #123 from ohnosequences/123/anyklist-fromlist-orde…
Browse files Browse the repository at this point in the history
…r-bug

Bug in AnyKList.fromList
  • Loading branch information
laughedelic authored Oct 4, 2017
2 parents bbeccbe + abf5a11 commit bc6f474
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/main/scala/cosas/klists/klists.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,16 @@ case object AnyKList {
: syntax.KListSyntax[L] =
syntax.KListSyntax[L](l)

def fromList[X](l: List[X]): AnyKList { type Bound = X } = fromList_rec(l,*[X])

@scala.annotation.tailrec
private def fromList_rec[X](
list: List[X],
acc: AnyKList.withBound[X]
)
: AnyKList.withBound[X] = list.reverse match {
case x :: xs => fromList_rec[X](xs, x :: acc)
case Nil => acc
}
def fromList[X](l: List[X]): AnyKList { type Bound = X } =
fromList_rec(l.reverse, *[X])

@scala.annotation.tailrec
private def fromList_rec[X](
reversedList: List[X],
acc: AnyKList.withBound[X]
): AnyKList.withBound[X] = reversedList match {
case x :: xs => fromList_rec[X](xs, x :: acc)
case Nil => acc
}

}
22 changes: 22 additions & 0 deletions src/test/scala/cosas/KListsTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ case object KListTestsContext {

case object A0 extends A { val boo = true }
case object A1 extends A { val boo = false }
case object A2 extends A { val boo = true }

}

Expand Down Expand Up @@ -62,6 +63,27 @@ class KListTests extends org.scalatest.FunSuite {
assert{ foo(oh) === true }
}

test("can convert normal Lists to KLists and back") {

val as = A0 :: A1 :: A2 :: Nil
val aks = (A0 :: A1 :: A2 :: *[A])

// list -> klist
assert { AnyKList.fromList(as) === aks }

// list -> klist -> list
assert { AnyKList.fromList(as).asList === as }

// klist -> list -> klist
assert { AnyKList.fromList(aks.asList) === aks }

// ensuring correct order:
assert {
AnyKList.fromList(1 :: 2 :: 3 :: 4 :: Nil) ===
(1 :: 2 :: 3 :: 4 :: *[Int])
}
}

test("can convert KLists to lists of their bound") {

assert {
Expand Down

0 comments on commit bc6f474

Please sign in to comment.