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
5
votes
2 answers

Scala Function.tupled vs f.tupled

I have the following Scala code: def f(x: Int, y: Int): Option[String] = x*y match { case 0 => None case n => Some(n.toString) } val data = List((0, 1), (1, 0), (2, 3), (4, -1)) data flatMap {case (x, y) => f(x, y)} But, the last line is…
pathikrit
  • 32,469
  • 37
  • 142
  • 221
5
votes
1 answer

Java Stream collect after flatMap returns List instead of List
I tried the following code using Java 8 streams: Arrays.asList("A", "B").stream() .flatMap(s -> Arrays.asList("X", "Y").stream().map(s1 -> s + s1)).collect(Collectors.toList()); What I get is a List while I would expect a…
Filippo Tabusso
  • 601
  • 3
  • 10
  • 16
5
votes
2 answers

What is the difference between mapcat in Clojure and concatmap in Haskell?

In Clojure you have a function called mapcat in Clojure, which bears some similarity to flatmap in Scala. It is used to map a function to a list and return a list. In Haskell we have a function ConcatMap which in name seems quite similar. My…
hawkeye
  • 34,745
  • 30
  • 150
  • 304
4
votes
2 answers

Why does using flatMap with an async function not return a flatten array?

I want to understand why using .flatMap() with async does not return a flatten array. For example, for Typescript, this returns an array of numbers: I know Promisse.all and async are useless with a simple array of number, it is just easier for…
maximeSurmontSO
  • 189
  • 3
  • 16
4
votes
5 answers

Flatten nested JavaScript object

I have a nested object and I want to flatten/map it into a single-layered, table-like object. [{ a: 1, b: 2, c: [{ x: 10, y: 20 }, { x: 30, y: 40 }] }, { a: 3, b: 4, c: [{ x:…
André Reichelt
  • 1,484
  • 18
  • 52
4
votes
1 answer

FlatMap gives warning with Int type array but not with String type array

When I use flatMap with String type array it didn't give any warning, while it gives warning in case of Int type array. Why? Example: let strings = [ "I'm excited about #SwiftUI", "#Combine looks cool too", "This year's #WWDC was…
4
votes
1 answer

What is the difference between merge and flatmap operator in RxSwift

I am currently new to RxSwift and I am a little bit confused about the flatMap and merge operator. I know the merge operator merges more than one observable into a single observable. I feel that flatMap is doing the same thing.Am I wrong? If so what…
Ahmed
  • 1,229
  • 2
  • 20
  • 45
4
votes
2 answers

Java - Turn Object with List Variable into a List of Objects

My basic class is: public class Student { public String name; public String className; // In real code I'd have a second object for return to the end user public List classes; // Can be zero } I'd like to flatten it out so I can…
canpan14
  • 1,181
  • 1
  • 14
  • 36
4
votes
3 answers

How to convert Stream to Stream?

I am trying to flatten my String[] stream to a String Stream E.g. { "A", "B", "C" }, {"D, "E" } to "A", "B", "C", "D", "E" This is the code I have so far: Files.lines(Paths.get(file)).map(a -> a.split(" ")); Files.lines(path) returns…
noobcoder24
  • 135
  • 1
  • 13
4
votes
1 answer

FlatMap and Map in Apache Beam

Does FlatMap and Map function in Apache Beam for python is running on parallel? (p | 'GetJava' >> beam.io.ReadFromText(input) | 'GetImports' >> beam.FlatMap(lambda line: startsWith(line, keyword)) | 'PackageUse' >>…
mileven
  • 204
  • 3
  • 13
4
votes
1 answer

Attach column names to elements with Spark and Scala using FlatMap

For a given table like +--+--+ | A| B| +--+--+ |aa|bb| |cc|dd| +--+--+ I want to get a dataframe like: +---+---+ |._1|._2| +---+---+ |aa | A | |bb | B | |cc | A | |dd | B | +---+---+ using Apache Spark and Scala. So basically I want tuples that…
4
votes
1 answer

Implementation of flatMap() for State transition

Exercise 6.8, Chiusano and Bjarnason, Functional Programming in Scala, p. 87 asks how one might implement flatMap() for the following trait: trait RNG { def nextInt: (Int, RNG) } type Rand[+A] = RNG => (A, RNG) The answer key gives the following…
Ben Weaver
  • 960
  • 1
  • 8
  • 18
4
votes
2 answers

Java 8 stream min is not returning expected value

I am looking to get minimum value from a list of maps in java. I am using combination of flatmap,stream and min to do so and not getting expected results. Here is the code public class TestMin { public static class TestHolder{ public …
maneet
  • 295
  • 2
  • 10
4
votes
4 answers

How to use the 'flatmap' operator conditionally ? (Angular 2/rxjs)

What I'm trying to achieve is to run a series of observables conditionally. return observable.map(response => response) .flatmap(response1 => observable1(response1)) .flatmap(response2 => observable2(response2)) …
Joel Raju
  • 1,210
  • 2
  • 18
  • 21
4
votes
4 answers

Swift Filter other arrays based on another Bool array

I have an array of Bools and want to edit an array of Scores and an array of Dates for the values that are False. I can't get to it. I thought of getting elements that are false and using that array to remove those elements from the score array but…
SashaZ
  • 393
  • 7
  • 20