Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
short-exercises
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
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
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
Tilman Voit
short-exercises
Commits
8a9bca1f
Commit
8a9bca1f
authored
2 years ago
by
Alexander Gehrke
Browse files
Options
Downloads
Patches
Plain Diff
[lec02] Scala 3 migration
parent
2ab69e12
Branches
readerwriter
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
src/main/scala/datastructures/List.scala
+16
-4
16 additions, 4 deletions
src/main/scala/datastructures/List.scala
src/test/scala/datastructures/ListSpec.scala
+3
-3
3 additions, 3 deletions
src/test/scala/datastructures/ListSpec.scala
with
19 additions
and
7 deletions
src/main/scala/datastructures/List.scala
+
16
−
4
View file @
8a9bca1f
package
datastructures
sealed
trait
List
[
+A
]
:
def
head
=
this
match
enum
List
[
+A
]
:
case
Nil
case
Cons
(
_head
:
A
,
_tail
:
List
[
A
])
def
head
:
A
=
this
match
case
Nil
=>
sys
.
error
(
"head of empty list"
)
case
Cons
(
a
,
_
)
=>
a
/** removes the first element of a list and returns the rest */
def
tail
:
List
[
A
]
=
???
/** returns all but the last element of a list */
def
init
:
List
[
A
]
=
???
/** replaces the first element of a list */
def
setHead
[
AA
>:
A
](
head
:
AA
)
:
List
[
AA
]
=
???
/** recurses through the list, combining elements with the given function
* Uncomment the annotation to enable checking for tail-recursiveness */
//@annotation.tailrec
def
foldLeft
[
B
](
z
:
B
)(
f
:
(
B
,
A
)
=>
B
)
:
B
=
???
case
object
Nil
extends
List
[
Nothing
]
case
class
Cons
[
+A
](
elem
:
A
,
rest
:
List
[
A
])
extends
List
[
A
]
object
List
:
/**
construct
a
list
by
passing
elements
*
*
Remember
:
`apply`
makes
the
object
behave
like
a
function
,
*
you
can
call
it
as
`List(elem1, elem2,...)`
**/
def
apply
[
A
](
as
:
A*
)
:
List
[
A
]
=
if
as
.
isEmpty
then
Nil
else
Cons
(
as
.
head
,
apply
(
as
.
tail
:
_
*
))
This diff is collapsed.
Click to expand it.
src/test/scala/datastructures/ListSpec.scala
+
3
−
3
View file @
8a9bca1f
...
...
@@ -12,13 +12,13 @@ class ListSpec extends AnyFlatSpec with Matchers with AppendedClues with Pending
"A list"
should
"have a tail function that removes the first element"
in
{
numbers
.
tail
shouldBe
List
(
2
,
3
,
4
,
5
)
single
.
tail
shouldBe
Nil
single
.
tail
shouldBe
List
.
Nil
a
[
RuntimeException
]
should
be
thrownBy
List
().
tail
}
it
should
"have a init function that removes the last element"
in
{
numbers
.
init
shouldBe
List
(
1
,
2
,
3
,
4
)
single
.
init
shouldBe
Nil
single
.
init
shouldBe
List
.
Nil
a
[
RuntimeException
]
should
be
thrownBy
List
().
init
}
...
...
@@ -31,4 +31,4 @@ class ListSpec extends AnyFlatSpec with Matchers with AppendedClues with Pending
strings
.
foldLeft
(
"X"
)(
_
+
_
)
should
not
be
"Xcba"
withClue
", this looks like a right fold with flipped args"
strings
.
foldLeft
(
"X"
)(
_
+
_
)
shouldBe
"Xabc"
}
}
\ No newline at end of file
}
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