I am a relative Scala newbie and would like some advice on how to proceed with an implementation that seems to be able to be executed with either a function returning Option or a PartialFunction. I read all the related entries I could find (see the bottom of the question), but they seem to be related to the technical details of using PartialFunction or converting one to another; I am looking for an answer like "if the circumstances are X, Y, Z, and then use A else B, but also consider C".
My use case is finding paths between locations using a path search library. Suppose the locations are of type L , the path is of type P , and the desired path search result is Iterable[P] . The result of the patch search should be compiled by requesting all search devices (for example, Google maps, it can be bicycles, car, walk, metro, etc.) For their suggestions along the way, which may or may not be determined for a particular launch end point.
There seem to be two ways to do this:
(a) define the path search as f: (L,L) => Option[P] , and then get the result through something like finders.map( _.apply(l1,l2) ).filter( _.isDefined ).map( _.get )
(b) define the path search as f: PartialFunction[(L,L),P] and then get the result via something like finders.filter (_. isDefined ((l1, l2))). map (_. apply (( l1, l2))) `
It seems that using the function returning Option[P] will avoid double-evaluating the results, so for an expensive calculation, this might be preferable if not caching the results. It seems that using Option may have an arbitrary input signature, while PartialFunction expects a single argument. But I am particularly interested in hearing from someone with practical experience regarding less direct, more βwiderβ considerations, such as interacting with the Scala library. Will using PartialFunction have significant advantages in providing certain collection API methods that can pay off in other ways? Would such code be more concise?
Related but different questions: