How it works...

At the very beginning, our function does a safety check for the track argument passed to it to verify whether it belongs to the current Album instance. If the given track is not found within the Album.tracks collection, the IllegalArgumentException exception is thrown.  Next, we create a sublist from the tracks property elements containing only the elements from the 0 index to the index of the track passed as the function parameter. This sublist is created using the take() operator. Then, we map each of the Track type elements to the Int type corresponding to the duration of the track. Finally, we apply the fold function, to sum the durationInSeconds property values of the consecutive Track elements. The fold function takes the initial argument responsible for initializing the internal accumulator variable holding the current state of the folding result.

In our case, we pass 0 as the initial value, which corresponds to the album start time. In the second argument passed to the fold function, we are defining how the accumulator should be updated with each of the consecutive durationInSeconds values.

Let's test the Album.getStartTime() function in action:

println(album.getStartTime(Track("Growing Up Beside You", 191)))
println(album.getStartTime(Track("Coming Up Easy", 292)))

The preceding code returns the following output:

468
176
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.104.73