Re: Proposal: create an Extractor for PartialFunction

杨博 <[email protected]>
Newsgroups gmane.comp.lang.scala
Message-ID <[email protected]>
BTW: At the moment, all the other answers on the discussion <at 
http://stackoverflow.com/questions/5902266/inverse-of-partialfunctions-lift-method> 
except mine are wrong because they did not implement getOrElse.

在 2015年7月9日星期四 UTC+8下午10:43:15,Vlad Patryshev写道:
>
> Here's some discussion of this issue: 
> http://stackoverflow.com/questions/5902266/inverse-of-partialfunctions-lift-method
>
> Instead of going back into the world of booleans and conditions, I'd 
> suggest to think about why there's no such thing already available. Maybe 
> this is the right direction. applyOrElse is total; if it does not work 
> for you, lift should. It is pretty scientific, and gets you to a Kleisli 
> category. All these ifs and match/cases are a little bit like crutches.
>
> Thanks,
> -Vlad
>
> On Wed, Jul 8, 2015 at 9:56 PM, 杨博 <[email protected] <javascript:>> 
> wrote:
>
>> Sometimes, when I create a method, I need an extractor parameter for 
>> pattern matching, while there is not a generic extractor trait in the Scala 
>> Standard Library. So we have to use a PartialFunction as the parameter 
>> instead.
>>
>>
>> When I use the PartialFunction, I had to create pattern matching 
>> expression like this.
>>
>>
>> input match {
>>
>>   case x if partialFunction.isDefined(x) =>
>>
>>     val data = partialFunction.apply(x)
>>
>>     doSomethingWith(data)
>>
>> }
>>
>>
>> This approach is not perfect because:
>>
>>
>>    1. partialFunction.isDefined and partialFunction.apply checked x 
>>    twice, which is slow if isDefined has complex logical, checking 
>>    whether a file exists for example.
>>    2. The if condition around temporary variable x is too trivial.
>>
>> I think the better approach is converting the PartialFunction to an 
>> extractor and matching with that extractor
>>
>>
>> implicit final class PartialFunctionAsExtractor[A, B](pf: 
>> PartialFunction[A, B]) {
>>
>>   object Extractor {
>>
>>     def unapply(a: A) = PartialFunction.condOpt(a)(pf)
>>
>>   }
>>
>> }
>>
>>
>> input match {
>>   case partialFunction.Extractor(data) =>
>>     doSomethingWith(data)
>> }
>>
>> Since this is a common approach, I hope adding the 
>> PartialFunctionAsExtractor to scala.Predef .
>>
>> I would like to create a pull request if people like this idea.
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "scala-language" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to [email protected] <javascript:>.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
You received this message because you are subscribed to the Google Groups "scala-language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/d/optout.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.