-
Notifications
You must be signed in to change notification settings - Fork 274
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
Add mapNotNull and whereNotNull #548
Conversation
I'm actually a bit on the fence with this one, Basically any transformer could change type to Nullable, flatmap, switchmap, etc... |
Some languages like // kotlin mapNotNull
inline fun <T, R : Any> Iterable<T>.mapNotNull(transform: (T) -> R?): List<R>
// kotlin flatMap
inline fun <T, R> Iterable<T>.flatMap(transform: (T) -> Iterable<R>): List<R> // swift compactMap
func compactMap<ElementOfResult>(_ transform: (Self.Element) throws -> ElementOfResult?) rethrows -> [ElementOfResult]
// swift flatMap
func flatMap<SegmentOfResult>(_ transform: (Self.Element) throws -> SegmentOfResult) rethrows -> [SegmentOfResult.Element] where SegmentOfResult : Sequence
Stream<R> f(Stream<R> Function(T)) Here,
Stream<R> map(R Function(T))
Stream<String> getMessageString(Stream<Message> stream) {\
String? mapping(Message msg) { ... }
return stream.map(mapping).where((e) => e != null).cast<String>();
} or Stream<String> getMessageString(Stream<Message> stream) {
String? mapping(Message msg) { ... }
return stream.map(mapping).whereType<String>();
} If we have Stream<String> getMessageString(Stream<Message> stream) {\
String? mapping(Message msg) { ... }
return stream.mapNotNull(mapping);
} |
For anyone interested in those extensions, consider my package https://github.com/hoc081098/rxdart_ext 😃 |
Codecov Report
@@ Coverage Diff @@
## master #548 +/- ##
==========================================
+ Coverage 93.82% 93.88% +0.06%
==========================================
Files 70 73 +3
Lines 2201 2223 +22
==========================================
+ Hits 2065 2087 +22
Misses 136 136 |
Sorry took a while on my end, Alternatively, we could also include them, but under a special import, like: |
I think including it in |
Sorry, but I don't feel like this is a good idea. This just expands the surface of the API, turning it more difficult to the user to master rxdart. Also, this operations can already be made with current operators. |
@hoc081098 - your work here is very useful and I thank you for it. @Ascenio - I disagree with you on this. Map-ing or where-ing where not null is pretty common in other languages Reactive or not. I understand the impulse not to make 100s of helpers all over the place but I don't think that applies in this circumstance. Having them would be massively useful day to day. It would be great to see this merged. |
Codecov Report
@@ Coverage Diff @@
## master #548 +/- ##
==========================================
+ Coverage 93.52% 93.59% +0.07%
==========================================
Files 74 76 +2
Lines 2284 2309 +25
==========================================
+ Hits 2136 2161 +25
Misses 148 148 |
Thanks @grahamsmith IMO, These lend to very clean and performant code when the right use case arises. As a quick example: Alternatives1.
|
I don't think that it expands the API, because many current operators in |
good. I think it is necessary. |
@hoc081098 I also created this extension myself to use. It will be helpful when it is available. |
Because we are using NNBD, it makes sense to add
mapNotNull
andwhereNotNull
extensions.Fixes #382
mapNotNull
: returns a Stream containing only the non-null
results of applying the giventransform
function to each element of the Stream.whereNotNull
: returns a Stream which emits all the non-null
elements of the Stream, in their original emission order.