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
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
Quentin Leblanc
scala2020
Commits
304f079d
Commit
304f079d
authored
5 years ago
by
Quentin Leblanc
Browse files
Options
Downloads
Patches
Plain Diff
TP6
parent
81aa9d8c
No related branches found
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
TP3/src/main/scala/6.collect.scala
+72
-0
72 additions, 0 deletions
TP3/src/main/scala/6.collect.scala
TP3/src/test/scala/6.collect_test.scala
+45
-0
45 additions, 0 deletions
TP3/src/test/scala/6.collect_test.scala
with
117 additions
and
0 deletions
TP3/src/main/scala/6.collect.scala
0 → 100644
+
72
−
0
View file @
304f079d
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
)
{
def
+
(
that
:
Duration
)
:
Duration
=
{
var
min
=
this
.
minutes
+
that
.
minutes
var
sec
=
(
this
.
seconds
+
that
.
seconds
)
if
(
sec
>
60
)
{
min
+=
1
sec
-=
60
}
Duration
(
min
,
sec
)
}
}
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
{
a
=>
a
.
artist
==
artist
}.
map
{
a
=>
a
.
title
}.
flatMap
(
tracks
)
}
/* Retourne la liste de tous les morceaux de moins de 4 minutes */
def
shortTracks
:
List
[
Track
]
=
{
tracks
.
flatMap
(
_
.
_2
.
filter
(
_
.
duration
.
minutes
<
4
)).
toList
}
/* Retourne les titres des morceaux antérieurs à une année */
def
titlesBefore
(
year
:
Int
)
:
List
[
String
]
=
{
albums
.
filter
(
_
.
year
<
year
).
map
(
_
.
title
).
flatMap
(
tracks
).
map
(
_
.
title
)
}
/* Calcule la durée totale de tous les morceaux disponibles.
REMARQUE: ont veut que les secondes soient inférieures à 60 mais les
minutes peuvent dépasser ce total.
*/
def
totalDuration
:
Duration
=
{
tracks
.
values
.
flatten
.
map
(
_
.
duration
).
reduce
(
_
+
_
)
}
}
This diff is collapsed.
Click to expand it.
TP3/src/test/scala/6.collect_test.scala
0 → 100644
+
45
−
0
View file @
304f079d
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