From ae6c65f11643875ed80e7c90c56828fe8c94ede7 Mon Sep 17 00:00:00 2001 From: Alexey Alekhin Date: Tue, 3 Oct 2017 17:57:34 +0200 Subject: [PATCH 1/2] Fixed AnyKList.fromList implementation (reversing list once) --- src/main/scala/cosas/klists/klists.scala | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/main/scala/cosas/klists/klists.scala b/src/main/scala/cosas/klists/klists.scala index 1a1015c..c137de1 100644 --- a/src/main/scala/cosas/klists/klists.scala +++ b/src/main/scala/cosas/klists/klists.scala @@ -64,16 +64,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 + } } From abf5a1157c8ab8f3040acf1e147fd035ad24c4d5 Mon Sep 17 00:00:00 2001 From: Alexey Alekhin Date: Tue, 3 Oct 2017 17:57:51 +0200 Subject: [PATCH 2/2] Added tests for AnyKList.fromList --- src/test/scala/cosas/KListsTests.scala | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/test/scala/cosas/KListsTests.scala b/src/test/scala/cosas/KListsTests.scala index f583b84..26c5fc0 100644 --- a/src/test/scala/cosas/KListsTests.scala +++ b/src/test/scala/cosas/KListsTests.scala @@ -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 } } @@ -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 {