Use `{` instead of `(` and the last `{` is not necessary: 

~~~
.mapPartitions { x =>
  ...
}
~~~