Tutorials | A Scala introductory tutorial for Python programmers __python

Source: Internet
Author: User
Guide to Statistics network This paper introduces Scala's language characteristics from seven aspects, such as installation, basic data type, operator, function and so on. Not just a Python programmer, anyone with programming experience or a desire to get started can look at it and make a basic understanding of Scala.

With the needs of business and data, we have introduced spark. Spark's support for Python is fine, but it has been developed using Scala, and the existing API does not have 100% coverage of Python. So with this article, Python programmers have access to the higher (level), faster (and more powerful) language of Scala (the Olympic spirit).

Scala's python-like development efficiency, but also Java-like performance, is a rare artifact. (Of course, fish and bears don't get the same, Scala's entry curve is as steep as python is to throw ) 1 installation

General Linux systems have a Python environment, but Scala is not. This requires us to install it manually, and we need to install the Java environment. Installation of the Java environment is not introduced here, a lot of online. Tell me about Scala's installation. The download address is in http://scala-lang.org/download/2.11.7.html.


1
2
3
4
5
6
7
8
9
Wget-c http://downloads.typesafe.com/scala/2.11.7/scala-2.11.7.tgz
Tar zxf scala-2.11.7
CD scala-2.11.7
./bin/scala
Welcome to Scala version 2.11.7 (Java HotSpot (TM) 64-bit Server VM, Java 1.8.0_60).
Type in expressions to have them evaluated.
Type:help for more information.

Scala>


We can see that the terminal has a scala> prompt, like Python repl, Scala has a repl. We can easily write some code here to test some ideas.

For Scala's Common IDE (integrated development environment), it is recommended to use idea for Scala plugins and Scala-ide.

Scala is powerful, in addition to its own better support for multi-core programming, functional features and some 3rd party libraries and frameworks based on Scala (e.g. Akka, Playframework, Spark, Kafka ...). , but also because it can be seamlessly combined with Java. All the libraries and frameworks developed for Java can be naturally integrated into the Scala environment. Of course, Scala can also facilitate the integration of Java environments, such as Spring. If you need 3rd party library support, you can use the compiler environment such as MAVEN, Gradle, SBT to introduce.

Scala is an object-oriented, functional programming language that inherits Java's object-oriented features while absorbing many functional features from Haskell and enhancements. 2Hello, world.

In general, Java System programs need to have a main method to execute. And it needs to be put into a web container container, or hit a jar package to execute. But Scala is different, in addition to supporting the traditional Java approach, it can also be like Python to save the code in a script file (. Scala) execution, like a shell script.


1
2
3
4
5
6
7
8

#!/bin/sh
exec Scala "$" "$@"
!#
Say hello to the argument
println ("Hello," + args (0) + "!")
yangjing-mac-air:scala-2.11.7 jingyang$./test.scala Yang Jing
Hello, Yang Jing!


(Note: You need to add $scala_home/bin to the system environment variable to directly execute SCALA commands)

As you can see, using Scala, you can launch it as a "service" in addition to a traditional Java program, as a "script" like Python.

(Note: Scala does not represent the hierarchical relationship of code in code, as Python does, but uses {} to represent the level of code as usual.) Give programmers more freedom) 3 variables, underlying data types

In Scala, a variable does not need to display the specified type, but it needs to be declared in advance. This avoids many namespace pollution problems. Scala has a powerful type automatic derivation function that can automatically derive the type of a variable based on the right value and context. You can declare and assign values directly in the following ways.


Related Article

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.