Fix, document and format existing demos
GenSet
1. Dropped The main change is that demos from lecture 3 now compile.
As GetSet
is deprecated, I first tried to rewrite a simplified version for Scala 3 that we could use:
GenSet
trait and conversions
GenSet
trait and conversionspackage common
import scala.collection.parallel.immutable.ParSet
import collection.parallel.CollectionConverters.IterableIsParallelizable
/** GenSet trait to replace the trait deprecated in Scala 2.13. */
sealed trait GenSet[A] extends Iterable[A]:
def +(elem: A): GenSet[A]
def -(elem: A): GenSet[A]
def contains(elem: A): Boolean
def subsetOf(that: GenSet[A]): Boolean
def iterator: Iterator[A]
def apply(elem: A) = contains(elem)
final case class SetGenSet[A](set: Set[A]) extends GenSet[A]:
def +(elem: A): GenSet[A] = set + elem
def -(elem: A): GenSet[A] = set - elem
def contains(elem: A): Boolean = set.contains(elem)
def subsetOf(that: GenSet[A]): Boolean = set.subsetOf(that)
def iterator: Iterator[A] = set.iterator
final case class ParSetGenSet[A](set: ParSet[A]) extends GenSet[A]:
def +(elem: A): GenSet[A] = set + elem
def -(elem: A): GenSet[A] = set - elem
def contains(elem: A): Boolean = set.contains(elem)
def subsetOf(that: GenSet[A]): Boolean = set.subsetOf(that)
def iterator: Iterator[A] = set.iterator
given fromParSetToGenSet[A]: Conversion[ParSet[A], GenSet[A]] = ParSetGenSet(_)
given fromSetToGenSet[A]: Conversion[Set[A], GenSet[A]] = SetGenSet(_)
given fromGenSetToSet[A]: Conversion[GenSet[A], Set[A]] = _ match
case SetGenSet[A](s) => s
case ParSetGenSet[A](s) => s.seq
given fromGenSetToParSet[A]: Conversion[GenSet[A], ParSet[A]] = _ match
case SetGenSet[A](s) => s.par.toSet
case ParSetGenSet[A](s) => s
But this one is buggy: there are unwanted conversations or snapshots that defeat the purpose of the demos. Furthermore, I think it's more confusing to have a fake trait than none at all.
Therefore, in a second iteration, I just dropped all usages of GenSet
. It's simpler and shorter, but preserve the points of the demos.
2. Added documentation and setup scalafmt
Also added a lot of comments, and setup scalafmt.