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
3
votes
1 answer

3 nested for-each loops as Java Stream (or better parallel stream)

On the project I am currently working on, we have this construct of three nested for loops: List resultList = new ArrayList<>(); for (OrgStructureEntity product : products) { for (String region : regions) { …
3
votes
1 answer

What is the significance of `flatmap` in SICP?

(define (accumulate op initial sequence) (if (null? sequence) initial (op (car sequence) (accumulate op initial (cdr sequence))))) (define (flatmap proc seq) (accumulate append nil (map proc seq))) The above is a code…
Jack Black
  • 65
  • 5
3
votes
2 answers

scala flatMap flatten nested lists

I'm learning Scala and going through 99 Scala problems. For the following exercises: Flatten a nested list structure. Example: scala> flatten(List(List(1, 1), 2, List(3, List(5, 8)))) res0: List[Any] = List(1, 1, 2, 3, 5, 8) The solution is given…
Runner Bean
  • 4,895
  • 12
  • 40
  • 60
3
votes
1 answer

FlatMap And Init in Swift

Struct I tried to recreate the codes in demo given by Chris Edihoff in dotSwift 2016 Demo. Here is the code. struct List { let name:String let id:Int } extension List { init?(json: [String:AnyObject]) { guard let…
Prajeet Shrestha
  • 7,978
  • 3
  • 34
  • 63
3
votes
1 answer

RxJava - flatmap vs concatMap - why is ordering the same on subscription?

According to this thread conCatMap and flatmap only differ by the order in which items are emitted. So i did a test and created a simple stream of integers and wanted to see in what order they would be emitted. I made a small observable that would…
j2emanue
  • 60,549
  • 65
  • 286
  • 456
3
votes
2 answers

Why is flatMap on a Vector[Option[Int]] whose mapper function result is not a Vector[Option[Int]] valid?

For example, Vector(Some(1), Some(2), Some(3), None).flatMap{ n => n } produces a Vector(1, 2, 3) instead of giving an error. As I have seen in other languages, flatMap is used when you have a mapper function that produces nesting so I would…
Cal
  • 734
  • 1
  • 9
  • 25
3
votes
3 answers

What does flatMap do exactly?

For a coding assignment for school I have to do stuff with flatmap but I have absolutely no idea what it does and I've read a couple pages online and read in my textbook but I still have no true understanding of what it does. I know what map does…
chevybow
  • 9,959
  • 6
  • 24
  • 39
3
votes
2 answers

Java 8 Stream flatMap and group by code compiler error

// given a set of Item objects, group them by the managers of creator and owners Map> managersItems = itemSet.parallelStream().flatMap(item -> { // get the list of the creator and owners List users =…
derrdji
  • 12,661
  • 21
  • 68
  • 78
3
votes
1 answer

Why range() and sequenceOf() behave differently with flatMap in RxSwift

I’m new to Swift and ReactiveX. I feel difficult in understanding flatMap. In my tests to learn flatMap, I used range() and sequenceOf() respectively to create observables. But why the outputs are unexpectedly different as seen below: import…
3
votes
1 answer

Usage of Spark DataFrame.flatMap in java

I have worked with RDD.flatMap function in java. Now trying my hands on DataFrames. They say: public RDD flatMap(scala.Function1> f, scala.reflect.ClassTag
3
votes
1 answer

How to convert this map/flatMap into a for comprehension in Clojure?

Given this Scala code: def compute2(maybeFoo: Option[Foo]): Option[Int] = maybeFoo.flatMap { foo => foo.bar.flatMap { bar => bar.baz.map { baz => baz.compute } } } Which is then translated to this for…
hawkeye
  • 34,745
  • 30
  • 150
  • 304
2
votes
1 answer

Is Reactor's FlatMap Asynchronous?

I'm new to reactive programming and I'm using reactor through micronaut framework and kotlin. I'm trying to understand the advantages of reactive programming and how we implement it using Map and FlatMap through Mono and Flux. I understand the…
2
votes
2 answers

Obtain from a List custom Objects of a List of distinct String values of Two String fields of these Objects using Java-Streams

Java stream on specific fields in a custom class object I have an ArrayList of Train objects. Each Train has three fields: source, destination, cost. I want to get all the place names, i.e. all distinct sources + destinations. I am using the below…
potato
  • 37
  • 3
2
votes
0 answers

Is it safe to use df.select(column_name).rdd.flatMap(lambda x: x).collect() when the order is important?

As part of investigation I came to this link: flatMap doesn't preserve order when creating lists from pyspark dataframe columns which would suggest that it is not safe. However, these links state that flatMap() preserves order: Does flatMap keep the…
MichiganMagician
  • 273
  • 2
  • 15
2
votes
1 answer

Spark: Extract Values from Output RDD

I am new in Spark programming. I am trying to extract values from RDD as I got the below output from RDD (CBI10006,(Some(Himanshu Vasani),None)) (CBI10004,(Some(Sonam Petro),Some(8500))) (CBI10003,(None,Some(3000))) And I want to extract above…