Writing type-safe Spark transformations