Skip to content
Snippets Groups Projects

Fix, document and format existing demos

Merged Matt Bovel requested to merge mb into main

1. Dropped GenSet

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

package 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.

Merge request reports

Loading
Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
Please register or sign in to reply
Loading