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
Sankalp Gambhir
CS-206 Demos
Commits
bbe14e29
Commit
bbe14e29
authored
1 year ago
by
Matt Bovel
Browse files
Options
Downloads
Patches
Plain Diff
Add become example
parent
5ca22982
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
.gitlab-ci.yml
+1
-1
1 addition, 1 deletion
.gitlab-ci.yml
src/main/scala/lecture13/Become.scala
+49
-0
49 additions, 0 deletions
src/main/scala/lecture13/Become.scala
with
50 additions
and
1 deletion
.gitlab-ci.yml
+
1
−
1
View file @
bbe14e29
...
...
@@ -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; 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;
scalafmtCheck; Test / scalafmtCheck"
This diff is collapsed.
Click to expand it.
src/main/scala/lecture13/Become.scala
0 → 100644
+
49
−
0
View file @
bbe14e29
package
lecture13
import
akka.actor.
{
Actor
,
ActorContext
,
ActorRef
,
ActorSystem
,
Props
}
import
akka.event.LoggingReceive
import
akka.testkit.
{
ImplicitSender
,
TestKit
}
class
CounterRight
extends
Actor
:
def
counter
(
n:
Int
)
:
Receive
=
{
case
"increment"
=>
context
.
become
(
counter
(
n
+
1
),
discardOld
=
false
)
// ^^^^^^^^^^^^^^^^^^
// This is needed.
case
"get"
=>
sender
()
!
n
case
"decrement"
=>
if
n
!=
0
then
context
.
unbecome
()
}
def
receive
=
counter
(
0
)
class
CounterWrong
extends
Actor
:
def
counter
(
n:
Int
)
:
Receive
=
{
case
"increment"
=>
context
.
become
(
counter
(
n
+
1
))
case
"get"
=>
sender
()
!
n
case
"decrement"
=>
if
n
!=
0
then
context
.
unbecome
()
}
def
receive
=
counter
(
0
)
@main
def
becomeDemo
()
=
new
TestKit
(
ActorSystem
(
"DebugSystem"
))
with
ImplicitSender
:
try
val
counter
=
system
.
actorOf
(
Props
(
CounterRight
()),
"counter"
)
counter
!
"increment"
counter
!
"get"
expectMsg
(
1
)
counter
!
"increment"
counter
!
"get"
expectMsg
(
2
)
counter
!
"decrement"
counter
!
"get"
expectMsg
(
1
)
// This is wrong if we use CounterWrong because it doesn't set the
// discardOld parameter to false. Therefore, it will not remember the
// previous state and reset the behavior to the initial one, which is
// `counter(0)`.
counter
!
"decrement"
counter
!
"get"
expectMsg
(
0
)
finally
shutdown
(
system
)
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