diff --git a/src/main/scala/TopSongs/TopSongs.scala b/src/main/scala/TopSongs/TopSongs.scala index 29e1fb8..a34d455 100644 --- a/src/main/scala/TopSongs/TopSongs.scala +++ b/src/main/scala/TopSongs/TopSongs.scala @@ -122,6 +122,14 @@ case class TopSongs(songs: List[Song] = List()) { .toMap } + // This function return the song spent the most weeks on the chart + // It uses reduce to find the song with the maximum streak value + def longestStreak(): Song = { + songs.reduce((s1, s2) => { + if (s1.rank._1.streak.getOrElse(0) > s2.rank._1.streak.getOrElse(0)) s1 else s2 + }) + } + // This function returns the top N songs by weeks on chart // It sorts the songs by the streak value in descending order and takes the top N songs def topNSongsByWeeks(n: Int): List[Song] = {