)]FlatMapflatMapMuch like a map function, but it abandons the elements that are values nil .let anotherArray2 = testArray.flatMap { (string:String) -> Int? in let length = string.characters.count guard length > 0 else { return nil } return string.characters.count} print(anotherArray2) //[5, 8, 6]The other map is different from the function: If the element value is not nil, the flapMap function can convert the optional type ( optional ) to a non-optional type ( non-opt
Description
In Spark, the map function and the Flatmap function are two more commonly used functions. whichMap: operates on each element in the collection.FLATMAP: operates on each element in the collection and then flattens it.Understanding flattening can give a simple example
Val arr=sc.parallelize (Array ("A", 1), ("B", 2), ("C", 3))
Arr.flatmap (x=> (x._1+x._2)). foreach (println)
The output result is
A
1
B
2
C
3
If you use map
Val arr=sc.paral
), (3,30)) //> Res0: (List[int], list[int]) = ( List (1, 2, 3), List (ten,())2 optionfunctor.unzip (Some ())) //> Res1: ( Option[int], option[int]) = (Some (1), Some (2))What does this functor have to do with Monad? But what kind of abstract purpose and pattern might be associated with Monad? So again, the deduction: In the previous data type design we had to encounter a lot of MAP2 functions:1def Map2[a,b,c] (La:list[a], lb:list[b]) (f: (A, B) + C): list[c] = {2La
can only do it in subscriber. We did not use such a cool map () operator!!!Of course, I can use the map operator, map input is the list of URLs, processing time or for each traversal, just as the egg hurts.Fortunately, there is also the Observable.from () method, which receives a collection as input, and then outputs one element at a time to the Subscriber:Observable.from ("Url1", "Url2", "Url3") . Subscribe (URL, System.out.println (URL));Let's use this method to the scene just now:Query ("
["1", "2", "3", "4"];The following method is a closure of the form of an array of operations, an array of each element? 10 and then converted to a string; I hope you understand the closure of the package first;Flatmap functionWhen it comes to map functions, it's natural to talk about Flatmap,flatmap and map functions, but there are other differences.Map can do a
Rxjava features that we have not yet talked about.
2. ImprovedThe Savior is coming, he is flatmap ().Observable.flatmap () receives a observable output as input, while outputting another observable. Look directly at the code:
Query ("Hello, world!")
. Flatmap (new Func1
Here I post the entire function code to make it easy for you to understand what's going on, and using a lambda can greatl
1.Data structures with maps and FlatMap seem to be quite common.In fact there's a name describes this class of a data structuresTogether with some algebraic laws that they should has.They is called monads.Data structures with map and flatmap are very commonClasses with such data structures plus some algebraic rules are called monads.2. Flatmap and UnitA monad M i
.
The way to create "art"
The above is the result of a synchronous call we create Mono or Flux , but sometimes we need to create Mono or Flux from the result of a non-reactive asynchronous call. How does that work?
If this async method returns one CompletableFuture , then we can CompletableFuture create a Mono based on this:
Mono.fromFuture(aCompletableFuture);
If this asynchronous call does not return CompletableFuture and has its own callback method, how can it be created Mono ? We can use the
Listing subdirectories using Flatmap
You've seen how to list the files in the specified directory earlier. Let's take a look at how to traverse the direct subdirectory of the specified directory (depth 1), first implement a simple version, and then use the more convenient Flatmap () method to implement.
We first use the traditional for loop to traverse a specified directory. If there is a file in the subd
In the previous two articles, we introduced some basic concepts of rxjava and the simplest usage of rxjava. From this chapter, we start talking about the operators in Rxjava the operators in Operators,rxjava are divided into three main categories:
Conversion class operator (map flatMap concatMap flatMapIterable switchMap scan groupBy ...) ;
Filter Class Operators (fileter take takeLast takeUntil distinct distinctUntilChanged skip skipLast
In the filter observables of the rxjava-operator, it is understood that the basic use of the RxJava filter operator is mainly for the observable list. This article focuses on the use of object operators in the observable list. Used to transform observable sequences to create a sequence that better meets our needs.MapThe map operator, by specifying a Func object, converts observables to a new observable object and launches, and the observer receives the new observable processing.The flowchart of
Public void _onnext(String o) {L.E ("AAAA"); Tv.settext (o); }@Override Public void _OnError(String msg) { } });This completes the encapsulation of the Rxjava. Now, every time you access the network, the dialog box is displayed, and the cancel dialog is requested, and the request is canceled at the activity lifecycle stop.Transformer is actually a func1The difference between compose and
of the optional instance. Through a set of lambda expressions that implement the function interface, the incoming operation. If you are unfamiliar with the function interface, you can refer to my blog post. The Map Method example is as follows:// The map method executes the passed-in lambda expression parameter to modify the value of the optional instance. // creates a new optional instance for the return value of the lambda expression as the return value of the map method. optional value.toup
Swift Programming Idea Part 4:map all the things!2015-10-22 837 Article Directory
1. array vs. optional type
2. map () acting on an optional type
3. back to our example
4. Let's use map .
5. where is the problem?
6. FlatMap () to help.
7. use init as a closing packet
8. the ultimate Swift code
9. Take a look back at our OBJC code
Conclusion
Olivier Halligon, original link, original date: 2015-10-11Translator: ray1689
time and other rules, such as setting a three-second cache to be released once. See the following code. We have created two Observable, and use the buffer to convert it. The first one is cached by quantity, and the second one is cached by time.
private Observable
return Observable.just(1, 2, 3, 4, 5, 6, 7, 8, 9).buffer(2, 3);
}
private Observable
return Observable.interval(1, TimeUnit.SECONDS).buffer(3, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread());
}
Subscribe
=City ; - } the - PublicOptionalGetstreet () {Wuyi returnStreet; the } - Wu PublicOptionalgetcity () { - returnCity ; About } $ } - - Public classOptionalDemo1 { - A Public Static voidMain (string[] args) { +optionalNewAddress (Optional.ofnullable (NULL), Optional.of ("New York"))); theoptionalNewOffice ("OF1", Address1)); -optionalNewCompany ("Door never Closed", Office1)); $ the //What is the street address of Company1? the //In
will emit the top two digits every 3 digits, and the second observable will output a few digits every 3 seconds.
Second, FlatMap
Flatmap is a useful operator that can convert data to the rules you want and then launch it. The principle is to convert the observable into a number of data emitted from the original observable as the source data observable, and then the multiple observable launched data integ
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.