Skip to content
Snippets Groups Projects
Commit 19c94620 authored by Alexander Gehrke's avatar Alexander Gehrke
Browse files

[lec03] Add templates

parent 1c70c64a
No related branches found
No related tags found
No related merge requests found
......@@ -7,10 +7,11 @@ Source templates for short exercises during the lecture, sometimes with tests.
Templates are within `src/main/scala`. Parts that you should complete are
usually marked with `???`.
| lecture | package |
|---------------------------------|-------------------------------------------------------------------|
| 2: Functional Data Structures | [`datastructures`](src/main/scala/datastructures) |
| 3: Error Handling | [`errors`](src/main/scala/errors) |
| lecture | package | tests
|---------------------------------|-------------------------------------------------------------------|----------------------------
| 2: Functional Data Structures | [`datastructures`](src/main/scala/datastructures) | `testOnly datastructures.*`
| 3: Error Handling | [`errors`](src/main/scala/errors) | `testOnly errors.*`
| 4: Laziness | [`laziness`](src/main/scala/laziness/) | `testOnly laziness.*`
## Usage tips:
To keep your local solutions to the exercises when pulling from the repository,
......@@ -19,6 +20,9 @@ use
git pull --rebase --autostash
```
This will keep both commited and uncommited changes.
If you are using Intellij IDEA, "rebase" is available as option in the update dialog and stashing is default.
If you are using Intellij IDEA, "rebase" is available as option in the update
dialog and stashing is default.
### Tests:
If you are using `sbt` directly, you can use `testOnly` to run a specific test suite (see table).
To automatically run tests, whenever a file changes, use `~testOnly` (`~` also works with other sbt commands).
package laziness
sealed trait Stream[+A] {
import Stream._
def toList: List[A] = ???
def take(n: Int): Stream[A] = ???
//This can be tailrecursive. Uncomment below to let the compiler check
//@annotation.tailrec
def drop(n: Int): Stream[A] = ???
// Methods shown during lecture
def headOption: Option[A] = this match {
case Cons(h, _) => Some(h())
case Empty => None
}
def exists(p: A => Boolean): Boolean = this match {
case Cons(x, xs) => p(x()) || xs().exists(p)
case Empty => false
}
def foldRight[B](z: => B)(f: (A, => B) => B): B = this match {
case Cons(x, xs) => f(x(), xs().foldRight(z)(f))
case Empty => z
}
}
case object Empty extends Stream[Nothing]
case class Cons[+A](h: () => A, t: () => Stream[A]) extends Stream[A]
object Stream { // companion object
def fibs: Stream[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 fibsViaUnfold: Stream[Int] = ???
// Methods shown during Lecture
def cons[A](h: => A, t: => Stream[A]): Stream[A] = {
lazy val head = h
lazy val tail = t
Cons(() => head, () => tail)
}
def empty[A]: Stream[A] = Empty
def apply[A](as: A*): Stream[A] =
if (as.isEmpty) empty
else cons(as.head, apply(as.tail: _*))
val ones: Stream[Int] = cons(1, ones)
}
......@@ -2,6 +2,9 @@ package testutil
import org.scalatest._
/**
* Mix into a test suite to mark tests for ??? methods as "pending" instead of "failed"
*/
trait PendingIfUnimplemented extends TestSuiteMixin { this: TestSuite =>
abstract override def withFixture(test: NoArgTest): Outcome = {
super.withFixture(test) match {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment