Re: Filter on future doesn't make sense

Viktor Klang <[email protected]> Fri, 1 Jul 2016 11:30:51 +0200
Newsgroups gmane.comp.lang.scala
Message-ID <CANPzfU_DcRpCPzgR2-qXk36b_ia6LDjMXLGm_d4NE_ZK2cDDpw@mail.gmail.com>
--001a113cf70677ca3405368fa446
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

But you don't get an exception on the success path. All operations on the
success path will be skipped, just as for Option.

--=20
Cheers,
=E2=88=9A
On Jul 1, 2016 10:35 AM, "Avi Levi" <[email protected]> wrote:

> Hi Victor ,
> I totally agree that expectations are subjective .
> But like you said a filter on List will return a List if no element compl=
y
> than we will get Nil which represents an empty container (empty list)
> a filter on option will return a option if the element doesn't comply tha=
n
> we will get None which represents an empty container
> following the same logic a filter on future should return something that
> express empty container , defiantly not an exception, because it is not a=
n
> exception we do intend to filter those elements.
>
>
>
> 2016-07-01 10:50 GMT+03:00 Viktor Klang <[email protected]>:
>
>> 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 ne=
eds
>> improvement:
>> http://www.scala-lang.org/api/2.11.8/index.html#scala.concurrent.Future@=
filter(p:T=3D
>> >Boolean)(implicitexecutor:scala.concurrent.ExecutionContext):scala.conc=
urrent.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 Fail=
ure.
>>
>> If success has an optionality aspect in your usage, use Future of Option
>> to express that, that would be my recommendation.
>>
>> --
>> Cheers,
>> =E2=88=9A
>> On Jul 1, 2016 9:20 AM, "Avi Levi" <[email protected]> wrote:
>>
>>> 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 a=
s
>>> one might expect and less expressive if it might or not return the valu=
e
>>> than Option is more expressive, don't you agree?
>>> On Jul 1, 2016 10:06 AM, "Viktor Klang" <[email protected]> wrote:
>>>
>>>> Hi Avi,
>>>>
>>>> Use Fututre[Option[Int]] if you want to deal with Some/None as a resul=
t
>>>> of filter ( f.map(_.filter(p) ) (alt. see monad transformers)
>>>>
>>>> --
>>>> Cheers,
>>>> =E2=88=9A
>>>> On Jul 1, 2016 8:53 AM, "Avi Levi" <[email protected]> wrote:
>>>>
>>>> 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 kin=
d
>>>> of breaks referential transparency (doesn't it ?) .
>>>> on the other hand what can I expect given this expression
>>>>
>>>> *def foo(i:Int):Future[Int]*
>>>>
>>>> the type result of
>>>>
>>>> *foo(1).filter(_ % 2 =3D=3D 0)*
>>>>
>>>>
>>>> I.M.H.O I would expect it to yield Option[T]  which makes more sense t=
o
>>>> express that we might get a value that pass the filter or not . is tha=
t
>>>> make sense ?
>>>>
>>>> Cheers
>>>> Avi
>>>>
>>>> --
>>>> 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.
>>>>
>>>> --
>>>> 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/unsubscri=
be
>>>> .
>>>> To unsubscribe from this group and all its topics, send an email to
>>>> [email protected].
>>>> 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.
>>>
>> --
>> 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
>> [email protected].
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
>
> --
> Sincerely,
> Avi Levi
> m: +972-52-3459959
> <https://www.linkedin.com/in/leviavi>
> https://about.me/avi.levi
>
> --
> 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.
>

--=20
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 e=
mail to [email protected].
For more options, visit https://groups.google.com/d/optout.

--001a113cf70677ca3405368fa446
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<p dir=3D"ltr">But you don&#39;t get an exception on the success path. All =
operations on the success path will be skipped, just as for Option.</p>
<p dir=3D"ltr">-- <br>
Cheers,<br>
=E2=88=9A</p>
<div class=3D"gmail_quote">On Jul 1, 2016 10:35 AM, &quot;Avi Levi&quot; &l=
t;<a href=3D"mailto:[email protected]">[email protected]</a>&gt; wrote:<br ty=
pe=3D"attribution"><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 =
.8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"rtl"><div dir=
=3D"ltr">Hi Victor ,</div><div dir=3D"ltr">I totally agree that expectation=
s are subjective .</div><div dir=3D"ltr">But like you said a filter on List=
 will return a List if no element comply than we will get Nil which represe=
nts an empty container (empty list)</div><div dir=3D"ltr">a filter on optio=
n will return a option if the element doesn&#39;t comply than we will get N=
one which represents an empty container=C2=A0</div><div dir=3D"ltr">followi=
ng the same logic a filter on future should return something that express e=
mpty container , defiantly not an exception, because it is not an exception=
 we do intend to filter those elements.</div><div dir=3D"ltr"><br></div><di=
v dir=3D"ltr">=C2=A0</div></div><div class=3D"gmail_extra"><br><div class=
=3D"gmail_quote"><div dir=3D"ltr">2016-07-01 10:50 GMT+03:00 Viktor Klang <=
span dir=3D"ltr">&lt;<a href=3D"mailto:[email protected]" target=3D"_b=
lank">[email protected]</a>&gt;</span>:</div><blockquote class=3D"gmai=
l_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left=
:1ex"><p dir=3D"ltr">Hi Avi,</p>
<p dir=3D"ltr">I see what you mean.</p>
<p dir=3D"ltr">I&#39;d want to avoid discussing expectation since it is sub=
jective. I will however note that the actualnsemantics of the filter method=
 on Future needs improvement: <a href=3D"http://www.scala-lang.org/api/2.11=
.8/index.html#scala.concurrent.Future@filter(p:T=3D" target=3D"_blank">http=
://www.scala-lang.org/api/2.11.8/index.html#scala.concurrent.Future@filter(=
p:T=3D</a>&gt;Boolean)(implicitexecutor:scala.concurrent.ExecutionContext):=
scala.concurrent.Future[T]</p>
<p dir=3D"ltr">As for might not return the value, the same goes for all fil=
ter-methods:</p>
<p dir=3D"ltr">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 o=
r Failure.</p>
<p dir=3D"ltr">If success has an optionality aspect in your usage, use Futu=
re of Option to express that, that would be my recommendation. </p><span><f=
ont color=3D"#888888">
<p dir=3D"ltr">-- <br>
Cheers,<br>
=E2=88=9A</p></font></span><div><div>
<div class=3D"gmail_quote">On Jul 1, 2016 9:20 AM, &quot;Avi Levi&quot; &lt=
;<a href=3D"mailto:[email protected]" target=3D"_blank">[email protected]</a>=
&gt; wrote:<br type=3D"attribution"><blockquote class=3D"gmail_quote" style=
=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><p dir=
=3D"ltr">Thanks Victor,<br>
 Of course it can be implemented as you suggested. But still I can argue th=
at the current implementation is not &quot;really filtering&quot; the resul=
ts as one might expect and less expressive if it might or not return the va=
lue than Option is more expressive, don&#39;t you agree? </p>
<div class=3D"gmail_quote">On Jul 1, 2016 10:06 AM, &quot;Viktor Klang&quot=
; &lt;<a href=3D"mailto:[email protected]" target=3D"_blank">viktor.kl=
[email protected]</a>&gt; wrote:<br type=3D"attribution"><blockquote class=3D"g=
mail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-l=
eft:1ex"><p dir=3D"ltr">Hi Avi,</p>
<p dir=3D"ltr">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)</=
p>
<p dir=3D"ltr">-- <br>
Cheers,<br>
=E2=88=9A</p>
<div class=3D"gmail_quote">On Jul 1, 2016 8:53 AM, &quot;Avi Levi&quot; &lt=
;<a href=3D"mailto:[email protected]" target=3D"_blank">[email protected]</a>=
&gt; wrote:<br type=3D"attribution"><blockquote style=3D"margin:0 0 0 .8ex;=
border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr">I was trying =
to figure out what should I accept when filtering over a future .<div>the c=
urrent implementation yields either a T or an exception which kind of break=
s referential transparency (doesn&#39;t it ?) .</div><div>on the other hand=
 what can I expect given this expression=C2=A0</div><div>







<p><span><i>def foo(i:Int):Future[Int]</i></span></p><p><span>the type resu=
lt of=C2=A0</span></p><p><i>foo(1).filter(_ % 2 =3D=3D 0)</i></p>







<p><span><br></span></p></div><div>I.M.H.O I would expect it to yield Optio=
n[T] =C2=A0which makes more sense to express that we might get a value that=
 pass the filter or not . is that make sense ?</div><div><br></div><div>Che=
ers</div><font color=3D"#888888"><div>Avi=C2=A0</div></font></div><font col=
or=3D"#888888">

<p></p>

-- <br>
You received this message because you are subscribed to the Google Groups &=
quot;scala-language&quot; group.<br>
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:[email protected]" targ=
et=3D"_blank">[email protected]</a>.<br>
For more options, visit <a href=3D"https://groups.google.com/d/optout" targ=
et=3D"_blank">https://groups.google.com/d/optout</a>.<br>
</font></blockquote></div>

<p></p>

-- <br>
You received this message because you are subscribed to a topic in the Goog=
le Groups &quot;scala-language&quot; group.<br>
To unsubscribe from this topic, visit <a href=3D"https://groups.google.com/=
d/topic/scala-language/wpGOB8f7qmY/unsubscribe" target=3D"_blank">https://g=
roups.google.com/d/topic/scala-language/wpGOB8f7qmY/unsubscribe</a>.<br>
To unsubscribe from this group and all its topics, send an email to <a href=
=3D"mailto:[email protected]" target=3D"_blank">s=
[email protected]</a>.<br>
For more options, visit <a href=3D"https://groups.google.com/d/optout" targ=
et=3D"_blank">https://groups.google.com/d/optout</a>.<br>
</blockquote></div>

<p></p>

-- <br>
You received this message because you are subscribed to the Google Groups &=
quot;scala-language&quot; group.<br>
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:[email protected]" targ=
et=3D"_blank">[email protected]</a>.<br>
For more options, visit <a href=3D"https://groups.google.com/d/optout" targ=
et=3D"_blank">https://groups.google.com/d/optout</a>.<br>
</blockquote></div>

<p></p>

-- <br>
You received this message because you are subscribed to a topic in the Goog=
le Groups &quot;scala-language&quot; group.<br>
To unsubscribe from this topic, visit <a href=3D"https://groups.google.com/=
d/topic/scala-language/wpGOB8f7qmY/unsubscribe" target=3D"_blank">https://g=
roups.google.com/d/topic/scala-language/wpGOB8f7qmY/unsubscribe</a>.<br>
To unsubscribe from this group and all its topics, send an email to <a href=
=3D"mailto:[email protected]" target=3D"_blank">s=
[email protected]</a>.<br>
For more options, visit <a href=3D"https://groups.google.com/d/optout" targ=
et=3D"_blank">https://groups.google.com/d/optout</a>.<br>
</div></div></blockquote></div><br><br clear=3D"all"><div><br></div>-- <br>=
<div data-smartmail=3D"gmail_signature"><div dir=3D"rtl"><div><div dir=3D"r=
tl"><div><div dir=3D"rtl"><div dir=3D"ltr"><span style=3D"color:rgb(51,51,5=
1);font-family:proxima-nova-1,proxima-nova-2,Tahoma,Helvetica,Verdana,sans-=
serif;font-size:14px;line-height:21px">Sincerely,</span><br style=3D"color:=
rgb(51,51,51);font-family:proxima-nova-1,proxima-nova-2,Tahoma,Helvetica,Ve=
rdana,sans-serif;font-size:14px;line-height:21px"><span style=3D"color:rgb(=
51,51,51);font-family:proxima-nova-1,proxima-nova-2,Tahoma,Helvetica,Verdan=
a,sans-serif;font-size:14px;line-height:21px">Avi Levi</span></div><div dir=
=3D"ltr"><span style=3D"color:rgb(51,51,51);font-family:proxima-nova-1,prox=
ima-nova-2,Tahoma,Helvetica,Verdana,sans-serif;font-size:14px;line-height:2=
1px">m: <a href=3D"tel:%2B972-52-3459959" value=3D"+972523459959" target=3D=
"_blank">+972-52-3459959</a></span><span style=3D"color:rgb(51,51,51);font-=
family:proxima-nova-1,proxima-nova-2,Tahoma,Helvetica,Verdana,sans-serif;fo=
nt-size:14px;line-height:21px"><br></span></div><div dir=3D"ltr"><span styl=
e=3D"color:rgb(51,51,51);font-family:proxima-nova-1,proxima-nova-2,Tahoma,H=
elvetica,Verdana,sans-serif;font-size:14px;line-height:21px"><a href=3D"htt=
ps://www.linkedin.com/in/leviavi" target=3D"_blank"><img src=3D"https://doc=
s.google.com/uc?export=3Ddownload&amp;id=3D0B82ayL0XJaZ5WEY0c2xWQWYxNDQ&amp=
;revid=3D0B82ayL0XJaZ5YklVWjUvcG9tQmNrVEFRSDhNd0lNLzUwKzlvPQ"><span></span>=
<span></span></a></span></div><div dir=3D"ltr"><span style=3D"margin:0px;pa=
dding:0px;border:0px;outline:0px;font-size:14px;font-family:proxima-nova-1,=
proxima-nova-2,Tahoma,Helvetica,Verdana,sans-serif;vertical-align:baseline;=
color:rgb(43,130,173);line-height:21px"><a href=3D"https://about.me/avi.lev=
i" target=3D"_blank">https://about.me/avi.levi</a></span><br></div></div></=
div></div></div></div></div>
</div>

<p></p>

-- <br>
You received this message because you are subscribed to the Google Groups &=
quot;scala-language&quot; group.<br>
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:[email protected]" targ=
et=3D"_blank">[email protected]</a>.<br>
For more options, visit <a href=3D"https://groups.google.com/d/optout" targ=
et=3D"_blank">https://groups.google.com/d/optout</a>.<br>
</blockquote></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;scala-language&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:[email protected]">scal=
[email protected]</a>.<br />
For more options, visit <a href=3D"https://groups.google.com/d/optout">http=
s://groups.google.com/d/optout</a>.<br />

--001a113cf70677ca3405368fa446--