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

Extract data from object using a path - include all data in arrays

I am trying to solve the following problem with LoDash. I know how to solve the problem using for loops, but am looking for the modern functional method of solving the problem. I have the following data: const data = { people: [ { …
sixtyfootersdude
  • 25,859
  • 43
  • 145
  • 213
2
votes
4 answers

Flatten nested objects in Scala

Given a complex object like the following: case class Complex ( id: Long, name: String, nested: Seq[Complex] ) In action this can turn into something like this: val stuff = List( Complex(1, "name1", List( Complex(2,…
iTsaMe
  • 23
  • 5
2
votes
1 answer

pyspark flatmat error: TypeError: 'int' object is not iterable

This is the sample example code in my book: from pyspark import SparkConf, SparkContext conf = SparkConf().setMaster("spark://chetan-ThinkPad- E470:7077").setAppName("FlatMap") sc = SparkContext(conf=conf) numbersRDD = sc.parallelize([1, 2,…
2
votes
1 answer

Scala FlatMap provides wrong results

given a list of documents, I want to obtain the pairs that shares at least one token. To do this I wrote the code below, that do that through an inverted index. object TestFlatMap { case class Document(id : Int, tokens : List[String]) def…
Gaglia88
  • 99
  • 7
2
votes
2 answers

Flattening a nested Hashmap using Stream

I have a nested HashMap and I want to create a HashMap by flattening the Hashmap. I have tried the solution from Recursively Flatten values of nested maps in Java 8. But I am unable to use the class FlatMap as mentioned…
Vini
  • 1,978
  • 8
  • 40
  • 82
2
votes
0 answers

Calling multiple API in sequence using RxJava,Retrofit,RxKotlin flatmap

I need to call 3 different api ...each api takes input of another api output in sequence.. eg : API1 -> output -> will be input for API2 API2 -> output -> will be input for API3 In my case , Spinner contain API1 ....On spinner selection i need…
IID
  • 93
  • 2
  • 8
2
votes
1 answer

Java 8 using stream, flatMap and lambda

I have this piece of code and I want to return a list of postCodes: List postcodes = new ArrayList<>(); List entries = x.getEntry(); //getEntry() returns a list of Entry class for (Entry entry : entries) { if (entry != null) { …
invincibles04
  • 109
  • 3
  • 13
2
votes
2 answers

Scala why flatMap treats (x => x) different than (identity)

First, map treats x => x identical than identity List(1,2,3).map(x => x) //res0: List[Int] = List(1, 2, 3) List(1,2,3).map(identity) //res1: List[Int] = List(1, 2, 3) Now let's transform a List[Option[Int]] into List[Int] discarding all None. We…
Polymerase
  • 6,311
  • 11
  • 47
  • 65
2
votes
4 answers

Scala - access collection members within map or flatMap

Suppose that I use a sequence of various maps and/or flatMaps to generate a sequence of collections. Is it possible to access information about the "current" collection from within any of those methods? For example, without knowing anything specific…
mike w
  • 23
  • 3
2
votes
2 answers

Flatmap scala [String, String,List[String]]

I have this prbolem, I have an RDD[(String,String, List[String]), and I would like to "flatmap" it to obtain a RDD[(String,String, String)]: e.g: val x :RDD[(String,String, List[String]) = RDD[(a,b, list[ "ra", "re", "ri"])] I would like…
Will
  • 145
  • 2
  • 8
2
votes
2 answers

How to Flatmap collection inside Pair in Kotlin

In this example I have to classes. Order(selections: List, discount: Double, ...) Selection(productId: Long, price: Double, ...) I then keep collection of Order that I want to calculate prices afterward which need to use Selection's…
RobGThai
  • 5,937
  • 8
  • 41
  • 59
2
votes
1 answer

Flatmap on dataframe

What is the best way to preform a flatMap on a DataFrame in spark? From searching around and doing some testing, I have come up with two different approaches. Both of these have some drawbacks so I'm thinking that there should be some better/easier…
Shaido
  • 27,497
  • 23
  • 70
  • 73
2
votes
1 answer

How to call Spark Functions in Sparklyr with invoke

I am trying to use invoke in RStudio's Sparklyr to do a simple word count off of a text file in HDFS and have not figured out the syntax. I can get the whole file back as a list by using (similar to the count example in the SparklyR doc on…
Dan F
  • 21
  • 1
2
votes
2 answers

RXJS - flatMap redundant usage?

I've read an article about RXJS where it also explains about flatMap. I already know what's the difference and what it actually does , it flatten observable of observables to a single observable sequence. ( something like SelectMany in C# ) Anyway…
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
2
votes
4 answers

Usage of flatMap() in Java8

I have an Artist class as follows: class Artist { private final String name; private final String origin; private Stream members; public Artist(String name, String origin) { this.name = name; this.origin =…
Soumya Kanti Naskar
  • 1,021
  • 3
  • 16
  • 29