scala初学_Scala初学者系列1基础

scala初学

This series is all about the taste of Scala. It is best suitable for all the newbies in Scala. So here we go…

这个系列是关于Scala的味道的。 它最适合Scala中的所有新手。 所以我们开始...

This article will cover the fundamentals of Scala language.

本文将介绍Scala语言的基础知识。

价值观 (Values)

In Scala, we work with values:

在Scala中,我们使用值:

Values are used to define constants. val modifier means constant or immutable that means we cannot change its value once it is created. Thus reassignment to val is prohibited.

值用于定义常量。 val修饰符表示常量或不可变,这意味着一旦创建它便无法更改其值。 因此,禁止将其重新分配给val

Image for post

It is evaluated at time of definition only. Once evaluated, it reuses same value for all references of it.

仅在定义时进行评估。 评估后,它将为所有引用重复使用相同的值。

变数 (Variables)

Scala also allows us to define mutable values. Variables are used to define mutable reference to a value. var modifier means changeable or mutable that means we can change its value throughout the program lifetime . Thus reassignment to var is allowed.

Scala还允许我们定义可变值。 变量用于定义对值的可变引用。 var修饰符表示可变或可变,这意味着我们可以在程序生命周期中更改其值。 因此允许重新分配给var

Image for post

We do have the notion of a variable in Scala, but it’s heavily discouraged. In general, we work with immutable data structures: any intended modification to an existing instance should return a new (modified) instance.

我们在Scala中确实有变量的概念,但是强烈建议不要这样做。 通常,我们使用不可变的数据结构:对现有实例的任何预期修改都应返回一个新的(修改后的)实例。

种类 (Types)

In Scala, we don’t always need to specify the data type of our value, because the compiler is smart enough to infer that for us. We can also write:

在Scala中,我们并不总是需要指定值的数据类型,因为编译器足够聪明,可以为我们推断出来。 我们也可以这样写:

Image for post

We can see, the compiler automatically inferred the data type of the value.

可以看到,编译器自动推断出数据类型的值。

弦乐 (Strings)

Strings in Scala are similar to what we see in other languages, but with some special functionalities:

Scala中的字符串类似于我们在其他语言中看到的字符串,但是具有一些特殊功能:

Strings are defined as:

字符串定义为:

Image for post

Whenever compiler encounters a string literal in the code, it creates a String object of java.lang.String class with its value.

每当编译器在代码中遇到字符串文字时,它都会使用其值创建java.lang.String类的String对象。

Methods used to obtain information about an object are known as accessor methods.

用于获取有关对象的信息的方法称为访问器方法。

One accessor method that can be used with strings is the length() method, which returns the number of characters contained in the string object.

可以与字符串一起使用的一种访问器方法是length()方法,该方法返回字符串对象中包含的字符数。

Image for post

The String class includes a method concat() for concatenating two strings. But strings are more commonly concatenated with the + operator.

String类包括用于连接两个字符串的方法concat() 。 但是字符串通常用+运算符连接在一起。

Image for post

String interpolation can also be done using s string interpolator. It allows the direct usage of variable in processing a string, by injecting a value with the $ sign.

字符串内插也可以使用s字符串内插器完成。 通过在变量中插入$符号,可以直接使用变量处理字符串。

Image for post

表达方式 (Expressions)

In Scala, we work with values and we compose them to obtain other values. The composition structures are expressions, and they’re exactly what we expect.

在Scala中,我们使用值,然后将它们组合以获得其他值。 合成结构是表达式它们正是我们所期望的。

Previously we have defined values assigned to literals. However, it is more accurate to say that they are assigned to the return value of expressions.

以前我们已经定义了分配给文字的值。 但是,更准确地说是将它们分配给表达式的返回值。

Thus expression is a single unit of code that that can be reduced to a value. It can be a literal, a calculation, or a function call. An expression has its own scope, and may contain values local to the expression block.

因此,表达式是可以减少为一个值的单个代码单元。 它可以是文字,计算或函数调用。 表达式具有自己的范围,并且可以包含表达式块本地的值。

We can define values based on expressions as:

我们可以根据以下表达式定义值:

Image for post

In Scala, we can also define expression blocks. Multiple expressions can be combined using curly braces to create a single expression block. The last expression in the block is the return value for the entire block.

在Scala中,我们还可以定义表达式块。 可以使用花括号将多个表达式组合在一起,以创建单个表达式块。 块中的最后一个表达式是整个块的返回值。

Image for post

If-structures are expressions as well.

if结构也是表达式。

Image for post

In C-like languages, we have equivalent ternary operator, but in Scala, it’s much more readable because we can chain if-expressions in endless if/else structures without the risk of misunderstanding any logic.

在类似C的语言中,我们具有等效的三元运算符,但是在Scala中,它更具可读性,因为我们可以将if表达式链接到无数if / else结构中,而不会产生误解任何逻辑的风险。

We have other type of expressions as well, like for-expressions, match-expressions, etc. We’ll talk about them later.

我们还有其他类型的表达式,例如for-expressions,match-expressions等。我们稍后再讨论。

功能 (Functions)

Scala has both functions and methods and we use the terms method and function interchangeably with a minor difference. A Scala method is a part of a class which has a name and a signature, where as a function in Scala is a complete object which can be assigned to a variable.

Scala同时具有功能和方法,我们可以互换使用术语“方法”和“功能”,两者之间的差别很小。 Scala方法是具有名称和签名的类的一部分,其中,作为Scala中的函数,是可以分配给变量的完整对象。

Functions are declared and defined as:

函数声明并定义为:

Image for post

So we have:

因此,我们有:

  • def

    def

  • function name

    功能名称
  • arguments in the form of arg : Type

    arg : Type形式的arg : Type

  • : ReturnType

    : ReturnType

  • =

    =

  • then a single expression the function will return.

    然后函数将返回一个表达式。

Recursion plays a big role in pure functional programming and Scala supports recursive functions very well. In Scala, we don’t think in terms of loops. We think in terms of recursion.

递归在纯函数式编程中起着重要作用,而Scala很好地支持递归函数。 在Scala中,我们不考虑循环。 我们考虑递归。

Recursion means a function can call itself repeatedly.

递归意味着一个函数可以重复调用自己。

A simple example of recursion is demonstrated as:

递归的一个简单示例如下所示:

Image for post

It is mandatory to mention the return type of recursive functions.

必须提及递归函数的返回类型。

单位类型 (The Unit Type)

Finally, I’ll wrap this article with the type of expressions that don’t return any meaningful value, i.e. Unit. It is the equivalent of void functions in other languages. It is used when nothing needs to be returned by the function.

最后,我将用不返回任何有意义值的表达式类型(即Unit包装本文。 它等效于其他语言中的void函数。 当函数不需要返回任何内容时使用它。

Image for post

An example is the println() function, which returns Unit. It is a type containing a single value denoted ().

一个示例是println()函数,该函数返回Unit 。 它是包含表示为()的单个值的类型。

Expressions returning Unit are called side effects, because they have nothing to do with computing meaningful values. In pure functional programming, we tend to keep side effects to a minimum.

返回Unit表达式称为副作用,因为它们与计算有意义的值无关。 在纯函数式编程中,我们倾向于将副作用降至最低。

敬请关注… (Stay Tuned…)

Stay tuned for our next part of Scala Beginner Series where we will cover the object oriented nature of Scala. You don’t want to miss it!

请继续关注我们的Scala入门系列下一部分,我们将介绍Scala的面向对象性质。 你不想错过它!

翻译自: https://levelup.gitconnected.com/scala-beginner-series-1-basics-d1dae1f3458d

scala初学

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值