Questions tagged [flatmap]

flatMap is a Java and Scala function that works by applying a function that returns a sequence for each element in the list, and flattening the results into the original list.

flatMap works by applying a function that returns a sequence for each element in the list, and flattening the results into the original list. This is easier to show than to explain:

scala> def g(v:Int) = List(v-1, v, v+1)
g: (v: Int)List[Int]

scala> l.map(x => g(x))
res64: List[List[Int]] = List(List(0, 1, 2), List(1, 2, 3), List(2, 3, 4), List(3, 4, 5), List(4, 5, 6))

scala> l.flatMap(x => g(x))
res65: List[Int] = List(0, 1, 2, 1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 5, 6)

Source: http://www.brunton-spall.co.uk/post/2011/12/02/map-map-and-flatmap-in-scala/

545 questions
195
votes
11 answers

When do you use map vs flatMap in RxJava?

When do you use map vs flatMap in RxJava? Say, for example, we want to map Files containing JSON into Strings that contain the JSON-- Using map, we have to deal with the Exception somehow. But how?: Observable.from(jsonFile).map(new Func1
Christopher Perry
  • 38,891
  • 43
  • 145
  • 187
91
votes
9 answers

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

I am chaining async operations using RxJava, and I'd like to pass some variable downstream: Observable .from(modifications) .flatmap( (data1) -> { return op1(data1); }) ... .flatmap( (data2) -> { // How to access data1 here ? …
Julian Go
  • 4,442
  • 3
  • 23
  • 28
90
votes
7 answers

Java 8 Streams FlatMap method example

I have been checking the upcoming Java update, namely: Java 8 or JDK 8. Yes, I am impatient, there's a lot of new stuff, but, there is something I don't understand, some simple code: final Streamstream =…
chiperortiz
  • 4,751
  • 9
  • 45
  • 79
72
votes
4 answers

Return multiple values from ES6 map() function

Say I have something like this: let values = [1,2,3,4]; let newValues = values.map((v) => { return v *v ; }); console.log(newValues); //[1,4,9,16] Pretty straight forward. Now what if I want to return multiple values for each of my…
dwjohnston
  • 11,163
  • 32
  • 99
  • 194
66
votes
7 answers

What is the difference between concatMap and flatMap in RxJava

It seems that these 2 functions are pretty similar. They have same signature (accepting rx.functions.Func1> func), and their marble diagrams look exactly same. Can't paste the pics here, but here's one…
Haspemulator
  • 11,050
  • 9
  • 49
  • 76
60
votes
3 answers

What is the use case for flatMap vs map in kotlin

in https://try.kotlinlang.org/#/Kotlin%20Koans/Collections/FlatMap/Task.kt it has sample of using flatMap and map seems both are doing the same thing, is there a sample to show the difference of using flatMap and map? the data type: data class…
lannyf
  • 9,865
  • 12
  • 70
  • 152
31
votes
5 answers

How to flatten List of Lists in Kotlin?

I have a list of objects of class AA that contain a date and a list of objects of class BB: data class AA( val date: LocalDate, val bb: List ) @Parcelize data class BB( val x: Int, val y: String, val z: String ) :…
user990635
  • 3,979
  • 13
  • 45
  • 66
27
votes
2 answers

When and how to perform one to 0..n mapping Stream mapMulti over flatMap

I have been skimming through the news and the source code of the newest LTE Java 17 version and I have encountered with new Stream method called mapMulti. The early-access JavaDoc says it is similar to flatMap. Stream mapMulti(BiConsumer
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
24
votes
5 answers

How to use swift flatMap to filter out optionals from an array

I'm a little confused around flatMap (added to Swift 1.2) Say I have an array of some optional type e.g. let possibles:[Int?] = [nil, 1, 2, 3, nil, nil, 4, 5] In Swift 1.1 I'd do a filter followed by a map like this: let filtermap =…
23
votes
2 answers

Why is this usage of Stream::flatMap wrong?

I expected to be able to use Stream::flatMap like this public static List duplicate(String s) { List l = new ArrayList(); l.add(s); l.add(s); return l; } listOfStrings.stream().flatMap(str ->…
Simon
  • 6,293
  • 2
  • 28
  • 34
19
votes
3 answers

Javascript - flatMap method over array - (flatMap is not a function)

According to the Mozilla Developer Website: The flatMap() method first maps each element using a mapping function, then flattens the result into a new array. It is identical to a map followed by a flat of depth 1, but flatMap is often quite useful,…
mph85
  • 1,276
  • 4
  • 19
  • 39
19
votes
3 answers

PySpark Throwing error Method __getnewargs__([]) does not exist

I have a set of files. The path to the files are saved in a file., say all_files.txt. Using apache spark, I need to do an operation on all the files and club the results. The steps that I want to do are: Create an RDD by reading all_files.txt For…
UnderWood
  • 803
  • 3
  • 12
  • 23
18
votes
3 answers

Stream difference between Java 8 and 11

Consider this code: public static void main(String[] args) { Stream.iterate(1, i -> i + 1) .flatMap(i -> Stream.of(i, i, i)) .peek(System.out::println) .limit(4) .forEach(i -> {}); } The output in…
DodgyCodeException
  • 5,963
  • 3
  • 21
  • 42
18
votes
4 answers

Flatmap nested collection

I have a list of objects, some of them can be collections. I would like to get a stream of plain objects. List objects = List.of(1, 2, "SomeString", List.of(3, 4, 5, 6), 7, List.of("a", "b", "c"), List.of(8, List.of(9,…
Yan Khonski
  • 12,225
  • 15
  • 76
  • 114
18
votes
4 answers

Swift: Flatten an array of dictionaries to one dictionary

In Swift, I am trying to flatten an array of dictionaries into one dictionary i.e let arrayOfDictionaries = [["key1": "value1"], ["key2": "value2"], ["key3": "value3", "key4": "value4"]] //the end result will be: flattenedArray = ["key1":…
Lneuner
  • 1,090
  • 1
  • 9
  • 19
1
2 3
36 37