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

Scala flatmap saving to cassandra via spark in loop

I have loop with Names{Real Test!, Real Test1!} and inside it loop throw days (here as example iteration is 1 day only), in this loop some operations with data, and then I flatMap RDD(with 2 rows) to RDD with 5 rows. And on…
ANTVirGEO
  • 124
  • 1
  • 16
0
votes
2 answers

How to get the index of a specific object in Dictionary in swift

I am searching a value of a key in Dictionary Array in this way. if let result = self.lasrIDArray.flatMap({$0["\(self.selectedTitle)"]}).first { print("-------RESULT------\(result)") //->title of the other } But wat I want to do is…
Irrd
  • 325
  • 1
  • 6
  • 18
0
votes
1 answer

Spark flatMap/reduce: How to scale and avoid OutOfMemory?

I am migrating some map-reduce code into Spark, and having problems when constructing an Iterable to return in the function. In MR code, I had a reduce function that grouped by key, and then (using multipleOutputs) would iterate the values and use…
xmar
  • 1,729
  • 20
  • 48
0
votes
1 answer

Scala/Spark's flatmap with repeated function calls

Say I have following code. def f(x): (Array[Double], Array[Int])={ val data: Array[Double] //1D array val index: Array[Int] //Data element's index //Read data from a file into "data" //Generate index (based on value "x")…
Bin Dong
  • 21
  • 2
0
votes
1 answer

XSLT, `flatmap` like operation

I'm currently learning XSLT but it's still too hazy for me to do anything useful. Could you please give me a hint (or better, the stylesheet) to convert all elements like this
sqxmn
  • 3
  • 3
0
votes
2 answers

Is boolean not supported with flatMap?

I'm facing this error with flatMap but not with map. Is boolean operation is not supported with flatMap transformation? scala> val array = Array("age","astro") array: Array[String] = Array(age, astro) scala> val baseRdd =…
Balaji Reddy
  • 5,576
  • 3
  • 36
  • 47
0
votes
4 answers

FlatMap behavior in scala

I am trying to get the hang of the flatMap implementation in Scala. Based on the definition in Scala programming Function returning a list of elements as its right argument. It applies the function to each list and returns the concatenation of all…
Gurupraveen
  • 181
  • 1
  • 13
0
votes
2 answers

Rxjava : Apply treatment on members of an object

I hava a function which is returning an object containing a list from a html page : @Override public Observable getMediaListFromUrl(String url) { return getHtml(url) .map(new Func1() { …
Nicolas
  • 598
  • 3
  • 15
0
votes
2 answers

how to single the list of list in java

I have a code here that getting the List of List in java. But my problem now is the result in my server was repeatable... Example [2012-01-04, 3 , 2012-02-04, 4, 2012-05-04, 3][2012-01-04, 3 , 2012-02-04, 4, 2012-05-04, 3][2012-01-04, 3 ,…
0
votes
1 answer

Scala Flatmap on Array inside the part of the value

I have RDD like (a,b,Array(p1,p2,p3),c), I want to apply flatmap on Array inside the value to get the below output like. (a,b,p1,c) (a,b,p2,c) (a,b,p3,c)
prakash
  • 111
  • 3
  • 9
0
votes
1 answer

Why does flatMap and fromEvent work only when returning it during first subscription as per example?

As shown below, why does example 1 fail when we return the div and then separately return a new click observable flatMap to click$ ? Example 2 works fine. JSBin below to try it out Anyone could explain why this happens? From what I understand,…
FeliciousX
  • 38
  • 3
0
votes
1 answer

How does a flatMap on a double-generic data-structure looks like?

I have the following (simple) data structure: struct Work { let work: Input -> Output } This type represents work which can take an Input and turns in into a desired Output. I am trying to see whether this data structure conforms…
Robin van Dijke
  • 752
  • 4
  • 13
0
votes
1 answer

Refactor Nested FlatMap in RxJS

I have a itemsObservable, filtersObservable, and a queryObservable and feed their values to a function that takes a list of items, filter conditions, and a search query, and returns a filtered list. Right now my code looks something…
0
votes
1 answer

Can I have instructions in each flatmap case? If not, how to increase counter?

I have a flatmap that's going through a Sequence, and I'm using case in order to identify different elements in the Sequence, and replace the elements with something else. However, in some of those cases, I also want to increase a counter variable.…
GeleiaDeMocoto
  • 151
  • 1
  • 10
0
votes
2 answers

How to assign value into a breeze Matrix in flatMap Scala-Spark?

i want to initialize a matrix using data in flatMap , this is my data: -4,0,1.0 ### horrible . not-work install dozen scanner umax ofcourse . tech-support everytime call . fresh install work error . crummy product crummy tech-support crummy…
hadiye
  • 3
  • 2