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

flatMap strange behaviour on List of List of Integers

In Scala: scala> val xs = List(List(1, 3, 5), List(3, 4, 30)) xs: List[List[Int]] = List(List(1, 3, 5), List(3, 4, 30)) scala> xs flatMap {x => x + 1} :9: error: type mismatch; found : Int(1) required: String xs flatMap…
Muhammad Hewedy
  • 29,102
  • 44
  • 127
  • 219
0
votes
2 answers

Putting two placeholders inside flatMap in Spark Scala to create Array

I am applying flatMap on a scala array and create another array from it: val x = sc.parallelize(Array(1,2,3,4,5,6,7)) val y = x.flatMap(n =>…
hmi2015
  • 831
  • 3
  • 10
  • 22
0
votes
2 answers

scala flatmap to get correct data structure

I have parse the data and generated following RDD: x [RDD] = (458817,(CompactBuffer(20),CompactBuffer((837063182,0,1433142639864), (676690466,0,1433175090184), (4642913327036075112,1,1433177284025), (464291332,1,1433182403135),…
add-semi-colons
  • 18,094
  • 55
  • 145
  • 232
0
votes
3 answers

Is there an implicit in this call to flatMap?

In this code : import java.io.File def recursiveListFiles(f: File): Array[File] = { val these = f.listFiles these ++ these.filter(_.isDirectory).flatMap(recursiveListFiles) } taken from : How do I list all files in a subdirectory in scala? Why…
blue-sky
  • 51,962
  • 152
  • 427
  • 752
0
votes
2 answers

How to use if to filter in a map function in scala?

I've got a hadoopFiles object which is generated from sc.newAPIHadoopFile. scala> hadoopFiles res1: org.apache.spark.rdd.RDD[(org.apache.hadoop.io.LongWritable, org.apache.hadoop.io.Text)] = UnionRDD[64] at union at :24 I intend to iterate…
Judking
  • 6,111
  • 11
  • 55
  • 84
0
votes
0 answers

Play Framework : how to use flatMap dynamically?

I have CSV string like HK1, Hk2, Hk3......, SG1, SG2 & so on. depending on size of list I need to call REST call to back end system but problem is that, back end system wants multiple calls if String is too long, like if String have 8 values then…
GLADIATOR
  • 119
  • 1
  • 12
0
votes
1 answer

Is flatmap/mapcat a function that can be used as the basis of other higher order functions?

We can see that we can use reduce/foldl1 as the function by which we can define other higher order functions such as map, filter and reverse. (defn mapl [f coll] (reduce (fn [r x] (conj r (f x))) [] coll)) (defn filterl [pred coll] …
hawkeye
  • 34,745
  • 30
  • 150
  • 304
0
votes
1 answer

How to implement flatMap for Option

I'm trying to implement map and flatMap as an extension/enrichment for Option, without cheating and looking at how it was implemented in Scalaz. So here's what I got so far before I got stuck: package extensions.monad trait Monad[M[_]] { // >>=…
Electric Coffee
  • 11,733
  • 9
  • 70
  • 131
0
votes
1 answer

How to memorize value to use it after flatMap?

Bacon.fromArray(list) .flatMap(function(user){ return Bacon.fromCallback(user, 'getClients'); }) .onValue(function(clients){ // need `user` object some how }) ; Need user object in onValue callback
redexp
  • 4,765
  • 7
  • 25
  • 37
0
votes
2 answers

Scala, prepend a list of variables to the beginning of each list in a list

I am new to scala and want to get the following thing done using map, flatMap, and/or for comprehension. I have a list of lists l = List[List[T]]. For example, l = [[1,2,3],[2,4,6,4],[3,4,6,2,3]]. Note that each list inside l can have varying…
zyl1024
  • 690
  • 1
  • 9
  • 23
0
votes
1 answer

Cannot iterate a flat map

I am working with a boost flat_map and trying to iterate through it, however, I cannot figure out how to create the iterator. my_map = mySeg.find("temp_map").first; //fetch a pointer to the map tlsShmemAllocator alloc_inst…
user3208010
  • 45
  • 1
  • 8
0
votes
2 answers

Performance difference in flatMap compared to for-comprehension

I am still new to Scala, and one of the things I read about is that for-comprehension is equivalent to a flatMap to a certain extent. However, in my code (below), flatMap is taking at least as twice as long to compute. What could be the reason for…
jbx
  • 21,365
  • 18
  • 90
  • 144
-1
votes
1 answer

Java Stream Optional: How can I convert the multiple inner Optional present condition to Optional.flatMap()

I am trying to convert the multiple Optional isPresent to Optional.flatMap but not getting on how to do that. private Optional getCommit1 (String id, Integer number) { **Optional revision = repository.findById(id,number); …
-1
votes
2 answers

Is the behavior of flatMap correct?. Where can I report a bug in Javascript?

I was writing about version news and I came across something that I think is a bug in javascript. The code: const arrValor = [1,2,3,4,5,,6,[7,8,9,[10,11,[111,222]],12],13,14]; //console.log("Original :",arrValor); console.log("mas s …
migarcia
  • 1
  • 4
-1
votes
3 answers

Flux .then() running before complete signal

I tried to do something with Flux streaming objects and after handling all elements do some last work and finish a Mono but it doesn't work: // data and id comming from a webrequest // myRepository is a…