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
Container Registry
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
intro-to-fp
short-exercises
Commits
3c3a0338
Commit
3c3a0338
authored
4 years ago
by
Alexander Gehrke
Browse files
Options
Downloads
Patches
Plain Diff
[lec04] Stream is now LazyList
parent
cd95001c
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
src/main/scala/laziness/LazyList.scala
+15
-15
15 additions, 15 deletions
src/main/scala/laziness/LazyList.scala
src/test/scala/laziness/LazyListSpec.scala
+8
-8
8 additions, 8 deletions
src/test/scala/laziness/LazyListSpec.scala
with
23 additions
and
23 deletions
src/main/scala/laziness/
Stream
.scala
→
src/main/scala/laziness/
LazyList
.scala
+
15
−
15
View file @
3c3a0338
package
laziness
sealed
trait
Stream
[
+A
]
{
sealed
trait
LazyList
[
+A
]
{
// uncomment to be able to use cons(h, t) and empty directly
// imports methods from the companion object
//import
Stream
._
//import
LazyList
._
def
toList
:
List
[
A
]
=
???
def
take
(
n
:
Int
)
:
Stream
[
A
]
=
???
def
take
(
n
:
Int
)
:
LazyList
[
A
]
=
???
//This can be tailrecursive. Uncomment below to let the compiler check
//This can be tailrecursive. Uncomment below to let the compiler check
for it.
//@annotation.tailrec
def
drop
(
n
:
Int
)
:
Stream
[
A
]
=
???
def
drop
(
n
:
Int
)
:
LazyList
[
A
]
=
???
// Methods shown during lecture
...
...
@@ -32,35 +32,35 @@ sealed trait Stream[+A] {
}
}
case
object
Empty
extends
Stream
[
Nothing
]
case
class
Cons
[
+A
](
h
:
()
=>
A
,
t
:
()
=>
Stream
[
A
])
extends
Stream
[
A
]
case
object
Empty
extends
LazyList
[
Nothing
]
case
class
Cons
[
+A
](
h
:
()
=>
A
,
t
:
()
=>
LazyList
[
A
])
extends
LazyList
[
A
]
object
Stream
{
// companion object
object
LazyList
{
// companion object
def
fibs
:
Stream
[
Int
]
=
{
def
fibs
:
LazyList
[
Int
]
=
{
// tip: write a recursive def here and call it with some start values
???
}
def
unfold
[
A
,
S
](
z
:
S
)(
f
:
S
=>
Option
[(
A
,
S
)])
:
Stream
[
A
]
=
???
def
unfold
[
A
,
S
](
z
:
S
)(
f
:
S
=>
Option
[(
A
,
S
)])
:
LazyList
[
A
]
=
???
def
fibsViaUnfold
:
Stream
[
Int
]
=
???
def
fibsViaUnfold
:
LazyList
[
Int
]
=
???
// Methods shown during Lecture
def
cons
[
A
](
h
:
=>
A
,
t
:
=>
Stream
[
A
])
:
Stream
[
A
]
=
{
def
cons
[
A
](
h
:
=>
A
,
t
:
=>
LazyList
[
A
])
:
LazyList
[
A
]
=
{
lazy
val
head
=
h
lazy
val
tail
=
t
Cons
(()
=>
head
,
()
=>
tail
)
}
def
empty
[
A
]
:
Stream
[
A
]
=
Empty
def
empty
[
A
]
:
LazyList
[
A
]
=
Empty
def
apply
[
A
](
as
:
A*
)
:
Stream
[
A
]
=
def
apply
[
A
](
as
:
A*
)
:
LazyList
[
A
]
=
if
(
as
.
isEmpty
)
empty
else
cons
(
as
.
head
,
apply
(
as
.
tail
:
_
*
))
val
ones
:
Stream
[
Int
]
=
cons
(
1
,
ones
)
val
ones
:
LazyList
[
Int
]
=
cons
(
1
,
ones
)
}
This diff is collapsed.
Click to expand it.
src/test/scala/laziness/
Stream
Spec.scala
→
src/test/scala/laziness/
LazyList
Spec.scala
+
8
−
8
View file @
3c3a0338
...
...
@@ -3,18 +3,18 @@ package laziness
import
org.scalatest._
import
testutil.PendingIfUnimplemented
class
Stream
Spec
extends
FlatSpec
with
Matchers
with
AppendedClues
with
PendingIfUnimplemented
{
class
LazyList
Spec
extends
FlatSpec
with
Matchers
with
AppendedClues
with
PendingIfUnimplemented
{
import
Stream
._
import
LazyList
._
/*
* Note that equivalence checking on
stream
s does not work.
* Note that equivalence checking on
lazy list
s does not work.
* Therefore it is important, that your toList implementation works for most later tests.
*/
"A
stream
"
should
"be convertable to a list"
in
{
Stream
(
1
,
2
,
3
).
toList
shouldBe
List
(
1
,
2
,
3
)
Stream
.
empty
.
toList
shouldBe
List
()
"A
LazyList
"
should
"be convertable to a
n eagerly evaluated
list"
in
{
LazyList
(
1
,
2
,
3
).
toList
shouldBe
List
(
1
,
2
,
3
)
LazyList
.
empty
.
toList
shouldBe
List
()
}
it
should
"have a take method that keeps only a certain number of elements"
in
{
...
...
@@ -24,7 +24,7 @@ class StreamSpec extends FlatSpec with Matchers with AppendedClues with PendingI
}
it
should
"have a drop method that removes a certain number of elements from the start"
in
{
Stream
(
1
,
2
,
3
,
4
).
drop
(
1
).
toList
shouldBe
List
(
2
,
3
,
4
)
LazyList
(
1
,
2
,
3
,
4
).
drop
(
1
).
toList
shouldBe
List
(
2
,
3
,
4
)
ones
.
drop
(
17
).
headOption
should
contain
(
1
)
}
...
...
@@ -33,7 +33,7 @@ class StreamSpec extends FlatSpec with Matchers with AppendedClues with PendingI
fibs
.
take
(
8
).
toList
shouldBe
List
(
0
,
1
,
1
,
2
,
3
,
5
,
8
,
13
)
}
"unfold"
should
"should generate
stream
s from a function while stopping on None"
in
{
"unfold"
should
"should generate
lazy list
s from a function while stopping on None"
in
{
val
alphabet
=
unfold
(
'a'
)(
s
=>
if
(
s
<=
'z'
)
Some
((
s
,
(
s
+
1
).
toChar
))
else
None
)
alphabet
.
toList
shouldBe
(
'a'
to
'z'
).
toList
}
...
...
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