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

spark rdd how to flat Key - Array(value) pair

I have a park paired rdd (key, Array(value1, value2, value3.....)). what scala statement should I wrote? How could I flat this rdd and creating a new rdd : (key, value1), (key, value2), (key, value3)....
sickcat
  • 11
  • 2
  • 2
0
votes
0 answers

Why is the flatmap-operator not working as I understood it?

fetch(url) returns a promise, which I'm converting to observable using flatmap. Why is this not working? Rx.Observable .from(imgSrc) .map(url => fetch(url)) .flatmap( res => R.Observable.fromPromise(res)) .subscribe( …
0
votes
1 answer

Returning value from a Scala Future

In the below code, I'm trying to do two operations. One, to create a customer in a db, and the other, to create an event in the db. The creation of the event, is dependent on the creation of the user. I'm new to Scala, and confused on the role of…
Rafa
  • 3,219
  • 4
  • 38
  • 70
0
votes
1 answer

How can I chain a subscription to a url parameter to a subscription in a service?

I am trying to create a component that subscribes to the id url parameter in the activated route, and then uses it to call a service of mine that makes an http request to a server with the id. I have a route that takes in an id like so -…
LayfieldK
  • 1,086
  • 1
  • 8
  • 9
0
votes
0 answers

Filter data from a filtered array

I'm just beginning to understand the use of flatMap, etc. To make filters to my arrays. The problem is that I don't understand well how to make filters within others to make my search more efficient, specifically I need to extract the elements of…
0
votes
1 answer

Unsupported Exception while modifying list inside flatmap function

I get an Unsupported Operation Exception when trying to modify a list from inside "apply" method of flatmap in RxJava2. compositeDisposable.add(createObservable() .flatMap(new Function, ObservableSource>>() { …
Gaurav
  • 667
  • 2
  • 13
  • 28
0
votes
1 answer

JSON and flatMap - error Computed property must have an explicit type

I am having a difficulty with flatMap on returned json data. How can I Implement flatMap in the following code. I keep receiving an error: Computed property must have an explicit type let task = URLSession.shared.dataTask(with: request) { data,…
Luke
  • 407
  • 2
  • 10
  • 22
0
votes
2 answers

Why flatMap is implemented with merge in RxJava?

Why is RxJava 1.x flatMap() operator is implemented with merge? public final Observable flatMap(Func1> func) { if (getClass() == ScalarSynchronousObservable.class) { return…
Kurovsky
  • 1,081
  • 10
  • 25
0
votes
1 answer

Scala - How to convert Map[Int,List[List[IntVar]]] to

I am new in Scala programming and the flatmap function gives me headache. Before you ask me to search, I made some research on Stackoverflow : https://stackoverflow.com/search?q=scala+map+to+list but I didn't find something to solve easily my…
jy95
  • 773
  • 13
  • 36
0
votes
1 answer

Reverse a word-frequency map in Scala

I have a word-frequency array like this: [("hello", 1), ("world", 5), ("globle", 1)] I have to reverse it such that I get frequency-to-wordCount map like this: [(1, 2), (5, 1)] Notice that since two words ("hello" and "globe") have the…
Darth.Vader
  • 5,079
  • 7
  • 50
  • 90
0
votes
2 answers

How to open an Option[Map(A,B)] in Scala?

I've done enough Scala to know what ugly code looks like. Observe: val sm Option[Map[String,String]] = Some(Map("Foo" -> "won", "Bar" -> "too", "Baz" -> "tree")) Expected output: : String = Foo=won,Bar=too,Baz=tree Here's my Tyler Perry code…
dlite922
  • 1,924
  • 3
  • 24
  • 60
0
votes
1 answer

Create Spark dataset with parts of other dataset

I'm trying to create a new dataset by taking intervals from another dataset, for example, consider dataset1 as input and dataset2 as output: dataset1 = [1, 2, 3, 4, 5, 6] dataset2 = [1, 2, 2, 3, 3, 4, 4, 5, 5, 6] I managed to do that using arrays,…
0
votes
1 answer

Immutable.js and flatMap equivalent function

Was just wondering if there an equivalent flatMap function in Immutable.js? I have been using this node package https://www.npmjs.com/package/flatmap for a while but I would prefer to write things like this listObject.flatMap(x => ...) Instead…
foomip
  • 574
  • 7
  • 7
0
votes
1 answer

How to express the following nested futures with for-comprehension

My question: is it possible to simplify this nested expression with for-comprehension ? run(dbAction).flatMap(insertedJobs => { Future.sequence(insertedJobs.map { job => recordingBean.findStreamsForInterval(job.mediaSource, job.begin,…
Slow Harry
  • 1,857
  • 3
  • 24
  • 42
0
votes
3 answers

Flattening the key of a RDD

I have a Spark RDD of type (Array[breeze.linalg.DenseVector[Double]], breeze.linalg.DenseVector[Double]). I wish to flatten its key to transform it into a RDD of type breeze.linalg.DenseVector[Double], breeze.linalg.DenseVector[Double]). I am…
Armand Grillet
  • 3,229
  • 5
  • 30
  • 60