def foo(i:Int):Future[Int]
the type result of
foo(1).filter(_ % 2 == 0)
Hi Avi,
Use Fututre[Option[Int]] if you want to deal with Some/None as a result of filter ( f.map(_.filter(p) ) (alt. see monad transformers)
--
Cheers,
√
--
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 scala-languag...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Thanks Victor,
Of course it can be implemented as you suggested. But still I can argue that the current implementation is not "really filtering" the results as one might expect and less expressive if it might or not return the value than Option is more expressive, don't you agree?
You received this message because you are subscribed to a topic in the Google Groups "scala-language" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/scala-language/wpGOB8f7qmY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to scala-languag...@googlegroups.com.
Hi Avi,
I see what you mean.
I'd want to avoid discussing expectation since it is subjective. I will however note that the actualnsemantics of the filter method on Future needs improvement: http://www.scala-lang.org/api/2.11.8/index.html#scala.concurrent.Future@filter(p:T=>Boolean)(implicitexecutor:scala.concurrent.ExecutionContext):scala.concurrent.Future[T]
As for might not return the value, the same goes for all filter-methods:
List returns List, but it might not have any elements at all. Option returns Option but can be Some or None, Future is either Success or Failure.
If success has an optionality aspect in your usage, use Future of Option to express that, that would be my recommendation.
--
Cheers,
√
But you don't get an exception on the success path. All operations on the success path will be skipped, just as for Option.
--
Cheers,
√
Correct, just on future you get an exception if element does not pass the filter. Wouldn't it be better if we can get something that express an empty future?
I think you are conflating the notion of optionality here.
option.filter(p).map(f).foreach(println)
behaves exactly like
future.filter(p).map(f).foreach(println)
--
Cheers,
√
Hi Oliver,
Which is why I explicitly talked about the success path.
As you saw, onComplete takes both path, much similar to something like toString.
See my previous example for details.
--
Cheers,
√
Hi Avi,
I'm unsure "seems odd" can be discussed successfully, I'm afraid.
As have been demonstrated, filter on Future behaves semantically equivalent to the methods used by for-comprehensions (map, flatMap, filter/withFilter and foreach)
--
Cheers,
√
Viktor, OliverI guess you are correct .Butscala> Some(1).filter(_ == 0)res0: Option[Int] = Nonescala> Set(1, 2, 3).filter(_ == 0)res1: scala.collection.immutable.Set[Int] = Set()BUTval x = Future{1}.filter(_ == 0)Failure(java.util.NoSuchElementException: Future.filter predicate is not satisfied))
I was trying to figure out what should I accept when filtering over a future .the current implementation yields either a T or an exception which kind of breaks referential transparency (doesn't it ?) .
on the other hand what can I expect given this expressiondef foo(i:Int):Future[Int]
the type result of
foo(1).filter(_ % 2 == 0)
I.M.H.O I would expect it to yield Option[T] which makes more sense to express that we might get a value that pass the filter or not . is that make sense ?CheersAvi
--
Yeah, you are right . I take that back
You received this message because you are subscribed to a topic in the Google Groups "scala-language" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/scala-language/wpGOB8f7qmY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to scala-languag...@googlegroups.com.