You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Imagine you are processing a (large) array, or working with a generator that on each record does a HTTP call or something that may fail. Normally looping through an array or collection that at some point throws an exception, ends the array, even if run within rescue(). This would mean you have to start all over again, just to get to the rest. But what if you could rescue that single item and continue where you left off?
For this I propose a rescue method on Collection and LazyCollection:
The above would work, but if you have to do more than just executing a callback, you would get a closure in a closure and it would become unreadable very quickly.
The following is much more readable. It would return the closure in case of an exception:
The reason of this is because under the hood you have to use map, otherwise values wouldn't be modified on the returned collection, and a failed tap call would otherwise return a collection with the second item null, which is undesirable. If you need it to return null, one could easily do the following:
The problem with this is that in order to do that, it needs to return a HigherOrderCollectionProxy, which must be able to pass along additional parameters to the Higher Order method. The current implementation does not do that, and the only viable way without modifying that class is by abusing Cache or Config or an Anonymous class which extends the proxy class. If someone has a better solution, please let me know.
The following would be the implementation. In rescueWith I have an Anonymous Class extending HigherOrderCollectionProxy to demonstrate the most viable idea, next to altering the original proxy class.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Imagine you are processing a (large) array, or working with a generator that on each record does a HTTP call or something that may fail. Normally looping through an array or collection that at some point throws an exception, ends the array, even if run within rescue(). This would mean you have to start all over again, just to get to the rest. But what if you could rescue that single item and continue where you left off?
For this I propose a rescue method on Collection and LazyCollection:
Given the following collection:
The following would fail:
Of course: you could write it like this:
The above would work, but if you have to do more than just executing a callback, you would get a closure in a closure and it would become unreadable very quickly.
The following is much more readable. It would return the closure in case of an exception:
The reason of this is because under the hood you have to use map, otherwise values wouldn't be modified on the returned collection, and a failed tap call would otherwise return a collection with the second item null, which is undesirable. If you need it to return
null
, one could easily do the following:This might quickly become unreadable again, so I was thinking of a second method called
rescueWith
.The problem with this is that in order to do that, it needs to return a
HigherOrderCollectionProxy
, which must be able to pass along additional parameters to the Higher Order method. The current implementation does not do that, and the only viable way without modifying that class is by abusing Cache or Config or an Anonymous class which extends the proxy class. If someone has a better solution, please let me know.The following would be the implementation. In
rescueWith
I have an Anonymous Class extendingHigherOrderCollectionProxy
to demonstrate the most viable idea, next to altering the original proxy class.Let me know what you guys think.
Beta Was this translation helpful? Give feedback.
All reactions