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

Java 8 List of list of list of Objects flatmap fetch the inner most list

I am not so familiar with Java 8 (still learning) and looking to see if I could find something equivalent of the below code using flatMap. I use lombok @Builder (you can ignore that part) Few checks that we might need - Lists could be empty A rough…
meso
  • 493
  • 2
  • 5
  • 15
2
votes
1 answer

for-comprehension returns None if at least one component in `for` is None

I have difficulty understanding the mechanism using for-compression in Scala. For example, if I have val x = for { i <- Option(1) j <- Option(2) k <- Option(3) } yield (i,j,k) x is x: Option[(Int, Int, Int)] = Some((1,2,3)). However, if at…
helloworld
  • 613
  • 8
  • 24
2
votes
1 answer

How do I explode a dataset with flatmap?

I have a sequence of a case class, which has a String followed by a sequence of Strings. How do I flatmap the sequence of Strings (second column) without losing the first column? I tried this: flatmap(_.second) But in this case I lose the first…
Ulrich Zendler
  • 141
  • 1
  • 10
2
votes
1 answer

Running await inside RxJS Observable flatMap

I am trying to return an observable inside an async arrow function passed to a flatMap, but the returned observable is not being called. protected buildUseCaseObservable(params: LoginUserParams): Observable { return…
dieppa
  • 166
  • 1
  • 9
2
votes
1 answer

Getting different results using flatMap and compactMap

I have Array A of objects of type struct struct Caste { var arr = [1,2] } let siri = [Caste(), Caste(), Caste()] Now I want a single array in which all elements of each objects array consist as shown below: let re1 = siri.compactMap { $0.arr…
Abhishek Thapliyal
  • 3,497
  • 6
  • 30
  • 69
2
votes
1 answer

Spark: Flatten simple multi-column DataFrame

How to flatten a simple (i.e. no nested structures) dataframe into a list? My problem set is detecting all the node pairs that have been changed/added/removed from a table of node pairs. This means I have a "before" and "after" table to compare.…
joynoele
  • 393
  • 4
  • 16
2
votes
2 answers

How to simplify nested ajax subscribe call for Angular 6

getBudgetMap(budgetMonthID){ this.rows = []; this.categoryService.getCategory(budgetMonthID).subscribe(category =>{ this.categoryList = category; this.categoryList.forEach(category => { …
Raymond Tey
  • 464
  • 3
  • 11
2
votes
1 answer

Nested subscribe and need all values to pass as body to API - Angular 6 RxJS

I am developing a table component and the data for the table component is to be populated on basis of three dropdown values. I need to pass in all three values to the API to get the desired response. I can achieve it using nested subscribes, which…
arunmmanoharan
  • 2,535
  • 2
  • 29
  • 60
2
votes
1 answer

Swift 4 - FlatMap does not flatten array

I'm playing around with Swift and I'm having some trouble with flatMap. I've seen this StackOverflow question, which describes how to flatten an array of arrays using flatMap, but when I reproduce the same code in a playground, it does not flatten…
John
  • 143
  • 1
  • 11
2
votes
3 answers

Iterate sequence of future in sequence of futures

I am working on a PlayFramework application written in Scala. Problem is that in a rest controller I need a list of elements (books) and for each element list of its subelements (chapters). Book repository: def findAll(): Future[Seq[Book]]…
martyn
  • 136
  • 1
  • 8
2
votes
0 answers

Mapping fluent result with joins to struct

OK, so, I've been struggling with Vapor/Fluent and the more complex returns of data. I am basically trying to return the following query in fluent-mysql: select e.*, et.description, es.description from e inner join et on e.tid = et.id …
ConBran
  • 369
  • 2
  • 15
2
votes
1 answer

functional scala- how to avoid deep nesting on optional mappings

I have a set of operations that are completed in sequence, but if an intermediate sequence returns "null" I would like to abort the operation early (skip the subsequent steps). I conjured up a function like this which given an input parameter,…
Avba
  • 14,822
  • 20
  • 92
  • 192
2
votes
2 answers

Combine two observables then transform the result

I have a REST endpoint that I am calling with Angular Http. I need to chain two calls, then use the results from each to create an object to return. Pseudo code: POST new entity to server and get a UID back PATCH entity content to server, using UID…
Kildareflare
  • 4,590
  • 5
  • 51
  • 65
2
votes
2 answers

Condition the execution of a flatmap in rxjs - angular 5

In rxjs I want to make a condition over multiple flatmap so I don't execute the second and third flatmap and I go directly to the subscribe : this.authenticationAPI.getUser() .flatmap(response1 => if(response1 != null) // I want to go…
Brahim LAMJAGUAR
  • 1,254
  • 5
  • 19
  • 28
2
votes
5 answers

How to replace all vowels in a String?

I tried with this code: let vowels: [Character] = ["a","e","i","o","u", "y"] let replaced = String(myString.map { $0 == vowels.contains($0) ? "1" : "0" }) But I have the error: Binary operator '==' cannot be applied to operands of type…
cmii
  • 3,556
  • 8
  • 38
  • 69