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

Flatten Array of Any

How to flatten an array of Any in Swift 4. Example: Let's suppose I have an array: var array:[Any] = [1, 2, [4, 3], [9, [8, 0]]] and after flatten this array, my expected result is [1, 2, 4, 3, 9, 8, 0]. I try to use flatMap but it didn't work.
lee
  • 7,955
  • 8
  • 44
  • 60
0
votes
1 answer

angular2 pass result from http request into mergemap http request

I am trying to get the result of one method that returns an observable to pass into the second request this.runfirstmethod(headers).mergeMap(response => this.http[verb](url, response).map(res => res.json())) This is everything i am doing, I think i…
user1552172
  • 614
  • 1
  • 9
  • 27
0
votes
1 answer

Observables: flatMap randomly not triggered

I'm facing a random behavior with the flatMap operator and I can't find the reason why. Sometimes it triggers, sometimes it doesn't... Here is the situation: The user can change the language in my app, so I have a BehaviorSubject on the language…
Maslow
  • 1,084
  • 9
  • 22
0
votes
2 answers

Scala FlatMap returning a vector instead of a String

I am following martin odesky course. And there is example where he applies flatMap to String and gets a string in return but I am getting a Vector. Here is the code that I am using val str = "Hello" println(str flatMap (x => List("." ,…
pannu
  • 518
  • 7
  • 20
0
votes
1 answer

Angular 2 - Avoid delay when chaining http request

this.http.get(validate\uniqueness).subscribe(unique => { console.log(" first subscribe "); if(unique.isValid) { this.http.put(url, data).subscribe(val => { console.log("second Subscribe") } …
user2810022
  • 31
  • 1
  • 4
0
votes
1 answer

Filtering RDD computation dataset

I was practicing with apache spark and i tried doing some computations. Although, i was able to achieve my desired result, but i had to try two different methods before it worked. I have an existing dataset which i created an RDD from. "RT…
PythonRookie
  • 301
  • 1
  • 5
  • 20
0
votes
1 answer

Strange behavior of Rx.Observable.prototype.fromEvent()

Today I've seen a strange problem when using RxJS. Please help me inspect it. The problem I am working on is: "Given an array of image URLs, load and append all images to a div." All the code snippets to demonstrate is here:…
thongncvn
  • 13
  • 1
0
votes
2 answers

Scala: Use map function tuples in a subsequent flatMap

I want to use a tuple of a map function in a subsequent flatMap. Like this: val list = orders.map(ord => (ord.prod.tasks, ord.quantity)) .zipWithIndex flatMap { case (variable, index) => …
J Costa
  • 23
  • 1
  • 3
0
votes
3 answers

How can you compose dependent futures in Scala so that all but one run in parallel?

For example you can do: Future(/* A */).flatMap(_ => Future(/* B */).flatMap(_ => Future(/* C */))) Here B depends on A to complete, is there a clean way to compose the futures so that C depends on both A and B to complete and A and B can run in…
0
votes
1 answer

RxJava concatMap no response

Hope you guys are doing well, I have been working on a personal Android Project using RxJava and Retrofit. It's a search request for GitHub issues, I am getting input from the user when he clicks Search Button and using a PublishSubject object to…
savvisingh
  • 93
  • 7
0
votes
1 answer

using flatmap to make nested service call with parameter

I am making a service call returning data from json file with a bunch of items but after get all the items i need to make another service call to get the contents of each of the items. I am using a flatmap but i am having some trouble on how to pass…
bluePearl
  • 1,237
  • 4
  • 24
  • 42
0
votes
2 answers

Swift: Reduce optional array of models to single Bool by optional property

I just learned about the wonderful world of map, flatMap and reduce in swift and I already use it where ever it makes sense and helps improve my code. Now I encountered a very special problem and I wonder if there is a solution to this using map,…
xxtesaxx
  • 6,175
  • 2
  • 31
  • 50
0
votes
3 answers

Angular 4 / RxJS, send HTTP of an array in sequence

Trying to http post items of an array one after another in sequence. Knowing should use RxJS flatMap but couldn't make it work. Basically I need something like this: item is an element of an array named items, want to go through the array and send…
brewphone
  • 1,316
  • 4
  • 24
  • 32
0
votes
3 answers

Filter two Future[Seq] with for comprehension scala

I have some functions which returns list of tuple elements(Int, Int) with future. For simplicity I will define two futures val f1 = Future { List((8, 3), (2, 1), (4, 2), (3, 4)) } val f2 = Future { List((2, 3), (5, 1), (7, 9)) } I…
eranga
  • 519
  • 7
  • 17
0
votes
3 answers

Filter out strings that cannot be converted to ints using closures in swift

So if I had an array of strings and I wanted to pull out all of the integers (as Strings), how could I do this? For example: myArray = ["1","2","3","unknown","bob"] I've tried myArray.filter { Int($0) } But I get an error telling me I can't…
user5154504