stacycurl / pimpathon

Adds useful methods to scala & java classes.
Apache License 2.0
35 stars 9 forks source link

Pimp List with a partial map (named update) similar to updateIf #266

Closed xavierguihot closed 6 years ago

xavierguihot commented 6 years ago

Hello,

I'm used to have the following version of updateIf, which takes a PartialFunction as parameter:

def update(pf: A ~> A): List[A] = self.map {
  case x if pf.isDefinedAt(x) => pf(x)
  case x                      => x
}

which translates into:

List.range(1, 6).update { case x if x % 2 == 0 => x - 1 } === List(1, 1, 3, 3, 5)

Hope that makes sens as part of your pimps.

Not sure about the naming as update is kind of very generic. I sometimes called it partialMap, but update is shorter.

stacycurl commented 6 years ago

That's a useful method, anything to get rid of case other => other. 'update' conflicts with AnyPimps.update, but I favour using AnyPimps.tap anyhow. I'll merge it for now and see how it goes, worse case scenario it can be renamed, I've called it 'updatePF' and 'transform' before.