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
14
votes
5 answers

Why do we need flatMap (in general)?

I have been looking into FP languages (off and on) for some time and have played with Scala, Haskell, F#, and some others. I like what I see and understand some of the fundamental concepts of FP (with absolutely no background in Category Theory -…
melston
  • 2,198
  • 22
  • 39
13
votes
3 answers

Is flatMap guaranteed to be lazy?

Consider the following code: urls.stream() .flatMap(url -> fetchDataFromInternet(url).stream()) .filter(...) .findFirst() .get(); Will fetchDataFromInternet be called for second url when the first one was enough? I tried with a…
balki
  • 26,394
  • 30
  • 105
  • 151
12
votes
3 answers

Is there any difference between flatten and flatMap(identity)?

scala> List(List(1), List(2), List(3), List(4)) res18: List[List[Int]] = List(List(1), List(2), List(3), List(4)) scala> res18.flatten res19: List[Int] = List(1, 2, 3, 4) scala> res18.flatMap(identity) res20: List[Int] = List(1, 2, 3, 4) Is there…
user3335040
  • 649
  • 1
  • 7
  • 17
11
votes
2 answers

Spark: FlatMapValues query

I'm reading the Learning Spark book and couldn't understand the following pair rdd transformation. rdd.flatMapValues(x => (x to 5)) It is applied on an rdd {(1,2),(3,4),(3,6)} and the output of the transformation is…
Vinay
  • 1,473
  • 4
  • 14
  • 24
10
votes
1 answer

Why can't Stream.flatMap accept a collection?

Given the following as an example of data classes: class Country { List regions = new ArrayList<>(); List getRegions() { return regions; } } class Region { String getName() { return "some name"; …
Lukas
  • 244
  • 2
  • 13
9
votes
3 answers

Swift: Lazily encapsulating chains of map, filter, flatMap

I have a list of animals: let animals = ["bear", "dog", "cat"] And some ways to transform that list: typealias Transform = (String) -> [String] let containsA: Transform = { $0.contains("a") ? [$0] : [] } let plural: Transform = { [$0 + "s"]…
Cortado-J
  • 2,035
  • 2
  • 20
  • 32
9
votes
3 answers

Swift flatMap gives unexpected result while using with optional array

We have an array of the Person objects and each object has another array of String, which is optional. We want the consolidated list of car names in our society. struct Person { let name: String let address: String let age: Int let…
Santosh Botre
  • 549
  • 5
  • 21
9
votes
1 answer

RxJS/Observable flatMap can return Observable or array

Can someone explain to me why the .flatMap operator can accept a function which returns an Observable, or an array? The official docs say: The FlatMap operator transforms an Observable by applying a function that you specify to each item emitted…
Dolan
  • 1,519
  • 4
  • 16
  • 36
9
votes
5 answers

deep flatten all items in collection using lodash

I'm looking to flatten an array that look like this: [{ "id": 0, "text": "item 0" }, { "id": 1, "items": [{ "id": 2, "text": "item 2" }, { "id": 3, "items": [{ "id": 4, …
nox
  • 331
  • 1
  • 4
  • 13
8
votes
1 answer

Using flatMap with dictionaries produces tuples?

I have this pretty basic and straight forward mapping of an object into dictionary. I am using and parsing dictionary on the top level. One of its fields is an array of other dictionaries. To set them I use flatMap which seems an appropriate method…
Matic Oblak
  • 16,318
  • 3
  • 24
  • 43
8
votes
3 answers

convert list of map to map using flatMap

How I can merge List> to Map using flatMap? Here's what I've tried: final Map result = response .stream() .collect(Collectors.toMap( s -> (String) s.get("key"), s -> (String)…
math
  • 111
  • 1
  • 1
  • 2
8
votes
2 answers

Create and Invert MultiMap w/ Java 8 Streams

How can I convert a Set into a Map> or SetMultimap using Java 8 streams or Multimaps, where Result is: class Result { String name; Set items; } For example, I start with: result1: name:…
mathematician
  • 1,942
  • 5
  • 19
  • 22
7
votes
1 answer

flatten Vs flatMap with def method and val function

flatten Vs flatMap with def method and val function: I defined a def method called toInt: def toInt(s: String): Option[Int] = { try { Some(Integer.parseInt(s.trim)) } catch { case e: Exception => None } } And this method…
Kiran
  • 73
  • 4
7
votes
1 answer

Keeping an index with flatMap in Swift

This is a follow-up to this question: flatMap and `Ambiguous reference to member` error There I am using the following code to convert an array of Records to an array of Persons: let records = // load file from bundle let persons =…
koen
  • 5,383
  • 7
  • 50
  • 89
7
votes
3 answers

Replace nested loop with Java 8 flatmap

I'm trying to use flatmap to make a nested loop with the Stream API, but I can't seem to figure it out. As an example, I want to recreate the following loop: List xs = Arrays.asList(new String[]{ "one","two", "three"}); List ys =…
wvdz
  • 16,251
  • 4
  • 53
  • 90
1
2
3
36 37