Presentation Thursday, Aug 12, 2021

This commit is contained in:
Logan McGrath 2021-08-12 15:04:34 -07:00
parent d59fbba056
commit e1210833d5
4 changed files with 45 additions and 25 deletions

View File

@ -1,6 +1,21 @@
package conversions
import data.{Cons, LispList, Nil}
import scala.annotation.tailrec
/**
* Implicit conversions for LispList.
*/
package object lisplist {}
package object lisplist {
implicit def lispListToList[A](lispList: LispList[A]): List[A] = {
@tailrec
def go(acc: List[A], l: LispList[A]): List[A] =
l match {
case Cons(car, cdr) => go(car +: acc, cdr)
case Nil => acc
}
go(List(), lispList).reverse
}
}

View File

@ -1,6 +1,20 @@
package extensions
import data.{Cons, LispList, Nil}
/**
* Extension methods for LispList.
*/
package object lisplist {}
package object lisplist {
implicit class LispListOps[A](val lispList: LispList[A]) extends AnyVal {
import conversions.lisplist._
def asList: List[A] = lispListToList(lispList)
def length: Int =
lispList match {
case Cons(_, cdr) => 1 + LispListOps(cdr).length
case Nil => 0
}
}
}

View File

@ -1,6 +1,6 @@
package conversions
import data.{Cons, Nil}
import data.{Cons, LispList, Nil}
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike
@ -14,30 +14,19 @@ class LispListSpec extends AnyWordSpecLike with Matchers {
* Exercise: Implicit Conversions
*/
"convert types" which {
val lispList = Cons(1, Cons(2, Cons(3, Nil)))
val lispList: LispList[Int] = Cons(1, Cons(2, Cons(3, Nil)))
def lengthAsList[A](list: List[A]): Int = list.length
def lengthAsSeq[A](seq: Seq[A]): Int = seq.length
"become a List" in {
pending
//lengthAsList(lispList) should eq(3)
}
"become a Seq" in {
pending
//lengthAsSeq(lispList) should eq(3)
}
"throw an exception if getting length as a Seq but actually is a List" in {
pending
//lengthAsSeq(lispList) should eq(3)
import conversions.lisplist._
lengthAsList(lispList) shouldBe 3
}
"become a Seq, but loudly" in {
val out = new ByteArrayOutputStream()
Console.withOut(out) {
pending
//lengthAsSeq(lispList) should eq(3)
//lengthAsSeq(lispList) shouldBe 3
}
out.toString should contain("THREE OF THEM!")
}
@ -50,9 +39,10 @@ class LispListSpec extends AnyWordSpecLike with Matchers {
val lispList = Cons(1, Cons(2, Cons(3, Nil)))
"asList" can {
import extensions.lisplist._
"explicitly become a List" in {
pending
//lispList.asList should eq(List(1, 2, 3))
lispList.asList shouldBe List(1, 2, 3)
}
}
"asSeq" can {

View File

@ -1,7 +1,7 @@
package extensions
import data.{Cons, Nil}
import org.scalatest.matchers.must.Matchers
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike
class LispListSpec extends AnyWordSpecLike with Matchers {
@ -13,14 +13,15 @@ class LispListSpec extends AnyWordSpecLike with Matchers {
*/
"length" which {
"returns 0 if the list is Nil" in {
import extensions.lisplist._
val lispList = Nil
pending
//lispList.length should eq(0)
lispList.length shouldBe 0
}
"returns the length of the list" in {
import conversions.lisplist._
val lispList = Cons(1, Cons(2, Cons(3, Nil)))
pending
//lispList.length should eq(3)
lispList.map(_ * 2) shouldBe 0
}
}