51st: The implementation code of the chain call style in Scala and its extensive application in spark programming

Source: Internet
Author: User

Today we learned the implementation of chained invocation styles in Scala, and in spark programming we often see the following code:

Sc.textfile ("hdfs://..."). FlatMap (_.split ("")). Map (_,1). Reducebykey (_ + _) ...

This style of programming is called chained invocation, and its implementation is described in the following code:

Class Animal {def Breathe:this.type = this}
Class Cat extends Animal {def eat:this.type = this}

Object test51{
def main (args:array[string]) {
Val cat = new Cat
Cat.breathe.eat
}
}

In the above code, we can see that our cat instance called the Breathe method and then called the Eat method.

If we make some changes to the animal class and cat classes, as shown here:

Class Animal {def breathe = this}
Class Cat extends Animal {def eat = this}

Object test51{
def main (args:array[string]) {
Val cat = new Cat
Cat.breathe.eat
}
}

This way, when we call Cat.breathe.eat, the compiler will error

It seems that the crux of the problem is the this.type of the method definition = this is here.

By applying this notation, we can make a chain call method and invoke the method after the class instantiation.

Share more of the Scala resources:

Baidu Cloud Disk: http://pan.baidu.com/s/1gd7133t

Micro Cloud Disk: http://share.weiyun.com/047efd6cc76d6c0cb21605cfaa88c416

360 Cloud Disk: Http://yunpan.cn/cQN9gvcKXe26M (extract code: 13CD)

Information from DT Big Data Dream Factory public account: Dt_spark

Follow the account for more information about Scala learning

51st: The implementation code of the chain call style in Scala and its extensive application in spark programming

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.