--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Didn't discarded it, just wondering if there was a better approach. Seems inconsistent you can throw monadic structures at for comprehensions but they don't always combine as expected based on the ones you combine. e.g. Option and List are fine together
Som, thanks for the clever analogy. I have seen that case before.Luis I will look into OptionT more in deep to achieve what I'm looking for.I'm still of the opinion that for comprehensions while powerful is not obvious by looking at them what the result would be.Asking other Scala devs outside of this list, they seem to find awkward how the different types may be used in a single comprehension yielding dispare results or resulting in cryptic compilation errors like in Som's example.
Hello,
Interesting. Why is it so hard? What happens if you try?
On Tue, Aug 13, 2013 at 4:54 AM, Kevin Wright <kev.lee...@gmail.com> wrote:
> On a theoretical level, yes, it would be easier.
>
> On a practical level, it can't be done, at least not yet.
>
> I've tried it, Paulp has tried it, for all I know even Martin has tried it.
> Everyone I know who looked into this has come to the same conclusion: It
> can't be done without royally screwing binary compatibility, so it ain't
> happenin' at the moment.
Well, flatMap is a rather critically important method with respect to the monadic effect I'm talking about!
On Tue, Aug 13, 2013 at 9:07 AM, Oliver Ruebenacker <cur...@gmail.com> wrote:
Hello,
It makes perfect sense to me that, for example, Option.map should
On Tue, Aug 13, 2013 at 10:34 AM, Kris Nuttycombe
<kris.nu...@gmail.com> wrote:
> The problem is the semantics of the monadic effect; the effect of Option is
> to impose zero-or-one semantics on the value in its context, whereas the
> effect of List is to impose zero-or-n semantics. These are not the same, any
> more than zero-or-one is the same as present-or-future (the Future monad) or
> at-least-one (NonEmptyList).
>
> Think of it this way: Option, by presenting a zero-or-one context, is
> promising its caller that there will never be more than one value. Allowing
> Option#flatMap to return List would make it impossible to enforce that
> contract through a sequence of operations. In short, it's a different type
> for a reason; if you want it to behave like a List, there's always .toList,
> which is nicely explicit.
return Option. But not flatMap. Certainly, we can't impose the
requirement that any method of Option returns Option. Why not let
flatMap be one of those that don't?
Hello,
Ah, my bad, I missed some subtlety. There are really two flatMap
methods, the one from Iterable and the one from Option. If Option
would become an Iterable, it would have both. And I think it should
be:
Option.flatMap[B](f: (A) => Option[B]): Option[B]
Option.flatMap[B](f: (A) => GenTraversableOnce[B]): Iterable[B]
If Option was an Iterable, it would also be a GenTraversableOnce.
Would that cause ambiguities?
Could we rename the current Option.flatMap to avoid collision with
Iterable.flatMap?
On Tue, Aug 13, 2013 at 1:54 PM, Oliver Ruebenacker <cur...@gmail.com> wrote:
Hello,
Ah, my bad, I missed some subtlety. There are really two flatMap
methods, the one from Iterable and the one from Option. If Option
would become an Iterable, it would have both. And I think it should
be:
Option.flatMap[B](f: (A) => Option[B]): Option[B]
Option.flatMap[B](f: (A) => GenTraversableOnce[B]): Iterable[B]Recall that these two signatures will have the same erasure.
If Option was an Iterable, it would also be a GenTraversableOnce.
Would that cause ambiguities?
Could we rename the current Option.flatMap to avoid collision with
Iterable.flatMap?You seem to miss my point that Option#flatMap[B](f: A => Option[B]): Option[B] is the *correct* signature for the zero-or-one monadic effect, which is what Option is all about in the first place. Once again, what's wrong with calling .toSeq, .toList, .toStream and being nicely explicit about it? It is *vastly* more important that consistent semantics with respect to the monadic effect designated by the type be preserved, than it is for you to be able to avoid 6 characters of typing.Direct implicit conversions between types (including the current implicit conversion that allows "for(i <- List(1, 2, 3), j <- Some(1)) yield ..." to work) are the source of many, many problems, and the solution to very few. Be explicit, and the future you will thank you.
If a monad is something that contains zero or more values, that sounds to me exactly like a collection. As I said, almost any one who explains what an Option is will say that it is "like a collection".
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
That means that you want Option to implement Iterable monadic flatMap instead of the monadic flatMap that really belongs to Option.
What stops you to do that is you should be able to implement Option's map method using Option's flatMap method. If we use Iterable flatMap then map should also return Iterable... that's a non-sense.
That means that you want Option to implement Iterable monadic flatMap instead of the monadic flatMap that really belongs to Option.
What stops you to do that is you should be able to implement Option's map method using Option's flatMap method.
If we use Iterable flatMap then map should also return Iterable... that's a non-sense.
Hello,
On Wed, Aug 14, 2013 at 1:50 PM, Luis Ángel Vicente Sánchez <langel...@gmail.com> wrote:
That means that you want Option to implement Iterable monadic flatMap instead of the monadic flatMap that really belongs to Option.
No, I want Option to have two flatMaps, a monadic flatMap and an iterable flatMap.
What stops you to do that is you should be able to implement Option's map method using Option's flatMap method.
Why do you need to be able to do this? You can still implement Option.map using monadic flatMap, if you like.
If we use Iterable flatMap then map should also return Iterable... that's a non-sense.
Option.map would still return Option. If Option is an Iterable, then that is compatible with Iterable.map.
On Wed, Aug 14, 2013 at 11:44 AM, Oliver Ruebenacker <cur...@gmail.com> wrote:
Hello,
I perfectly understand that you suggest that Option should not be Iterable, but instead provide conversion to iterable. You have stated that clearly.
What I don't understand is why you suggest that.
I also don't understand why you think that solving your, ahem, exercise would bring me any closer to an answer.
That's because you haven't solved it yet. :) In seriousness though, you need to recognize that for-comprehensions are *not just for traversing collection-like things*. They are general-purpose machinery for sequencing operations in a type-safe fashion, built around the idea of the monadic bind.Here is an example of something that is not collection-like at all:val myGen = for { n <- Gen.choose(10,20) m <- Gen.choose(2*n, 500) } yield (n,m)Should Gen (this is from https://github.com/rickynils/scalacheck/wiki/User-Guide) also implement your "iterable flatMap"? Of course not, this makes no sense.Similarly,val myFuture = for {a <- Future(/* some long-running computation */)b <- someOtherLongRunningComputationReturningFuture(a)} yield bmakes *no sense* unless the consistency requirement for the monadic context is maintained.Monadic composition has *laws* which must be obeyed. In Scala, for-comprehensions desugar via a form of structural typing to flatMap (and map and filter) calls on the underlying types. Now, it is true that Scala's structural typing would make it possible to change Option in the way that you suggest, and in so doing you'd simultaneously make Option *utterly useless* for a much wider set of use cases.The reason that the Monad typeclass is interesting is because knowing that something is a Monad allows you to completely abstract away the context of your computation. Here's a blog post you should read on this topic, if you have a genuine desire to understand:Kris
Kris,
Great! Thanks!