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
4
votes
2 answers

Scala: flatMap with tuples

Why is the below statement valid for .map() but not for .flatMap()? val tupled = input.map(x => (x*2, x*3)) //Compilation error: cannot resolve reference flatMap with such signature val tupled = input.flatMap(x => (x*2, x*3)) This statement has…
Somasundaram Sekar
  • 5,244
  • 6
  • 43
  • 85
4
votes
2 answers

Swift flatMap and generics

I'm trying to use flatMap to build a Resource in Swift, but keep getting a strange error, and only works when I force the cast. Resource: public struct Resource { let record: CKRecord let parser: [String: AnyObject] -> T? } Working…
Victor
  • 137
  • 1
  • 11
4
votes
1 answer

How to recursively flatMap a stream?

I was asked to retrieve every leaf node that is a descandant of a tree node. I quickly got the idea that I could do this job in one line! public Set> getLeaves() { return getChildrenStream().flatMap(n ->…
glee8e
  • 6,180
  • 4
  • 31
  • 51
4
votes
5 answers

Which happens first in flatMap, flatten or map?

If I use xxx.flatMap(_.split(" ")), will it split the array an then flatten or will it flatten and then split?
马慧超
  • 183
  • 1
  • 10
4
votes
2 answers

Java flatMap - whats the difference stream.of() and collection.stream()

I'm trying to understand flatMap: flatMap(x->stream.of(x) ) does not flat the stream and flatMap(x->x.stream()) works and gives the desired result. Can someone explain the difference between two? import java.util.*; import java.util.stream.*; class…
puvi
  • 231
  • 2
  • 10
4
votes
1 answer

Flatten the map and associate values using Java 8 Stream APIs

Suppose that I have a map of a set of strings to an integer value: Map, Integer> map = new HashMap<>(). For example, the map is (we assume no duplicate strings): {x,y} -> 2 {z} -> 3 {u,v,w} -> 4 How can I get another_map…
hengxin
  • 1,867
  • 2
  • 21
  • 42
4
votes
4 answers

Scala - flattening a tree structure

I have a tree structure I'm receiving from a Java library. I am trying to flatten it since I'm interested only in the "key" values of the tree. The tree is made up of zero or more of the following classes: class R(val key: String, val nodes:…
Will I Am
  • 2,614
  • 3
  • 35
  • 61
4
votes
2 answers

Composing streams with flatmap in Java 8

Let's consider I have the following class: class A { int i, j, k; public A(int i, int j, int k) { this.i = i; this.j = j; this.k = k; } } where i, j, k have a known range: r_i, r_j, r_k. Now I want to to generate all possible…
Wickoo
  • 6,745
  • 5
  • 32
  • 45
4
votes
3 answers

Spark: How to transform a Seq of RDD into a RDD

I'm just starting in Spark & Scala I have a directory with multiple files in it I successfully load them using sc.wholeTextFiles(directory) Now I want to go one level up. I actually have a directory that contains sub directories that contain…
Stephane Maarek
  • 5,202
  • 9
  • 46
  • 87
4
votes
2 answers

How to flatMapToLong a Stream>?

I have this method: public static long sumDigits(final List list) { return list .stream() .map(l -> toDigits(l)) .flatMapToLong(x -> x.stream()) .sum() } toDigits has this…
mrt181
  • 5,080
  • 8
  • 66
  • 86
4
votes
1 answer

Java 8 Optional and flatMap - what is wrong?

Some piece of code: public class Player { Team team; String name; } public class Team { List players; } public class Demo { @Inject TeamDAO teamDAO; @Inject PlayerDAO playerDAO; List
Dariusz Mydlarz
  • 2,940
  • 6
  • 31
  • 59
3
votes
4 answers

List of List of Lists (Flatten a ArrayList with N-Depth)

I am trying to Flatten a ArrayList with N-Depth. For this I tried using flapMap method of Stream API. I am able to get it. But I have to use flatMap() method repeatedly as per the number of list of lists. If I am using one more flatMap() method, it…
Klaus
  • 59
  • 1
  • 5
3
votes
2 answers

Why does Finatra use flatMap and not just map?

This might be a really dumb question but I am trying to understand the logic behind using #flatMap and not just #map in this method definition in Finatra's HttpClient definition: def executeJson[T: Manifest](request: Request, expectedStatus: Status…
3
votes
1 answer

How to add more rows in pyspark df by column value

I'm stuck with this problem quite a while and probably making it bigger than really it is. I will try to simplify it. I'm using pyspark and data frame functions along my code. I already have a df as: +--+-----+---------+ |id|col1 |col2 …
3
votes
2 answers

How to flatMap cats Applicatives

I've started learning functional programming with Cats and I stuck with flatMapping (merging) applicatives F[List]. What is very simple in pure Scala is flatmapping list of lists like that: val animals = List("Dog", "Cat", "Bird") def…