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
963c3cdf
Commit
963c3cdf
authored
1 year ago
by
Matt Bovel
Browse files
Options
Downloads
Patches
Plain Diff
Use a Scala file instead of worksheet
parent
d84371c3
No related branches found
No related tags found
No related merge requests found
Pipeline
#157542
failed
1 year ago
Stage: test
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/main/scala/ed/39368.scala
+24
-0
24 additions, 0 deletions
src/main/scala/ed/39368.scala
src/main/scala/ed/39368.worksheet.sc
+0
-22
0 additions, 22 deletions
src/main/scala/ed/39368.worksheet.sc
with
24 additions
and
22 deletions
src/main/scala/ed/39368.scala
0 → 100644
+
24
−
0
View file @
963c3cdf
// To demonstrate different ways of pattern matching, let's consider the
// following example case class and instance:
case
class
Person
(
name
:
String
,
age
:
Int
)
val
ada
=
Person
(
"Ada"
,
36
)
// Run using `sbt "runMain ed.patternMatching"`.
@main
def
patternMatching
=
// There are several ways to pattern match on the `ada` instance:
// 1. Pattern matching on the case class constructor using `Person(name, age)`.
// If the pattern matches, the value of `n` is bound `ada.name` field, and
// `a` is bound to the `ada.age` field.
ada
match
case
Person
(
n
,
a
)
=>
println
(
f
"$n is $a years old"
)
// 2. We can also check only that `ada` is of type `Person` without binding its
// fields by using a `:` pattern. The `:` pattern is used to check if a value is
// of a certain type.
ada
match
case
p
:
Person
=>
println
(
f
"${p.name} is ${p.age} years old"
)
// 3. If we want to both bind the fields and bind a value to the whole instance,
// we can use an `@` pattern.
ada
match
case
p
@
Person
(
n
,
a
)
=>
println
(
f
"${p.name} is ${a} years old"
)
This diff is collapsed.
Click to expand it.
src/main/scala/ed/39368.worksheet.sc
deleted
100644 → 0
+
0
−
22
View file @
d84371c3
//
To
demonstrate
different
ways
of
pattern
matching
,
let
'
s consider the
// following example case class and instance:
case class Person(name: String, age: Int)
val ada = Person(
"
Ada
"
, 36)
// There are several ways to pattern match on the `ada` instance:
// 1. Pattern matching on the case class constructor using `Person(name, age)`.
// If the pattern matches, the value of `n` is bound `ada.name` field, and
// `a` is bound to the `ada.age` field.
ada match
case Person(n, a) => s
"
$n is $a years old
"
// 2. We can also check only that `ada` is of type `Person` without binding its
// fields by using a `:` pattern. The `:` pattern is used to check if a value is
// of a certain type.
ada match
case p: Person => s
"
${p.name} is ${p.age} years old
"
// 3. If we want to both bind the fields and bind a value to the whole instance,
// we can use an `@` pattern.
ada match
case p @ Person(n, a) => s
"
${p.name} is ${a} years old
"
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