-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
enumerable methods not correctly iterating over an enumerable #16
Comments
@TiagoCardoso1983 Remember that the Maybe is just a thin Array-like wrapper. You can think Some as an array of one element ( So, And Since Maybe implements the Enumerable interface, all Enumerable methods (like reduce) are applied to the Maybe object, not to the value object. If you want to apply the Enumerable method to the value object, then you need to map first.
If this seems complicated, again, you can think it like an array:
There is an open Pull Request #7, which implements
The PR #7 is still open, mostly because I'm not too happy with the method name |
I understood the thin-wrapper logic, I was just making the point, it would make sense to me to delegate enumerable methods to the elements of the Some instance, i.e., if Some is composed of: # one could deleate map calls to the enumerables in the value container
=> Maybe([1, 2, 3]).reduce(0, :+)
#=> 6 So, just making a case that it could potentially make sense to handle it this way. The #inner method implementation, while going in that direction, seems to not yield Maybe's, which is the whole point of the other enumerable. I just wanted to know potential drawbacks from this approach. |
So what you're proposing is that Maybe should not implement the (Btw. is there a mistake in your example? Did you really mean that The So if Maybe would not implement In addition, this would be a change that will break a lot of code. At least we at Sharetribe are using Maybe quite heavily, and a lot of code would break. That's definitely a drawback :) Thanks for your comments! I'll think about it. I do agree that the behaviour is somewhat non-intuitive. I have personally made mistakes where I have called enumerable method and actually intended that method call to be passed to the value inside the wrapper (i.e. called The #inner method definitely yields Maybe values. |
I might be going out of scope with my request on what the Maybe monad should provide, but then again, I only read Haskell and never wrote code myself. My understanding of the Monads is that it behaves the same whether the value is one or many. Therefore I think that Some should be an Enumerable and should be able to implement #each. If the value would be an enumerable, block would be passed to value wrapping yielded values as Some, and if a normal value, wrapped it as Some and yield. It would also potentially have the benefit of decrease array allocations, depending of how much interaction there is with that __enumerable_value method. I think Some (and therefore None) should be de facto Enumerables instead of reimplementing all Enumerable methods. |
I'm finding this behaviour non-intuitive in terms of applying the monad on enumerable objects, like arrays. For instance, here we have the example from the tutorial:
when we do:
this is where I get confused. I'll show you my expectation and what it does:
So, my expecting in this case, when using enumerable method on the monad, was that the monad would yield monads, which is not. Is this a valid expectation?
The text was updated successfully, but these errors were encountered: