I have the following code using Dispatch 0.11:
def myHttpPut(urlToPut: String, params: Map[String, String]): Future[Response] = {
val req = url(urlToPut).PUT
params.foreach { case (k, v) => req.addParameter(k, v) }
Http(req)
}
This does not work because addParameter does not modify req - instead it produces a new req object with the parameter added (which, in this case, is being thrown away). What's the most elegant way to write this so that I essentially loop over params, calling addParameter with each key/value pair of the map, building up req until I pass it into Http(req)?
In this case, you want to fold over the
paramsmap, applying a function to each key/value pair that also takes the result of calling that function on the previous key/value pair.params.foldLefttakes a start value (ur(urlToPut).PUT) and then passes that into the function defined here (case (req, (k, v)) => ...). Each key/value pair in the map gets passed into this function withreqtaking the value of the previous step (or the start value for the initial step).