Scala introduction and installation

Scala introduction and installation

    This article will introduce the following Scala and Scala installations and the use of IDE (eclipse). Although this basic environment has no technical content, the basic environment cannot be done well. It will be nonsense to discuss more advanced things in the future. All Scala usage is in the window environment.

1. Introduction

1. Official description

    Official website: http://www.scala-lang.org

    The official website's introduction to scala:

    Scala is both an object-oriented language and a function-oriented language. Scala can provide elegant hierarchies for you to do a lot of code reuse and extension, and can achieve this through higher-order functions. (Higher-order functions are one of the features in functional programming, allowing functions to be passed as arguments and returned as return values).

2. About the author

    Scala founder Martin Odersky Martin Odersky.

    Martin is a professor in the Programming Research Group at EPFL (Leading Swiss Technical University, Ecole Polytechnique Fédérale de Lausanne). He has pursued one goal throughout his career: to make the basic task of writing programs efficient, easy, and enjoyable.

    He has probably written more Java and Scala code than anyone else in the world. He wrote javac, the compiler most Java programmers use today. He also wrote the Scala compiler, scalac, which is the cornerstone of the rapidly growing Scala community. He is the author of "Programming in Scala", the best-selling Scala book. He has worked at IBM Research, Yale University, Karlsruhe University and University of South Australia. Before that, he studied with Pascal founder Niklaus Wirth at ETH Zurich, Switzerland, where he received his PhD in 1989.

    For the language scala, some people think of it as an extension of Java, some people think of it as C++ on the JVM, some people think it's a simple mixture of object-oriented language and function-oriented language, some people think it's Haskell, and it's not as good as Haskell powerful. (pure functional programming language)

    Martin Odersky below Martin Odersky (the inventor of Scala and a professor at EPFL) sent out this famous joke photo at the Scala Day San Francisco conference in the summer of 2016:

    The translation on this image is: "Scala's only function is to lead people to Haskell". Martin Odeskey took this as a joke, said he should rename Scala to Hascalator, and hired someone to design a logo.

3. Features of Scala language

    Scala is not suitable for beginner courses in programming. Rather, it is a powerful language tailored for professional programmers.

    1) It is a modern programming language by Martin Odersky (father of javac), influenced by languages ​​like Java, Ruby, Smalltalk, ML, Haskell, Erlang, etc.

    2) It is an object-oriented (OOP) language, each variable is an object, and each "operator" is a method. It is also a functional programming (FP) language, and functions can be passed as arguments. You can write code in OOP, FP, or a combination of both.

    3) The Scala code is compiled into a .class file through scalac, and then runs on the JVM, which can seamlessly use the existing rich Java class library. That is, the Scala code will be compiled into bytecode and run on the Java Virtual Machine (JVM).

    4) You can write interesting programs on the first day of exposure to the language, but the language is very deep, and as you learn more, you will find newer and better ways to write code. Scala will change the way you think about programming. For the same task, there can be many different implementations, and the readability and performance are different.

4, scala programming example

Array(1,2,3,4)

    How to iterate over the elements in an array, you should do this:

for(i<-Array(1,2,3,4))println(i)
Array(1,2,3,4).foreach{print(_)}

    If you try to convert these elements into a new collection, you should use for/yield expressions or map methods:

for(i<-Array(1,2,3))yield i*2
//> res4: Array[Int] = Array(2, 4, 6)
Array(1,2,3).map(_*2)
//> res5: Array[Int] = Array(2, 4, 6)

    For example, to filter out a collection whose elements are less than 4, you can use the filter method:

val nums=List(1,2,3,4).filter(_<4)
//> nums  : List[Int] = List(1, 2, 3)

5. Proverbs of the Author

    Compared with Java, Scala believes in the optimization ability of programmers. Martin Odersky said, "A lot of programmers will tell me that they generally refactor their Scala code two or three times, or even three or four times." This may sound very inefficient, but Scala is such a language, With each refactoring, the performance or readability of the code will be greatly improved.

    Scala doesn't treat programmers as fools. Martin Odesky's positioning of scalca is clear: "Scala is for smart people now, and it will be for smart people in the future."

    Scala provides a complete set of tools, allowing programmers to choose freely, whether it is a mutable data structure, an immutable data structure, or a parallel data structure. Then, among these choices, Scala performs special optimizations at the algorithm level for them. Scala believes in the ingenuity of programmers and allows programmers to choose the appropriate structure for the ever-changing task requirements, which is where Scala does an excellent job.

    Scala is not a pure functional programming language, so one of the differences between pure functional languages ​​is that scala provides variables and constants, while pure functional programming languages ​​do not have the concept of variables.

    Someone mentioned before that the code written by newbies and veterans of Scala will show two different styles, and even newcomers can't read the code written by experienced Scala programmers. Some people joked: "Great, In this way, the interns in our department can't mess with the code I wrote!" But in fact, not only are the styles different, but the gap in execution efficiency must be huge.

6. Application of Scala language

    kafka: Distributed message queue, the internal code is often used to deal with concurrency issues, and scala can greatly simplify its code.

    spark: It is convenient to handle multi-threaded scenarios. In addition, spark is mainly used for memory computing, and is often used to implement complex algorithms. Using a functional programming language such as scala can greatly simplify the code.

    Note: This article only lists some of them.

2. Operation configuration and use

1 、Windows scale

1. installation steps

    The premise of installing Scala must have JDK installed. The installation of JDK will not be described in detail. Version 1.7 and above are required here.

1> Double click to run the installation

    Go to the official website to download the installation package as shown below.

    Double-clicking to run is basically a fool-like operation, so I won't go into details here.

2> Add environment variables

    If after installation, you see that the scala environment variable already exists in the Path of the system environment variable, then no configuration is required. This was the case when I installed it.

    If after the installation is complete, there is no Scala environment variable in the environment variable Path, then you need to manually add the bin directory path of the scala installation directory to the system environment variable. This configuration is similar to the JDK configuration. As shown below:

2. use

    Scala is basically based on Java, and all the methods used are basically the same as Java. The difference is that it is different from some commands, as follows:

1> via cmd

    Through the cmd command window, enter scala to view the version information of Scala, which has the same function as Java's java -version. As shown below:

①Interactive mode

    You can directly enter scala commands through the command line, as follows to print characters to the console:

②Compile mode

    You can also write the ***.scala file first, and execute the command by executing the file, such as:

    First create a hello.scala file under the e drive, and write the following content in the file:

print("hello scala");

    Then execute it through cmd:

    You can also generate a .scala file first, then compile and generate a .class file, and then execute it.

    In the e drive, create the Person.scala file with the following contents:

object Person{
  def main(args: Array[String]): Unit = {
    println("hello scala hello world~")
  }
}

    Enter the cmd command window under the e-disk path, and then execute: scalac Person.scala

    After the execution, you will find that there are more corresponding class files under the e disk.

    Call execution: scala Person

    Seeing this, you will find that Scala and Java have something in common.

2. Use IDE to develop

    I see a lot of people on the Internet worrying about installing the Scala plugin in eclipse. Here I directly use eclipse with Scala. After decompression, configure a few parameters and use it. You can also download this version of eclipse for development.

1. Install

    Unzip it to use.

    As shown in the figure below: Open the preferences of the window option of eclipse. Generally, the places that need to be adjusted are these options in the red box.

    Workspace: It is mainly character encoding, which is adjusted to what you need. I choose UTF-8 here. Now, this character encoding is generally used for development.

    Java-Compiler: Check whether the jdk version is the same as the jdk version installed by yourself.

    Java-installed JREs: Check whether the jre version is the same as the one you installed, if not, modify it.

    Scala-Compiler: Check the version of the JVM, you can adjust it to the same version as your own jdk, or you can use the default 1.6.

    If you need this eclipse, you can leave a message below, or privately message me, I will put this version of jdk on the network, and paste the link here for everyone to download and use.

2. use

1> Create project

    Create a scala project, as shown below:

2> create class

After the project is created, create a scala object, as shown below:

3> Write the code

Write the code as shown below:

4> run

Run to get the result.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325295949&siteId=291194637