flatmap

Swift: Lazily encapsulating chains of map, filter, flatMap

瘦欲@ 提交于 2019-12-03 14:45:36
I have a list of animals: let animals = ["bear", "dog", "cat"] And some ways to transform that list: typealias Transform = (String) -> [String] let containsA: Transform = { $0.contains("a") ? [$0] : [] } let plural: Transform = { [$0 + "s"] } let double: Transform = { [$0, $0] } As a slight aside, these are analogous to filter (outputs 0 or 1 element), map (exactly 1 element) and flatmap (more than 1 element) respectively but defined in a uniform way so that they can be handled consistently. I want to create a lazy iterator which applies an array of these transforms to the list of animals:

How to use swift flatMap to filter out optionals from an array

一世执手 提交于 2019-12-03 10:55:12
问题 I'm a little confused around flatMap (added to Swift 1.2) Say I have an array of some optional type e.g. let possibles:[Int?] = [nil, 1, 2, 3, nil, nil, 4, 5] In Swift 1.1 I'd do a filter followed by a map like this: let filtermap = possibles.filter({ return $0 != nil }).map({ return $0! }) // filtermap = [1, 2, 3, 4, 5] I've been trying to do this using flatMap a couple ways: var flatmap1 = possibles.flatMap({ return $0 == nil ? [] : [$0!] }) and var flatmap2:[Int] = possibles.flatMap({ if

How to use swift flatMap to filter out optionals from an array

风流意气都作罢 提交于 2019-12-03 01:23:42
I'm a little confused around flatMap (added to Swift 1.2) Say I have an array of some optional type e.g. let possibles:[Int?] = [nil, 1, 2, 3, nil, nil, 4, 5] In Swift 1.1 I'd do a filter followed by a map like this: let filtermap = possibles.filter({ return $0 != nil }).map({ return $0! }) // filtermap = [1, 2, 3, 4, 5] I've been trying to do this using flatMap a couple ways: var flatmap1 = possibles.flatMap({ return $0 == nil ? [] : [$0!] }) and var flatmap2:[Int] = possibles.flatMap({ if let exercise = $0 { return [exercise] } return [] }) I prefer the last approach (because I don't have to

Struct Init with JSON and flatMap

佐手、 提交于 2019-12-03 00:50:07
问题 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 Actor. When I try to compile the code i get the error: Cannot assign value of type '()' to type [Actor]. The error corresponds to a line in viewDidLoad actorsList = downloadActors() Would anybody have any recommendation who to solve this? import

Swift flatMap gives unexpected result while using with optional array

拜拜、爱过 提交于 2019-12-03 00:13:44
We have an array of the Person objects and each object has another array of String, which is optional. We want the consolidated list of car names in our society. struct Person { let name: String let address: String let age: Int let income: Double let cars: [String]? } let personsArray = [Person(name:"Santosh", address: "Pune, India", age:34, income: 100000.0, cars:["i20","Swift VXI"]), Person(name: "John", address:"New York, US", age: 23, income: 150000.0, cars:["Crita", "Swift VXI"]), Person(name:"Amit", address:"Nagpure, India", age:17, income: 200000.0, cars:nil)] let flatmapArray =

How to perform multiple API call synchronously or call API sequentially using RxAndroid or RxJava

与世无争的帅哥 提交于 2019-12-02 19:21:11
问题 Hi There I am new in RxJava I am trying to call two API sequential the result of one api used as input to other and lastly return value of second app Currently I am trying with below code public Single<List<LocationParcelableItem>> getUpdateRecentLocation( LocationViewType locationViewType, Map<String, String[]> filter) { return locationRepository .getRecentLocations(filter) .subscribeOn(Schedulers.io()) .flatMapMaybe(this::updateRecentLocationList) .flatMapSingle(userLocationParcelableItem -

Struct Init with JSON and flatMap

纵饮孤独 提交于 2019-12-02 13:33:45
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 Actor. When I try to compile the code i get the error: Cannot assign value of type '()' to type [Actor]. The error corresponds to a line in viewDidLoad actorsList = downloadActors() Would anybody have any recommendation who to solve this? import UIKit func downloadActors() { var request = URLRequest(url: URL(string: "url...")!) request.httpMethod =

Java 8 Stream flatMap and group by code compiler error

孤街醉人 提交于 2019-12-01 06:46:18
问题 // given a set of Item objects, group them by the managers of creator and owners Map<String, List<Item>> managersItems = itemSet.parallelStream().flatMap(item -> { // get the list of the creator and owners List<String> users = new ArrayList(); users.add(item.getCreator()); users.addAll(item.getOwners()); return Stream.of(users.toArray(new String[] {})).map(user -> { LdapUserInfo ldapUser = LdapUserInfoFactory.create(user); String manager = ldapUser.getManager(); return new AbstractMap

Replace nested loop with Java 8 flatmap

会有一股神秘感。 提交于 2019-12-01 06:16:16
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 ***"); xs.stream().forEach(x -> ys.stream().forEach(y -> System.out.println(x + " + " + y)) ); Flatmap looks

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

旧时模样 提交于 2019-12-01 05:06:50
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 .flatMap { let z = $0 return $0 // Or return z - all is the same "Cannot convert return expression of type