Presentation Thursday, Aug 12, 2021
This commit is contained in:
parent
d59fbba056
commit
e1210833d5
@ -1,6 +1,21 @@
|
|||||||
package conversions
|
package conversions
|
||||||
|
|
||||||
|
import data.{Cons, LispList, Nil}
|
||||||
|
|
||||||
|
import scala.annotation.tailrec
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implicit conversions for LispList.
|
* 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,6 +1,20 @@
|
|||||||
package extensions
|
package extensions
|
||||||
|
|
||||||
|
import data.{Cons, LispList, Nil}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extension methods for LispList.
|
* 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package conversions
|
package conversions
|
||||||
|
|
||||||
import data.{Cons, Nil}
|
import data.{Cons, LispList, Nil}
|
||||||
import org.scalatest.matchers.should.Matchers
|
import org.scalatest.matchers.should.Matchers
|
||||||
import org.scalatest.wordspec.AnyWordSpecLike
|
import org.scalatest.wordspec.AnyWordSpecLike
|
||||||
|
|
||||||
@ -14,30 +14,19 @@ class LispListSpec extends AnyWordSpecLike with Matchers {
|
|||||||
* Exercise: Implicit Conversions
|
* Exercise: Implicit Conversions
|
||||||
*/
|
*/
|
||||||
"convert types" which {
|
"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 lengthAsList[A](list: List[A]): Int = list.length
|
||||||
def lengthAsSeq[A](seq: Seq[A]): Int = seq.length
|
|
||||||
|
|
||||||
"become a List" in {
|
"become a List" in {
|
||||||
pending
|
import conversions.lisplist._
|
||||||
//lengthAsList(lispList) should eq(3)
|
lengthAsList(lispList) shouldBe 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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
"become a Seq, but loudly" in {
|
"become a Seq, but loudly" in {
|
||||||
val out = new ByteArrayOutputStream()
|
val out = new ByteArrayOutputStream()
|
||||||
Console.withOut(out) {
|
Console.withOut(out) {
|
||||||
pending
|
pending
|
||||||
//lengthAsSeq(lispList) should eq(3)
|
//lengthAsSeq(lispList) shouldBe 3
|
||||||
}
|
}
|
||||||
out.toString should contain("THREE OF THEM!")
|
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)))
|
val lispList = Cons(1, Cons(2, Cons(3, Nil)))
|
||||||
|
|
||||||
"asList" can {
|
"asList" can {
|
||||||
|
import extensions.lisplist._
|
||||||
|
|
||||||
"explicitly become a List" in {
|
"explicitly become a List" in {
|
||||||
pending
|
lispList.asList shouldBe List(1, 2, 3)
|
||||||
//lispList.asList should eq(List(1, 2, 3))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"asSeq" can {
|
"asSeq" can {
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package extensions
|
package extensions
|
||||||
|
|
||||||
import data.{Cons, Nil}
|
import data.{Cons, Nil}
|
||||||
import org.scalatest.matchers.must.Matchers
|
import org.scalatest.matchers.should.Matchers
|
||||||
import org.scalatest.wordspec.AnyWordSpecLike
|
import org.scalatest.wordspec.AnyWordSpecLike
|
||||||
|
|
||||||
class LispListSpec extends AnyWordSpecLike with Matchers {
|
class LispListSpec extends AnyWordSpecLike with Matchers {
|
||||||
@ -13,14 +13,15 @@ class LispListSpec extends AnyWordSpecLike with Matchers {
|
|||||||
*/
|
*/
|
||||||
"length" which {
|
"length" which {
|
||||||
"returns 0 if the list is Nil" in {
|
"returns 0 if the list is Nil" in {
|
||||||
|
import extensions.lisplist._
|
||||||
|
|
||||||
val lispList = Nil
|
val lispList = Nil
|
||||||
pending
|
lispList.length shouldBe 0
|
||||||
//lispList.length should eq(0)
|
|
||||||
}
|
}
|
||||||
"returns the length of the list" in {
|
"returns the length of the list" in {
|
||||||
|
import conversions.lisplist._
|
||||||
val lispList = Cons(1, Cons(2, Cons(3, Nil)))
|
val lispList = Cons(1, Cons(2, Cons(3, Nil)))
|
||||||
pending
|
lispList.map(_ * 2) shouldBe 0
|
||||||
//lispList.length should eq(3)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user