What is the use case for flatMap vs map in kotlin

Consider the following example: You have a simple data structure Data with a single property of type List. class Data(val items : List<String>) val dataObjects = listOf( Data(listOf(“a”, “b”, “c”)), Data(listOf(“1”, “2”, “3”)) ) flatMap vs. map With flatMap, you can “flatten” multiple Data::items into one collection as shown with the items variable. val items: … Read more

Return multiple values from ES6 map() function

With using only one reduce() you can do this. you don’t need map(). better approach is this: const values = [1,2,3,4]; const newValues= values.reduce((acc, cur) => { return acc.concat([cur*cur , cur*cur*cur, cur+1]); // or acc.push([cur*cur , cur*cur*cur, cur+1]); return acc; }, []); console.log(‘newValues=”, newValues) EDIT: The better approach is just using a flatMap (as @ori-drori … Read more

Java 8 Streams FlatMap method example

It doesn’t make sense to flatMap a Stream that’s already flat, like the Stream<Integer> you’ve shown in your question. However, if you had a Stream<List<Integer>> then it would make sense and you could do this: Stream<List<Integer>> integerListStream = Stream.of( Arrays.asList(1, 2), Arrays.asList(3, 4), Arrays.asList(5) ); Stream<Integer> integerStream = integerListStream .flatMap(Collection::stream); integerStream.forEach(System.out::println); Which would print: 1 … Read more

In RxJava, how to pass a variable along when chaining observables?

The advice I got from the Couchbase forum is to use nested observables: Observable .from(modifications) .flatmap( (data1) -> { return op1(data1) … .flatmap( (data2) -> { // I can access data1 here return op2(data2); }) }); EDIT: I’ll mark this as the accepted answer as it seems to be the most recommended. If your processing … Read more