Concatenated Observables (RxJava ) with flapMap - java

I have some concatenated observable using flatMap such as
api.call1()
.flatMap{a-> api.call2(a)}
.flatMap{b-> api.call3(a,b) //<- no access to a}
the issue is that In third concatenated call I need what I got from previous one but also the result from the first one
this is the scheme:
A->(a)->B->(b)-> C( needs b but also a)->...
I try to solve it out like that :
1 approach ( nested 3rd observable inside the 2nd one)
api.call1()
.flatMap{a-> api.call2(a).flatMap{b-> api.call3(a,b)}}
it works but I know that this is not a good approach (because of passing parms from outside of the pipe)
2 approach (better but a lot of boilerplate)
api.call1().flatMap{ a->
Observable.combineLatest(Observable.just(a),api.call2(a),
BiFunction{ a,b -> Pair(a,b)})
.flatMap(pair:Pair<A,B> -> api.call3(pair.first,pair.second))
}}
anybody know better approach (some fashion operator)
Thanks everybody sory for my poor ingles.

There is a second overload of flatMap which takes both the input and the output. You can then combine these into a Pair to send to the next flatMap
api.call1()
.flatMap(
{ a -> api.call2(a) },
{ a, b -> new Pair(a, b) }
)
.flatMap { pair-> api.call3(pair.first, pair.second) }

Related

Zip mono/flux with another that depends on the one I own

I want to zip two monos/fluxes, but the second one (the one I'll zip) is dependand from the first that I already own.
For example:
//...
fun addGroup(
input: GroupInput
): Mono<Group> = Mono.just(Group(title = input.title, description = input.description))
.flatMap { g -> groupsRepository.save(g) } // Gives me back the new db ID
.zipWith(Mono.just(GroupMember(g.id /* <-- ??*/, input.ownerId, true)))
//...
// ...
Is it possible?
I would say no. You can only zip things that can run in parallel.
you could use map or flatmap in this case, is there any reason you need to use Zip?
No, your code cannot even compile because variable g belongs to the lambda inside the flatMap() call.
zipWith() is not intended for this use case, use map() or flatMap()

Java streaming higher order function

I am trying to process an object that has nested lists 2 levels deep. For example my object can be broken down to something like this:
TopLevel: [
MidLevel: [
LowLevel,
LowLevel,
.
.
],
MidLevel: [
LowLevel,
LowLevel,
.
.
]
.
.
]
Essentially TopLevel contains a list of MidLevel objects, which in turn each contain a list of LowLevel objects. At the end of the processing I would like to build SomeObj for each of the LowLevel objects. However SomeObj requires information from TopLevel, MidLevel, and LowLevel.
I have been trying to write code in a more functional style over the last several months so my first thought was to create a higher order function that I could build up over each level in the object. The function looks like this:
Function<MidLevel, Function<LowLevel, SomeObj>> buildObjects(TopLevel topLevel) {
return midLevel ->
lowLevel -> {
return buildSomeObj(topLevel, midLevel, lowLevel);
};
}
I intend to use this function in some way like the following (assume I have utility functions that provide a stream of the lists):
Function<MidLevel, Function<LowLevel, SomeObj>> topBuilder = buildObjects(topLevel);
List<SomeObj> objs = topLevel.streamMid()
.map(topBuilder)
.streamLow()
.map(Function::apply)
.collect(/*collect to list*/);
However, this obviously does not work because once I apply the MidLevel objects to the topBuilder function my stream is now a stream of functions and not MidLevel objects, thus I do not have access to the list of LowLevel objects in the stream anymore.
Is there any solution to this or am I trying to solve this functionally when it's not well suited to it? Is there a way to both apply the function, and also have access to the original object that was applied to that function?
flatMap() and nesting are the way to go. Try this:
topLevelStream() //create a stream of top level elements
.flatMap( top -> top.streamMid() //create a stream of mid level elements
.flatMap( mid -> mid.streamLow() //create a stream of low level elements
.map(low -> "use top, mid and low here")
)
)
.collect( ... );
By nesting like this you still have access to the elements in the outer functions and the combination of flatMap() and map() exposes the stream that map() is called upon to collect().
You could simply use flatMap as:
List<SomeObj> objs = topLevel.getMidLevels().stream()
.flatMap(a -> a.getLowLevels().stream().map(b -> topBuilder.apply(a).apply(b)))
.collect(Collectors.toList());
with your entities analogous to:
class TopLevel {
List<MidLevel> midLevels;
}
class MidLevel {
List<LowLevel> lowLevels;
}
class LowLevel {
}
class SomeObj {
}

Reactive Producer Consumer Observable in Java

Lets say I have a very long string:
trillions of chunks
|
v
/asdf/........./bar/baz/foo
^
|
what I try to find is closer to the right:
the data after 9999999th '/'
I need all the chunks of data up to this slash, but not any slashes. I see this as a stream and want to do the following:
I start to read symbols from the back and count slashes.
Anything but slash I put into Last-In-First-Out data structure.
In order not to wait for the whole operation to finish, I start reading data from the lifo datastructure as it becomes available.
I terminate after the 9999999th '/'
Can something like this be accomplished with reactive streams and how?
I think the following code will achve what you want
#Test
public void reactiveParser() throws InterruptedException {
ConnectableFlux<String> letters = Flux.create((Consumer<? super FluxSink<String>>) t -> {
char[] chars = "sfdsfsdf/sdf/sdfs/dfsdfsd/fsd/fsd/fs/df/sdf".toCharArray();
for (char c : chars) {
t.next(String.valueOf(c));
}
}).publish();
letters
.window(
letters.filter( t -> t.equals("/"))
)
.flatMap( t -> t.collectList())
.map( t -> t.stream().collect(Collectors.joining()))
.subscribe(t -> {
System.out.println(t);
});
letters.connect();
}
The example above utilizes the project reactor. Which is pretty cool way of doing the reactive stuff inside of java.
There is plenty of optimization that can be done in the following code. Not using Strings to represent a single letter would be one of them.
But the basic idea is there. You create flux/observable that emits a letters as they come in and make that observable shareable (you have to window over emitting values) and then just collect them in to a single sting. The code bellow should give the following output:
sfdsfsdf
/sdf
/sdfs
/dfsdfsd
/fsd
/fsd
/fs
/df
Of course you have to utilize non-blocking connection so the bytes could be read asynchronously.

Can Java 8 Streams use multiple items from mapping pipeline

I have some data stored in a JPA Repository that I am trying to process. I would like to be able to use Java 8 Streams to do so, but can not figure out how to get the required information. This particular 'Entity' is actually only for recovery, so it holds items that would need to be processed after something like a power-fail/restart.
Using pre-Java 8 for-loops the code would look like:
List<MyEntity> deletes = myEntityJpaRepository.findByDeletes();
for (MyEntity item : deletes) {
String itemJson = item.getData();
// use a Jackson 'objectMapper' already setup to de-serialize
MyEventClass deleteEvent = objectMapper.readValue(itemJson, MyEventClass.class);
processDelete(deleteEvent, item.getId());
}
The problem arises from the two parameter method called at the very end. Using Streams, I believe I would do:
// deletes.stream()
// .map(i -> i.getData())
// .map(event -> objectMapper.readValue(event, MyEventClass.class))
// .forEach(??? can't get 'id' here to invoke 2 parameter method);
I have a solution (without Streams) that I can live with. However I would think this problem comes up a lot, thus my question is: IN GENERAL, is there a way using Streams to accomplish what I am trying to do?
Why not a Pair return on your map operation:
.map(i -> new Pair<>(i.getData(), i.getId()))
.map(pair -> new Pair<>(objectMapper.readValue(pair.getLeft(), MyEventClass.class), pair.getRight())
.forEach(p -> processDelete(pair.getLeft(), pair.getRight()))
I did not compile this, so there might be minor things to fix. But in general, you would need a Holder to pass your objects to the next stage in such a case. Either a Pair or some type or even a array.
Why not doing it simply this way?
deletes.forEach(item ->
processDelete(objectMapper.readValue(item.getData(), MyEventClass.class),
item.getId()));
This is a start at least, I guess it is dependent on why you want to use stream and how much you want to make it more functional
List<MyEntity> deletes = myEntityJpaRepository.findByDeletes();
deletes.stream().foreach(item -> {
String itemJson = item.getData();
// use a Jackson 'objectMapper' already setup to de-serialize
MyEventClass deleteEvent = objectMapper.readValue(itemJson, MyEventClass.class);
processDelete(deleteEvent, item.getId());
});

Java, search for an element, if not found, add it

I am pretty new to streams.
I would like to stream the geometries EC_Geometry arraylist and if the EC_Geometry element is not present (or better equals never returns true), then I add it.
public void init(GL3 gl3, EC_Mesh mesh) {
geometries.stream()
.filter(geometry -> mesh.getGeometry().equals(geometry))
.findAny()
.orElse(..?);
}
But I am stuck at the last line
How can I solve it using streams?
Please note that equals is a method I wrote checking if the geometry is the same (i.e: if the triangles correspond)
orElse will always run even if the value returned isn't used so it is preferable to use orElseGet here which will only run if nothing is found.
geometries.stream()
.filter(geometry -> mesh.getGeometry().equals(geometry))
.findAny()
.orElseGet(() -> {
geometries.add(mesh.getGeometry());
return mesh.getGeometry();
});
.findAny().orElse(..?);
is for Optional - if you would like to get first element found.
For what you would like to achieve the best approach would be just to:
meshG = mesh.getGeometry();
if (!geometries.contains(meshG)) {
geometries.add(meshG);
}
No need to overuse Stream API.

Categories

Resources