flatmap

Replace nested loop with Java 8 flatmap

坚强是说给别人听的谎言 提交于 2019-12-01 04:48:09
问题 I'm trying to use flatmap to make a nested loop with the Stream API, but I can't seem to figure it out. As an example, I want to recreate the following loop: List<String> xs = Arrays.asList(new String[]{ "one","two", "three"}); List<String> ys = Arrays.asList(new String[]{"four", "five"}); System.out.println("*** Nested Loop ***"); for (String x : xs) for (String y : ys) System.out.println(x + " + " + y); I can do it like this, but this seems ugly: System.out.println("*** Nested Stream ***");

“Cannot convert return expression” in flatMap with a meaningless expression inside

≡放荡痞女 提交于 2019-12-01 02:40:48
问题 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 // Cannot convert return expression of type 'Int' to return type 'String?' } .forEach { print("SS") print($0) } Commenting out a bit array .flatMap { // print("DD") return $0 } .forEach { print("SS") print($0) } And everything works.. even more interesting example array

Swift: Flatten an array of dictionaries to one dictionary

◇◆丶佛笑我妖孽 提交于 2019-11-30 17:48:07
In Swift, I am trying to flatten an array of dictionaries into one dictionary i.e let arrayOfDictionaries = [["key1": "value1"], ["key2": "value2"], ["key3": "value3", "key4": "value4"]] //the end result will be: flattenedArray = ["key1": "value1", "key2": "value2", "key3": "value3", "key4": "value4"] I have tried using flatmap, but the type of the returned result is [(String, AnyObject)] and not [String, Object] ie let flattenedArray = arrayOfDictionaries.flatMap { $0 } // type is [(String, AnyObject)] So I have 2 questions: Why is type [(String, AnyObject)] returned? And what do the brackets

Why can't Stream.flatMap accept a collection?

随声附和 提交于 2019-11-29 10:17:53
Given the following as an example of data classes: class Country { List<Region> regions = new ArrayList<>(); List<Region> getRegions() { return regions; } } class Region { String getName() { return "some name"; } } Presuming I would have a List of Countries List<Country> countries = new ArrayList<>(); And I wanted to Stream those to their Regions and their corresponding names I would like to do the following: countries.stream().flatMap(Country::getRegions).map(Region::getName)... However that code does not compile since the return value of "getRegions" is a Collection (List) as opposed to a

Is there any difference between flatten and flatMap(identity)?

会有一股神秘感。 提交于 2019-11-29 04:32:32
scala> List(List(1), List(2), List(3), List(4)) res18: List[List[Int]] = List(List(1), List(2), List(3), List(4)) scala> res18.flatten res19: List[Int] = List(1, 2, 3, 4) scala> res18.flatMap(identity) res20: List[Int] = List(1, 2, 3, 4) Is there any difference between these two functions? When is it appropriate to use one over the other? Are there any tradeoffs? You can view flatMap(identity) as map(identity).flatten . (Of course it is not implemented that way, since it would take two iterations). map(identity) gives you the same collection, so in the end it is the same as only flatten . I

PySpark Throwing error Method __getnewargs__([]) does not exist

喜你入骨 提交于 2019-11-28 10:55:24
I have a set of files. The path to the files are saved in a file., say "all_files.txt". Using apache spark, I need to do an operation on all the files and club the results. The steps that I want to do are: Create an RDD by reading "all_files.txt" For each line in "all_files.txt" (Each line is a path to some file), read the contents of each of the files into a single RDD Then do an operation all contents This is the code I wrote for the same: def return_contents_from_file (file_name): return spark.read.text(file_name).rdd.map(lambda r: r[0]) def run_spark(): file_name = 'path_to_file' spark =

Why can't Stream.flatMap accept a collection?

♀尐吖头ヾ 提交于 2019-11-28 03:31:31
问题 Given the following as an example of data classes: class Country { List<Region> regions = new ArrayList<>(); List<Region> getRegions() { return regions; } } class Region { String getName() { return "some name"; } } Presuming I would have a List of Countries List<Country> countries = new ArrayList<>(); And I wanted to Stream those to their Regions and their corresponding names I would like to do the following: countries.stream().flatMap(Country::getRegions).map(Region::getName)... However that

What is the difference between concatMap and flatMap in RxJava

北城以北 提交于 2019-11-27 19:37:25
It seems that these 2 functions are pretty similar. They have same signature (accepting rx.functions.Func1<? super T, ? extends Observable<? extends R>> func ), and their marble diagrams look exactly same. Can't paste the pics here, but here's one for concatMap , and here's one for flatMap . There seems to be some subtle difference in the description of resulting Observable , where one produced by concatMap contains items that result from concatinating resulting Observables, and the one produced by flatMap contains items that result from first merging the resulting Observables, and emitting

Is flatMap guaranteed to be lazy? [duplicate]

若如初见. 提交于 2019-11-27 14:26:25
This question already has an answer here: Why filter() after flatMap() is “not completely” lazy in Java streams? 6 answers Consider the following code: urls.stream() .flatMap(url -> fetchDataFromInternet(url).stream()) .filter(...) .findFirst() .get(); Will fetchDataFromInternet be called for second url when the first one was enough? I tried with a smaller example and it looks like working as expected. i.e processes data one by one but can this behavior be relied on? If not, does calling .sequential() before .flatMap(...) help? Stream.of("one", "two", "three") .flatMap(num -> { System.out

Java 8 Streams FlatMap method example

 ̄綄美尐妖づ 提交于 2019-11-26 21:37:33
I have been checking the upcoming Java update , namely: Java 8 or JDK 8 . Yes, I am impatient, there's a lot of new stuff, but, there is something I don't understand, some simple code: final Stream<Integer>stream = Stream.of(1,2,3,4,5,6,7,8,9,10); stream.flatMap(); the javadocs are public <R> Stream<R> flatMap(Function<? super T,? extends Stream<? extends R>> mapper) Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element. Each mapped stream is closed after its