Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
scala2020
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
joel.vonderwe
scala2020
Commits
aa4de7d7
Commit
aa4de7d7
authored
5 years ago
by
joel.vonderwe
Browse files
Options
Downloads
Patches
Plain Diff
Collections exos
parent
d8643d22
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
base_tp/src/collection/collect.scala
+66
-0
66 additions, 0 deletions
base_tp/src/collection/collect.scala
base_tp/test/collectTest.scala
+45
-0
45 additions, 0 deletions
base_tp/test/collectTest.scala
with
111 additions
and
0 deletions
base_tp/src/collection/collect.scala
0 → 100644
+
66
−
0
View file @
aa4de7d7
package
ch.hepia.tpscala
/* Implémentez les fonctions suivantes.
*/
object
Collect
{
case
class
Album
(
title
:
String
,
artist
:
String
,
year
:
Int
)
case
class
Duration
(
minutes
:
Int
,
seconds
:
Int
)
case
class
Track
(
title
:
String
,
duration
:
Duration
)
val
albums
=
List
(
Album
(
"Mit Gas"
,
"Tomahawk"
,
2003
),
Album
(
"Pork Soda"
,
"Primus"
,
1993
),
Album
(
"Brown Album"
,
"Primus"
,
1997
),
Album
(
"Distraction Pieces"
,
"Scroobius Pip"
,
2011
)
)
val
tracks
=
Map
(
"Mit Gas"
->
List
(
Track
(
"Mayday"
,
Duration
(
3
,
32
)
)
),
"Pork Soda"
->
List
(
Track
(
"DMV"
,
Duration
(
4
,
58
)
),
Track
(
"Mr. Krinkle"
,
Duration
(
5
,
27
)
)
),
"Brown Album"
->
List
(
Track
(
"Fisticuffs"
,
Duration
(
4
,
25
)
),
Track
(
"Camelback Cinema"
,
Duration
(
4
,
0
)
),
Track
(
"Kalamazoo"
,
Duration
(
3
,
31
)
)
),
"Distraction Pieces"
->
List
(
Track
(
"Let 'Em Come"
,
Duration
(
4
,
25
)
),
Track
(
"Domestic Silence"
,
Duration
(
3
,
58
)
)
)
)
/* Retourne la liste de morceaux associés à un artiste */
def
tracksOf
(
artist
:
String
)
:
List
[
Track
]
=
{
albums
.
filter
(
_
.
artist
==
artist
).
flatMap
(
a
=>
tracks
(
a
.
title
))
}
/* 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
}
/* 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
)
}
/* Calcule la durée totale de tous les morceaux disponibles.
REMARQUE: on veut que les secondes soient inférieures à 60 mais les
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
)
}
}
This diff is collapsed.
Click to expand it.
base_tp/test/collectTest.scala
0 → 100644
+
45
−
0
View file @
aa4de7d7
package
ch.hepia.tpscala
import
org.scalatest.funsuite.AnyFunSuite
import
Collect._
class
Collect6Suite
extends
AnyFunSuite
{
test
(
"tracksOf"
)
{
assert
(
tracksOf
(
"Justin Bieber"
).
isEmpty
)
assert
(
tracksOf
(
"Tomahawk"
)
==
List
(
Track
(
"Mayday"
,
Duration
(
3
,
32
)
)
)
)
assert
(
tracksOf
(
"Primus"
).
size
==
5
)
}
test
(
"shortTracks"
)
{
assert
(
shortTracks
.
toSet
==
Set
(
Track
(
"Mayday"
,
Duration
(
3
,
32
)
),
Track
(
"Kalamazoo"
,
Duration
(
3
,
31
)
),
Track
(
"Domestic Silence"
,
Duration
(
3
,
58
)
)
)
)
}
test
(
"titlesBefore"
)
{
assert
(
titlesBefore
(
1928
).
size
==
0
)
assert
(
titlesBefore
(
2020
).
size
==
8
)
assert
(
titlesBefore
(
2000
).
toSet
==
Set
(
"DMV"
,
"Mr. Krinkle"
,
"Fisticuffs"
,
"Camelback Cinema"
,
"Kalamazoo"
)
)
}
test
(
"totalDuration"
)
{
assert
(
totalDuration
==
Duration
(
34
,
16
)
)
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment