Skip to content

Commit cecaa6f

Browse files
committed
While I was playing around with the code to familiarize myself with the
project, I tried to clean up a few things. -
1 parent ed7ebf9 commit cecaa6f

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

src/main/scala/org/scalaexercises/exercises/Exercises.scala

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import java.net.URLClassLoader
1313
import java.io.File
1414

1515
import cats.data.Xor
16+
import cats.std.list._
17+
import cats.syntax.monadCombine._
1618
import org.clapper.classutil.ClassFinder
1719
import org.scalaexercises.evaluator.Dependency
1820

@@ -40,24 +42,18 @@ object Exercises {
4042
loop(cl, Nil)
4143
}
4244

43-
def discoverLibraries(cl: ClassLoader = classOf[Exercise].getClassLoader) = {
45+
def discoverLibraries(cl: ClassLoader = classOf[Exercise].getClassLoader): (List[String], List[Library]) = {
4446
val classNames: List[String] = subclassesOf[Library](cl)
4547

46-
val (errors, libraries) = classNames.foldLeft((Nil: List[String], Nil: List[Library])) { (acc, name)
47-
val loadedLibrary = for {
48+
val errorsAndLibraries = classNames.map { name
49+
for {
4850
loadedClass guard(Class.forName(name, true, cl), s"$name not found")
4951
loadedObject guard(loadedClass.getField("MODULE$").get(null), s"$name must be defined as an object")
5052
loadedLibrary guard(loadedObject.asInstanceOf[Library], s"$name must extend Library")
5153
} yield loadedLibrary
52-
53-
// until a bifoldable exists in Cats...
54-
loadedLibrary match {
55-
case Xor.Right(c) (acc._1, c :: acc._2)
56-
case Xor.Left(e) (e :: acc._1, acc._2)
57-
}
5854
}
5955

60-
(errors, libraries)
56+
errorsAndLibraries.separate
6157
}
6258

6359
private def guard[A](f: A, message: String) =

0 commit comments

Comments
 (0)