Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
CS-206 Demos
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
LARA
CS-206 Demos
Commits
c122c2da
Commit
c122c2da
authored
1 year ago
by
Matt Bovel
Browse files
Options
Downloads
Patches
Plain Diff
Add concpar20final03
parent
7f492db6
No related branches found
No related tags found
No related merge requests found
Pipeline
#157720
passed
1 year ago
Stage: test
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
.gitlab-ci.yml
+1
-1
1 addition, 1 deletion
.gitlab-ci.yml
src/main/scala/concpar20final03/Futures.scala
+61
-0
61 additions, 0 deletions
src/main/scala/concpar20final03/Futures.scala
with
62 additions
and
1 deletion
.gitlab-ci.yml
+
1
−
1
View file @
c122c2da
...
...
@@ -21,4 +21,4 @@ test:
tags
:
-
cs320
script
:
-
sbt "runMain lecture1.javaThreads; runMain lecture1.scalaThreadWrapper; runMain lecture1.ExampleThread; runMain lecture3.intersectionWrong; runMain lecture3.intersectionCorrect; runMain lecture3.intersectionNoSideEffect; runMain lecture3.parallelGraphContraction; runMain lecture3.parallelGraphContractionCorrect; runMain midterm22.mock1; runMain midterm22.part3; runMain ed.patternMatching; runMain lecture13.askPatternDemo; test; runMain lecture13.becomeDemo; runMain ed.futuresForTranslation; scalafmtCheck; Test / scalafmtCheck"
-
sbt "runMain lecture1.javaThreads; runMain lecture1.scalaThreadWrapper; runMain lecture1.ExampleThread; runMain lecture3.intersectionWrong; runMain lecture3.intersectionCorrect; runMain lecture3.intersectionNoSideEffect; runMain lecture3.parallelGraphContraction; runMain lecture3.parallelGraphContractionCorrect; runMain midterm22.mock1; runMain midterm22.part3; runMain ed.patternMatching; runMain lecture13.askPatternDemo; test; runMain lecture13.becomeDemo; runMain ed.futuresForTranslation;
runMain concpar20final03.concpar20final03;
scalafmtCheck; Test / scalafmtCheck"
This diff is collapsed.
Click to expand it.
src/main/scala/concpar20final03/Futures.scala
0 → 100644
+
61
−
0
View file @
c122c2da
package
concpar20final03
import
scala.concurrent.
{
Await
,
Future
}
import
scala.concurrent.duration.Duration
import
scala.concurrent.ExecutionContext.Implicits.global
import
scala.util.
{
Failure
,
Success
}
def
sequence1
[
A
](
fs
:
List
[
Future
[
A
]])
:
Future
[
List
[
A
]]
=
fs
match
case
f
::
fs
=>
for
x
<-
f
xs
<-
sequence1
(
fs
)
yield
x
::
xs
case
Nil
=>
Future
.
successful
(
Nil
)
def
sequence2
[
A
](
fs
:
List
[
Future
[
A
]])
:
Future
[
List
[
A
]]
=
fs
match
case
f
::
fs
=>
f
.
flatMap
(
x
=>
sequence2
(
fs
).
map
(
xs
=>
x
::
xs
))
case
Nil
=>
Future
.
successful
(
Nil
)
def
sequence3
[
A
](
fs
:
List
[
Future
[
A
]])
:
Future
[
List
[
A
]]
=
fs
match
case
f
::
fs
=>
f
.
transformWith
{
case
Failure
(
e
)
=>
Future
.
failed
(
e
)
case
Success
(
value
)
=>
sequence3
(
fs
).
transform
{
case
res
:
Failure
[
List
[
A
]]
=>
res
case
Success
(
values
)
=>
Success
(
value
::
values
)
}
}
case
Nil
=>
Future
.
successful
(
Nil
)
def
sequence4
[
A
](
fs
:
List
[
Future
[
A
]])
:
Future
[
List
[
A
]]
=
fs
match
case
f
::
fs
=>
val
fsFuture
:
Future
[
List
[
A
]]
=
sequence4
(
fs
)
f
.
zip
(
fsFuture
).
map
((
fValue
,
fsValue
)
=>
fValue
::
fsValue
)
case
Nil
=>
Future
.
successful
(
Nil
)
@main
def
concpar20final03
=
val
xs
=
List
(
Future
{
1
},
Future
{
2
},
Future
{
3
})
val
ys
=
List
(
Future
{
1
},
Future
.
failed
(
Error
(
"BOOM"
)),
Future
{
3
})
println
(
Await
.
result
(
sequence1
(
xs
),
Duration
.
Inf
))
// println(Await.result(sequence1(xs), Duration.Inf)) // Throws exception
println
(
Await
.
result
(
sequence2
(
xs
),
Duration
.
Inf
))
// println(Await.result(sequence2(ys), Duration.Inf)) // Throws exception
println
(
Await
.
result
(
sequence3
(
xs
),
Duration
.
Inf
))
// println(Await.result(sequence3(ys), Duration.Inf)) // Throws exception
println
(
Await
.
result
(
sequence4
(
xs
),
Duration
.
Inf
))
// println(Await.result(sequence4(ys), Duration.Inf)) // Throws exception
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