---
# System prepended metadata

title: 'Problem 1: Implementing map and filter on Futures'

---

SCIPERS: 310088 295694 297138 314706 311844
Update this file with a copy of your group's solution.
Solution:

# Problem 1: Implementing map and filter on Futures
```scala=
trait Future[T] { self =>
  def onComplete(k: Try[T] => Unit):Unit
  def map[S](f: T => S): Future[S] =
    new Future[S] {
      def onComplete(callback: Try[S] => Unit): Unit = 
        self.onComplete((k) => k match {
            case Success(v) => callback(Success(f(v)))
            case Failure(err) => callback(Failure(err))
        })
    }

  def filter(f: T => Boolean): Future[T] =
    new Future[T] {
      def onComplete(callback: Try[T] => Unit): Unit = 
        self.onComplete((k) => k match {
            case Success(v) =>
                if (f(v)) callback(Success(v)) else callback(Failure(NoSuchElementException()))
            case fail => callback(fail)
        })
    }
}

```
# Problem 2: Coordinator / Worker
```scala=
class Coordinator extends Actor {

    val freeWorker = List()

    override def receive = {
        case r @ Request(comp) => 
            if (!freeWorker.isEmpty) then freeworker.head() ! r
        case Ready => 
            freeWorker:+sender
    }
}

class Worker(coordinator: Coordinator) extends Actor {

  override def receive = {
      case Request(comp) => 
        comp
        coordinator ! Ready 

  }
}