diff --git a/base_tp/.gitignore b/base_tp/.gitignore index 382c6d7ac3e4555fa6290efb275ce587d48f5672..c87f0fb29563dbeb18c9c8f800a172542e53b599 100644 --- a/base_tp/.gitignore +++ b/base_tp/.gitignore @@ -1,3 +1,4 @@ *~ target/ +.metals/ \ No newline at end of file diff --git a/base_tp/src/collection/collect.scala b/base_tp/src/collection/collect.scala index acff4ffaafe6238477527850f788f1e0b077d9f1..587a89bef93fab32afa84d46d5835279938a80da 100644 --- a/base_tp/src/collection/collect.scala +++ b/base_tp/src/collection/collect.scala @@ -37,17 +37,19 @@ object Collect { /* Retourne la liste de morceaux associés à un artiste */ def tracksOf( artist: String ): List[Track] = { - albums.filter(_.artist == artist).flatMap( a => tracks(a.title)) + albums.flatMap( a => if (a.artist == artist) tracks(a.title) else List()) } /* Retourne la liste de tous les morceaux de moins de 4 minutes */ def shortTracks: List[Track] = { - tracks.flatMap(t => t._2).filter(t => t.duration.minutes < 4).toList + tracks.flatMap { + case (_,t) => t.filter(t => t.duration.minutes < 4) + }.toList } /* Retourne les titres des morceaux antérieurs à une année */ def titlesBefore( year: Int ): List[String] = { - albums.flatMap(a => if (a.year < year) tracks(a.title) else List()).map(_.title) + albums.flatMap(a => if (a.year < year) tracks(a.title).map(_.title) else List()) } /* Calcule la durée totale de tous les morceaux disponibles. @@ -55,12 +57,9 @@ object Collect { minutes peuvent dépasser ce total. */ def totalDuration: Duration = { - var m = 0 - var s = 0 - tracks.flatMap(_._2).foreach(t => { - m += t.duration.minutes - s += t.duration.seconds - }) - Duration(m + s/60, s % 60) + val s = tracks.flatMap(_._2.map(t => { + t.duration.seconds + t.duration.minutes*60 + })).sum + Duration(s/60, s % 60) } }