Back
Close

Reactive Programming with Reactor 3

Reactor
145.1K views
Previous: StepVerifier and how to use it Next: Merge

Transform

Description

Reactor ships with several operators that can be used to transform data.

Practice

In the first place, we will capitalize a String. Since this is a simple 1-1 transformation with no expected latency, we can use the map operator with a lambda transforming a T into a U.

Capitalize data on Mono

We can use exactly the same code on a Flux, applying the mapping to each element as it becomes available.

Capitalize data on Flux

Now imagine that we have to call a webservice to capitalize our String. This new call can have latency so we cannot use the synchronous map anymore. Instead, we want to represent the asynchronous call as a Flux or Mono, and use a different operator: flatMap.

flatMap takes a transformation Function that returns a Publisher<U> instead of a U. This publisher represents the asynchronous transformation to apply to each element. If we were using it with map, we'd obtain a stream of Flux<Publisher<U>>. Not very useful.

But flatMap on the other hand knows how to deal with these inner publishers: it will subscribe to them then merge all of them into a single global output, a much more useful Flux<U>. Note that if values from inner publishers arrive at different times, they can interleave in the resulting Flux.

Async transformation
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
package io.pivotal.literx;
//generic imports to help with simpler IDEs (ie tech.io)
import java.util.*;
import java.util.function.*;
import java.time.*;
import io.pivotal.literx.domain.User;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
/**
* Learn how to transform values.
*
* @author Sebastien Deleuze
*/
public class Part04Transform {
//========================================================================================
// TODO Capitalize the user username, firstname and lastname
Mono<User> capitalizeOne(Mono<User> mono) {
return null;
}
//========================================================================================
// TODO Capitalize the users username, firstName and lastName
Flux<User> capitalizeMany(Flux<User> flux) {
return null;
}
//========================================================================================
// TODO Capitalize the users username, firstName and lastName using #asyncCapitalizeUser
Flux<User> asyncCapitalizeMany(Flux<User> flux) {
return null;
}
Mono<User> asyncCapitalizeUser(User u) {
return Mono.just(new User(u.getUsername().toUpperCase(), u.getFirstname().toUpperCase(), u.getLastname().toUpperCase()));
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants