I have a function that looks like this:
def createBuilder(builder: InitialBuilder, name: Option[String], useCache: Boolean, timeout: Option[Long]): Builder = {
val filters: List[Builder => Option[Builder]] = List(
b => name.map(b.withName),
b => if (useCache) Some(b.withCache) else None,
b => timeout.map(b.withTimeout))
filters.foldLeft(builder)((b,filter) => filter(b).getOrElse(b))
}
It defines 3 filter functions from Builder => Option[Builder] (converting from optional parameters). I want to apply them to an existing builder value, so in case of a None, I can return itself, unchanged.
The code above is the best I could come up with, but it feels that I should somehow be able to do this with a Monoid - return the identity in case of a None.
Unfortunately, I can't figure out how to define one that makes sense. Or, if there's a better/different way of doing this?
I'm using Cats, if that matters. Any ideas?