Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
CS-206 Parallelism and Concurrency 2020
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
Zad Abi Fadel
CS-206 Parallelism and Concurrency 2020
Commits
877510ef
Commit
877510ef
authored
5 years ago
by
Olivier Blanvillain
Browse files
Options
Downloads
Patches
Plain Diff
Add solutions-4.md
parent
aa432ea2
Branches
master
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
recitation-sessions/solutions-4.md
+61
-0
61 additions, 0 deletions
recitation-sessions/solutions-4.md
with
61 additions
and
0 deletions
recitation-sessions/solutions-4.md
0 → 100644
+
61
−
0
View file @
877510ef
# Exercise 1 : Implementing map and filter on Futures
```
scala
trait
Future
[
T
]
{
self
=>
def
map
[
S
](
f
:
T
=>
S
)
:
Future
[
S
]
=
new
Future
[
S
]
{
def
onComplete
(
callback
:
Try
[
S
]
=>
Unit
)
:
Unit
=
self
.
onComplete
{
case
Success
(
v
)
=>
callback
(
Success
(
f
(
v
)))
case
Failure
(
e
)
=>
callback
(
Failure
(
e
))
}
}
def
filter
(
f
:
T
=>
Boolean
)
:
Future
[
T
]
=
new
Future
[
T
]
{
def
onComplete
(
callback
:
Try
[
T
]
=>
Unit
)
:
Unit
=
self
.
onComplete
{
case
Success
(
v
)
=>
if
(
f
(
v
))
callback
(
Success
(
v
))
else
callback
(
Failure
(
new
NoSuchElementException
(
"..."
)))
case
Failure
(
e
)
=>
callback
(
Failure
(
e
))
}
}
}
```
# Exercise 2 : Coordinator / Worker
```
scala
class
Coordinator
extends
Actor
{
var
availableWorkers
:
List
[
ActorRef
]
=
Nil
var
pendingRequests
:
List
[
Request
]
=
Nil
def
receive
:
Receive
=
{
case
Ready
=>
if
(
pendingRequests
.
isEmpty
)
{
availableWorkers
=
availableWorkers
:+
sender
}
else
{
val
request
=
pendingRequests
.
head
pendingRequests
=
pendingRequests
.
tail
sender
!
request
}
case
request
:
Request
=>
availableWorkers
match
{
case
worker
::
rest
=>
worker
!
request
availableWorkers
=
rest
case
Nil
=>
pendingRequests
=
pendingRequests
:+
request
}
}
}
class
Worker
(
coordinator
:
ActorRef
)
extends
Actor
{
coordinator
!
Ready
def
receive
:
Receive
=
{
case
Request
(
f
)
=>
f
()
coordinator
!
Ready
}
}
```
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