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

Is std::iter::FlatMap.clone() possible?

I am trying to create all possible pairs of items in a FlatMap: possible_children.clone().flat_map(|a| possible_children.clone().map(|b| (a,b))) In order to do this, I am trying to clone a FlatMap and I see in the documentation that the FlatMap…
7
votes
1 answer

Swift: flatMap to Dictionary

I am trying to parse the following JSON: { "String For Key 1": [ "Some String A", "Some String B", "Some String C", ], "String For Key 2": [ "Some String D", "Some String E", "Some String F" ], "String For Key 3":…
user1107173
  • 10,334
  • 16
  • 72
  • 117
6
votes
2 answers

How can I convert this rxjava/rxkotlin flatMap into lambda expression?

Observable.just(1) .flatMap(object : Function> { override fun apply(integer: Int): Observable { return Observable.just(integer * 10) } }) …
s-hunter
  • 24,172
  • 16
  • 88
  • 130
6
votes
2 answers

"Cannot convert return expression" in flatMap with a meaningless expression inside

I was examining .lazy for high order functions and have got some interesting compile errors related to flatMap function (and possibly others) Examples let array = [1, 2, 3, 4, 5, 6] array .flatMap { print("DD") return $0 //…
Pavel Stepanov
  • 891
  • 8
  • 13
6
votes
1 answer

Why does "flatMap" work with sequences of Option type in Scala?

I can't figure out how the Scala compiler figures out how to use flatMap with a sequence of Options. If I use a flatMap on a sequence of sequences: println(Seq(Seq(1), Seq()).flatMap(a => a)) // List(1) it will concatenate all nested sequences The…
Ivan Mushketyk
  • 8,107
  • 7
  • 50
  • 67
6
votes
1 answer

flatMap() to convert a stream of LinkedList into a stream of String

My goal is exactly what the title say. What I'm doing is: .stream().flatMap(x -> x.getTitles()) getTitles() returns a LinkedList, and I expected flatMap() to do the job and create a stream of Strings instead of a stream of…
RVKS
  • 147
  • 3
  • 16
6
votes
4 answers

Map and flatMap difference in optional unwrapping in Swift 1.2

Both map and flatMap are defind on ImplicitlyUnwrappedOptional, but they differ (obviously) in their definition according to the documentation: func map(f: @noescape (T) -> U) -> U! If self == nil, returns nil. Otherwise, returns f(self!). func…
Dániel Nagy
  • 11,815
  • 9
  • 50
  • 58
6
votes
2 answers

Java 8 flatMap + Optional.of doesn't compile

I'm trying to flatMap Optionals in Java. Here is a simplified example: List x = Arrays.asList("a", "b", "c"); List result = x.stream().flatMap((val) -> val.equals("b") ? Optional.empty() :…
auramo
  • 13,167
  • 13
  • 66
  • 88
6
votes
2 answers

Scala async/callback code rewriting

Simple code that should check user by pass, user is active and after that update last login datetime. def authenticate() = Action.async { implicit request => loginForm.bindFromRequest.fold( errors =>…
sh1ng
  • 2,808
  • 4
  • 24
  • 38
6
votes
2 answers

what is the map/flatmap function used in for comprehensions?

I want to see the f function that is getting passed in map/flatmap, but no luck. I threw an an exception to see any sign of f, did not work. What is that function? how is it generated behind the scenes? Exception in thread "main"…
mert inan
  • 1,537
  • 2
  • 15
  • 26
5
votes
0 answers

skipUntil and flatMap with concurrency 1 hangs in Reactor

I have experienced a strange behavior in Reactor (v. 3.4.2) when using skipUntil and flatMap(fn, concurrency) operators with concurrency=1. The pipeline hangs. To demonstrate what I mean here is a short example that works as I would…
5
votes
5 answers

Is there any way to convert a 2D List to 1D List using only `map` not using `flatMap`?

I'm a Java beginner, I just got learn about map and flatMap. When 2d List should be converted to 1d List, it was implemented like below. List> list_2d = List.of(List.of(1, 2), List.of(3, 4)); List lst1 = list_2d …
frhyme
  • 966
  • 1
  • 15
  • 24
5
votes
5 answers

Return Optional as it is if the returned Optional has value, else call another function

I have two methods func1 and func2 which return Optional. Return Optional as it is if the returned Optional from func1 has value, else call func2 and return its Optional. One way is to use if-else. Optional opt1 = func1(); if…
adimoh
  • 658
  • 2
  • 8
  • 20
5
votes
1 answer

Using flatMap in RxJava 2.x

I'm working with an API of my own and a i'm hoping to chain a few paginated results using RxJava. I use cursor based pagination. (imagine there are 50 users in this first request): { "data":{ "status":"ok", "total":988, //users…
sandes
  • 1,917
  • 17
  • 28
5
votes
1 answer

Efficiency of flatMap vs map followed by reduce in Spark

I have a text file sherlock.txt containing multiple lines of text. I load it in spark-shell using: val textFile = sc.textFile("sherlock.txt") My purpose is to count the number of words in the file. I came across two alternative ways to do the…
1 2
3
36 37