官术网_书友最值得收藏!

Introduction to Scala

Consider a scenario where you get a paragraph and a word and you are asked to get the number of occurrences for that word. You're lucky enough to know a language such as Java. Your solution might look like this:

String str = "Scala is a multi-paradigm language. Scala is scalable too."
int count = 0;
for (stringy: str.split (" ")) {
if (word.equals (stringy))
count++;
}
System.out.println ("Word" + word + " occurred " + count + " times.")

That was easy, wasn't it? Now our Scalable language has a simple way of accomplishing this. Let's take a look at that:

val str = "Scala is a multi-paradigm language. Scala is scalable too."
println ("Word" + word + " occurred " + str.split(" ").filter(_ == word).size + " times.")

That's it, a one-liner solution for the same problem. The code may not look familiar right now, but gradually you'll have command over it. By the end of this chapter, we'll understand everything that's needed to run a Scala program, not just a Hello World program, but one that does something.

Scala's no different. It runs on Java Virtual Machine (JVM), so Java folks must have an idea about it. If not, JVM is defined as an abstract computing machine that operates on a set of instructions (Java Bytecode). It enables a machine to run a Java program. So here's the conclusion: when we write Scala programs and compile them, they are converted into Java Bytecode and then run on JVM. Scala interoperates with all Java libraries. It's easier and, of course, possible to write our own Scala code and also incorporate library functions written in Java.

Scala is a multi-paradigm language; it's a mixture of object-oriented and functional programming. But what good is it to us?

主站蜘蛛池模板: 霍州市| 南华县| 岗巴县| 丹江口市| 辛集市| 鞍山市| 中江县| 安泽县| 枞阳县| 盐源县| 江安县| 乌鲁木齐县| 若尔盖县| 喜德县| 凯里市| 六盘水市| 大渡口区| 扎囊县| 蓝田县| 工布江达县| 油尖旺区| 武山县| 荣昌县| 沙坪坝区| 巫溪县| 惠来县| 尼勒克县| 沛县| 名山县| 新巴尔虎右旗| 阿克陶县| 通榆县| 兰考县| 潮安县| 庆阳市| 兖州市| 左贡县| 桃江县| 朝阳市| 武山县| 潮州市|