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
3 answers

Why chain of flatMap stops on first Left value but continues on Right values

I don't understand this line: Right(1).flatMap(_ => Left(2)).flatMap(_ => Left(3)) Right(1) is passed to .flatMap(_ => Left(2). It returns Left(2) which is passed to .flatMap(_ => Left(3). And it should've returned Left(3). But it returns…
techkuz
  • 3,608
  • 5
  • 34
  • 62
3
votes
3 answers

Issue in understanding compactMap & flatMap

I have gone through from multiple tutorials that flatMap/compactMap is used to flatten an array of array but in my case it's not working or I am not understanding it properly. let myArray = [["Raja","Kumar",…
Muhammad Waqas Bhati
  • 2,775
  • 20
  • 25
3
votes
1 answer

Equivalent to pySpark flatMap in Python

I was searching for a function to flatten an array of lists. First, I implemented my solution using the Apach Spark function flatMap on RDD system, but I would like to do this locally. However, I can't manage to find the equivalent of samples =…
vftw
  • 1,547
  • 3
  • 22
  • 51
3
votes
1 answer

Is it better to use a single flatMap() over a map().flatMap()?

I'm wondering if there is any significant difference between two cases of flatmapping. Case 1: someCollection .stream() .map(CollectionElement::getAnotherCollection) .flatMap(Collection::stream); Case 2: someCollection .stream() …
Fossan
  • 76
  • 1
  • 9
3
votes
1 answer

Why does Spark JavaRDD flatmap function return an iterator

I am trying to go through the java word count example. As I understand spark RDDs are a special type of collections, and flat map basically converts a nested collection such Stream> => Stream then Why does the spark Java API in the line below need…
mdmac
  • 51
  • 1
  • 5
3
votes
1 answer

Spring reactor webclient sequential requests with flatmap and takewhile

I want to retrieve all pages from a third-party resource. To do that I wrote this: final WebClient webClient = WebClient.builder() .baseUrl("http://resource.com") .build(); Flux.fromStream(IntStream.iterate(0, i -> i + 1).boxed()) …
Slava
  • 311
  • 3
  • 9
3
votes
1 answer

How to use map / flatMap on a scala Map?

I have two sequences, i.e. prices: Seq[Price] and overrides: Seq[Override]. I need to do some magic on them yet only for a subset based on a shared id. So I grouped them both into a Map each via groupBy: I do the group by via: val pricesById =…
k0pernikus
  • 60,309
  • 67
  • 216
  • 347
3
votes
3 answers

Join/unfolded mapType column in spark back with the original dataframe

I have a dataframe in (py)Spark, where 1 of the columns is from the type 'map'. That column I want to flatten or split into multiple columns which should be added to the original dataframe. I'm able to unfold the column with flatMap, however I loose…
5nv
  • 441
  • 2
  • 15
3
votes
2 answers

RxJava Kotlin how to get separated objects from single observable

RxJava Kotlin flatmap don't return separated objects from splitted string. instead it returns List val source: Observable = Observable.just("521934/2342/FOXTROT") .flatMap{Observable.fromArray(it.split("/"))} .subscribe{Log.d(TAG,…
Denis Belfort
  • 51
  • 1
  • 4
3
votes
1 answer

Kotlin unable to infer type when using method reference in Flowable

I have this code in Java: Flowable.just(1,2,3) .flatMap(Flowable::just); and this code in Kotlin: Flowable.just(1,2,3) .flatMap(Flowable::just) While Java code compiles fine, Kotlin compiler says: "Error:(47, 30) Kotlin: One type…
3
votes
2 answers

Making nested API calls in Angular

I need to make nested http calls in my angular app. First call returns the list of venues, then using each venue id i have to make multiple api calls to get details of each venue. Here is my code, service.ts GetResponse(){ return…
user7889681
3
votes
1 answer

chaining multiple Observables sequentially

What is the best practice of chaining 5-6 Single observables that are being executing sequentially? For example I have Single1->Single2-> ... ->Single6. Single2 depends on Single1's result. Single3 depends on Single2 result, etc. I have been using…
codereviewanskquestions
  • 13,460
  • 29
  • 98
  • 167
3
votes
4 answers

Use Java 8 Optional in existing Java 7 code

I have an assignment in which I need to convert the following pre-Java 8 code to Java 8 code. Below is just one method which is giving me hard time to finish up: public static List loadMatching(Region region, String nameStartsWith,…
qrius
  • 621
  • 2
  • 9
  • 22
3
votes
3 answers

flatMap typing - attempting to merge DoubleStreams

final double[][] a = new double[][] { { 1, 2, 3 }, { 4, 5, 6 } }; final int numRows = a.length; final int numCols = a[0].length; final double[] da = IntStream.range(0, numCols) .mapToObj(i -> IntStream.range(0,…
dreamer
  • 69
  • 3
3
votes
1 answer

explode a row of spark dataset into several rows with added column using flatmap

I have a DataFrame with the following schema : root |-- journal: string (nullable = true) |-- topicDistribution: vector (nullable = true) The topicDistribution field is a vector of doubles: [0.1, 0.2 0.15 ...] What I want is is to explode each…
Kyle Wang
  • 31
  • 2