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

PySpark: RDD how to reducebykey on a list of dictionaries?

I have a simple paired word counter problem in PySpark: This is the input as an RDD: [' the adventure of the blue carbuncle the adventure of the blue carbuncle the adventure of the blue carbuncle ',' the adventure of the blue carbuncle'] I've…
Teddy
  • 21
  • 3
2
votes
1 answer

Updating webclient response from database

I have situation where I can't update column at database. The problem is that only method setIsPurchased is executed and the flag is_purchase at SQL table changed to true, but the next line with setPurchasedDate which has to set current time is not…
2
votes
2 answers

Akka Streams (Scala): Filtering out exceptions

One of the steps in my akka streams pipeline is a transformation that throws an exception when it receives an invalid input. I would like to discard these problematic inputs. So, I came up with the following solution: ... .map( input => Try(…
david
  • 309
  • 1
  • 4
  • 15
2
votes
1 answer

How to avoid .subscribe in subscribe in RxJava

I have a code snippet using Rxjava and I have written it using subscribing inside a subscription. How can I use the only one subscribe using maps or flatmaps? amount.textChanges.observable.map { when { (it.isNotEmpty() &&…
2
votes
1 answer

Why flatMap doesn't remove empty arrays?

I have a flatMap function that fetch and return data for each item in array. It also returns an empty array if item doesn't have certain informations. Since I'm using flatMap I was expecting that it will remove all the empty arrays, but the finished…
2
votes
2 answers

transform distinct row values to different columns with corresponding rows using Pyspark

I'm new to Pyspark and trying to transform data Given dataframe Col1 A=id1a A=id2a B=id1b C=id1c B=id2b D=id1d A=id3a B=id3b C=id2c A=id4a C=id3c Required: A B C id1a id1b id1c id2a id2b id2c id3a id3b …
Murali Sai
  • 23
  • 3
2
votes
4 answers

flatMap function in scala

In the below two scenarios, I have flatMap function invoked on a list. In both the cases, the map portion of the flatMap function returns an array which has a iterator. In the first case, the code errors out where in the second case, it produces the…
user3103957
  • 636
  • 4
  • 16
2
votes
3 answers

how to iterate over array[string] in spark scala?

enter image description herehere is my sample input: val list=List("a;bc:de;f","uvw:xy;z","123:456") I am applying following operation val upper=list.map(x=>x.split(":")).map(x=>x.split(";")) but it is throwing error- error: value split is not a…
Aditya Ranjan
  • 135
  • 1
  • 2
  • 11
2
votes
1 answer

Instance method 'flatMap' requires that 'String' conform to 'ObservableConvertibleType'

I am getting below build error for RxSwift, func testFlatMap() { let bag = DisposeBag() let subject = PublishSubject.init() subject.flatMap({ (value) -> String in PublishSubject.just(value) }).subscribe( …
Sazzad Hissain Khan
  • 37,929
  • 33
  • 189
  • 256
2
votes
2 answers

Java stream flatMap with keeping first and second level objects in stream

I was wondering how I could use Java Stream API to flatten a structure where I have an object and a collection of the same type objects nested in it. What I mean is that I have a Component class which has a field of type List. What I…
furry12
  • 902
  • 1
  • 14
  • 31
2
votes
2 answers

Retrofit - android.os.NetworkOnMainThreadException with RxKotlin

I've created rx function to call a network call from view-model in android, it parses network on main thread function. I just change few line of code it worked. but i need to know the reason for this because its use same builder pattern to create a…
2
votes
1 answer

Bad return type in method reference: Cannot convert Employee to Optional

I am trying to write a lambda function that gets employee location preference and have the code sample below. But for my lambda function I get a compilation error at flatMap(this::buildEmployeeGeolocation) saying Bad return type in method reference:…
koustubhC
  • 157
  • 2
  • 15
2
votes
0 answers

Handeling errors in flatmap on rdd pyspark/python

I am using a user-defined function (readByteUFF) to read file, perform transform the content and return a pyspark.sql Row. I use this function on an rdd (which is a large collection of files that should follow the same pattern) in the following…
Martin Petri Bagger
  • 2,187
  • 4
  • 17
  • 20
2
votes
2 answers

A typical scenario of using flatMap in State Monad?

I read the definition of a State Monad as follows, which includes a definition of flatMap. The literal definition of flatMap is clear to me, but what would be a typical use case of it? trait State[S,A]{ def run (initial:S):(S,A) def flatMap[B]…
zell
  • 9,830
  • 10
  • 62
  • 115
2
votes
1 answer

Map an instance containing a list to a flatMap (using stream)

i have the following class 'Store': public class Store { private String storeId; private String storeName; private List openiningDays; } (e.g.:) { storeId: 1 storeName: blabla openingDays:…
user8166384