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

Java 8 Stream implementation for nested documents returned from MongoDB

I have a MongoDB collection of nested documents as follows. I am trying to read the tags in TestCase object. Right now my implementation is really basic with 4 for loops. I am new to java8 and I would like to use streams or any simple approach to…
kvm006
  • 4,554
  • 2
  • 18
  • 22
0
votes
1 answer

RxJava 2 Nested Network Requests

in the app I am currently working on I use retrofit to create an Observable >. Party has a hostId field as well as a field of type User which is null at the point of creation by Retrofits GsonConverter. I now want to use hostId to…
Wiintend0
  • 23
  • 4
0
votes
1 answer

Swift: create a flat array of views from view hierarchy

I have a view hierarchy and I need to flatten it into a linear array, say: + rootView 0 - subView 1 - subview 2 + subview 3 - subview 3.1 - subview 3.2 - subview 4 I need an array of: [0, 1, 2, 3, 3.1, 3.2, 4] I wondered if map or…
Heuristic
  • 5,087
  • 9
  • 54
  • 94
0
votes
0 answers

RxPy: How to convert stream of sets of elements into a stream of single elements

I have two pieces of code s.t. - one produces stream of sets of active alerts in the system. - second consumes events of raise/fall of an alert. assuming the first part produces the following stream ["a", "b"], ["c"], ["e", "f", "g"], I want to push…
0
votes
1 answer

RxJava Zip not working properly

I am trying to implement this complex API structure. So I tried to implement it with RxJava2 zip for parallel requests private fun getDetails(marketDataQuotes: MarketDataQuotes, instrumentById: InstrumentById, subscribe: Subscribe):…
Rupesh
  • 51
  • 2
  • 10
0
votes
0 answers

combined flatmap and forkjoin in one call angular

i have to do 5 api call, the first 2 are in sequence than the other 2 are in parallel and the last one come after the 2 in parallel, i'm trying to use flatmap + forkjoin but it seems that is not possible combine all in one call is there any other…
zanza67
  • 223
  • 4
  • 20
0
votes
4 answers

How to use Java streams to combine child generated lists into a single list?

In my code below, I don't like that I have the List list defined outside of the stream. I'm pretty sure there is a way to have the stream processing return a List list directly, I'm just not sure how to do this. …
Michael Remijan
  • 687
  • 7
  • 22
0
votes
1 answer

Interact with multiple files in Spark - Java code

I am new to Spark. I am trying to implement spark program in java. I just want to read multiple files from a folder and combine altogether by pairing its words@filname as key and value(count). I don't know how to combine all data together.. and I…
user3152493
  • 21
  • 1
  • 5
0
votes
2 answers

How to apply a function to a list of lists

I know of the flatMap operation but don't fully understand how it works. I have a class: class Days() { List dates; } Then I have a list of these objects List month And I want to do something to each of the dates in this object (for…
TovrikTheThird
  • 471
  • 2
  • 7
  • 20
0
votes
1 answer

How to simplify method using Option or PartialFunction

I need some help to modify(simplify) my code. Here an example: def getBids(rawBids: RDD[List[String]], exchangeRates: Map[String, Double]): RDD[BidItem] = { rawBids.filter(bidList => !bidList(2).matches(ERROR_REGEXP)) .flatMap(bidList =>…
Pavel Orlov
  • 23
  • 2
  • 6
0
votes
0 answers

flatMapIterable is not emitting object in list

Thanks in advance for help. I have a scenario, where I am taking an input of order details(includes order object and list of orderItem object). My aim is to first create an order and then use the orderId generated to create orderItems. below is the…
0
votes
1 answer

How to use flatMapValues on Kafka

I am getting an error when using flatMapValues in Scala with Kafka library. Here is my code: val builder: KStreamBuilder = new KStreamBuilder() val textLines: KStream[String, String] = builder.stream("streams-plaintext-input") import…
Felipe
  • 7,013
  • 8
  • 44
  • 102
0
votes
1 answer

Flattening nested observables and include regular variables in outer observable

Sorry to clarify, the following data structure: const obs = Observable.fromPromise(firebase.storage().ref('Test').child('logo.png').getDownloadURL()); const json = { obs: obs, test: 1 }; const obs2 = Observable.of( json ); So that would…
smeddles24
  • 101
  • 1
  • 1
  • 8
0
votes
1 answer

how use Optional with cascaded objects not created with Optional

Context: I have chained objects generated by wsdl2java so none of them contains java.util.Optional. There is an already created method that call the soap web services, receives the xml and unmarshalling it in cascaded objects. Desire: I want to use…
Jim C
  • 3,957
  • 25
  • 85
  • 162
0
votes
1 answer

PySpark equivalent of Flatmapgroups RDD

I have a data like below: +----+----+ |user|item| +----+----+ | a| 1| | a| 2| | a| 3| | b| 1| | b| 5| | b| 4| | b| 7| | c| 10| | c| 2| +----+----+ I would like to have the data after some transformation like as…