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
-1
votes
1 answer

Does Stream#findFirst() (or findAny) shortcircuit a flatmapped stream?

Java 8 Stream#findFirst() is a short-circuiting terminal operation. Meaning it will stop generating the stream once an element is found (usually used with a filter() operation). However, I want to know if it will short circuit on a stream created by…
wilmol
  • 1,429
  • 16
  • 22
-1
votes
2 answers

How to filter data using stream and flatMap?

I have a class public class Person { private int _id; private String _name; private int _age; private List _interests; public Person() { } public Person(int id, String name, int age,…
-1
votes
1 answer

flatMap operator causes compilation error

As shown below, I am creating Observables. When I try to apply the faltMap operator i receive compiltation error says: Cannot infer type argument(s) for flatMap(Function>) Type mismatch: cannot…
Amrmsmb
  • 1
  • 27
  • 104
  • 226
-1
votes
2 answers

What is an alternative to finding item with forEach from nested lists with Java 8?

I need some alternative for the following code to be more readable. ssrFlightList is nested list. I tried with forEach: ssrFlightList.stream().forEach( x -> x.getSsrPassengerList().stream().forEach( y ->…
Arif Acar
  • 1,461
  • 2
  • 19
  • 33
-1
votes
2 answers

Scala API response JSON parser from JSON object

I am new to Scala. I was trying to parse an API response in Scala. The API response is in the format: {"items":[{"name":"john", "time":"2017-05-11T13:51:34.037232", "topic":"india", "reviewer":{"id":"12345","name":"jack"}}, …
das
  • 669
  • 2
  • 12
  • 22
-1
votes
1 answer

How to use grouping, aggregate, transformation and filtering on stream

assume stream of txn, where each txn has following fields { date, amt, name, merchant } given txn stream group by yyyy-mm // fold 1 within group(yyyy-mm); Map to new object Expense(income, spent) if amount > 0 then…
bajju
  • 1
  • 2
-1
votes
1 answer

flatMap, Struct and JSON in Swift 3

I am having a problem with initializing Struct with JSON Data received from the URLRequest in Swift 3. The protocol JSONDecodable: protocol JSONDecodable { init?(JSON: [String : AnyObject]) } The struct is as follows and it…
Luke
  • 407
  • 2
  • 10
  • 22
-1
votes
1 answer

Java, Python - How to convert Java FlatMap into Python LinkedList

I am working on a formulation of transportation problem through Linear Programming. Primarily I searched it on the web and found a code which is written in Java. But, I have to write whole stuff in Python. And I am converting it into Python. I don't…
-1
votes
1 answer

Why does bind flatten the List?

I wonder, you say that flatMap is monad's bind method. The bind method takes a function that maps monad's contained item to another monad. This is how option.flatMap(item => another option (f(item))) gives me another monad with f(item) inside.…
Little Alien
  • 1
  • 8
  • 22
-1
votes
1 answer

how to skip empty rdd when join in spark

I want to get 2 rdd from Cassandra,then join them.And I want to skip the empty value. def extractPair(rdd: RDD[CassandraRow]) = { rdd.map((row: CassandraRow) => { val name = row.getName("name") if (name == "") None //join…
xidianw3
  • 1
  • 1
-2
votes
1 answer

What is "mapped stream" mentioned in the docs for flatMap?

This question is not about flatMap() in general. I want to understand the documentation of flatMap. Documentation says: Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream…
shiva
  • 2,535
  • 2
  • 18
  • 32
-2
votes
1 answer

flatMap without object in front in a function kotlin

fun Mono.update(): Mono { return flatMap { mySomething -> // some stuff }.map() } How does the above code works? What does Mono means before the name of the function/method? How can it use flatMap…
Harts
  • 4,023
  • 9
  • 54
  • 93
-2
votes
2 answers

Sum values in the same object

I have an object with many properties I want to sum all of 50 values in one properties called sumOfValues; And also, valueX can be null, this is why I use Double With java 8. public class MyObject { private String id; private String…
Mohammed
  • 89
  • 1
  • 3
  • 12
-2
votes
1 answer

Struct Init with JSON and flatMap

I'm having a problem with the following code. I'm downloading a list of actors in JSON and I want to populate Struct Actor with the received data. Everything works great until I try to flatMap on the received data and try to initialize the struct…
Luke
  • 407
  • 2
  • 10
  • 22
-2
votes
2 answers

flatten and flatMap in scala

I'll like to check if I have understood flatten and flatMap functions correctly. 1) Am I correct that flatten works only when a collection constitutes of other collections. Eg flatten would work on following lists //list of lists val l1 =…
Manu Chadha
  • 15,555
  • 19
  • 91
  • 184
1 2 3
36
37