内容简介:Scalais a general-purpose, object-oriented programming language. It provides support to functional programming. It also helps in avoiding bugs in complex applications. In this article, you get to know about Scala andScala is a programming language invented
Scalais a general-purpose, object-oriented programming language. It provides support to functional programming. It also helps in avoiding bugs in complex applications. In this article, you get to know about Scala and what is Scala used for ?
Introduction
Scala is a programming language invented in the year 2003 by Mr. Martin Odersky and his team. Scala released publicly in 2004 on the Java platform . The name Scala is a blend of scalable and languages . That signifies it designed to grow with the demands of its users.
- Scala is a Robust and High-Caliber programming language.
- It is also capable to outrun the speed of the fastest existing programming languages.
- it’s a compiler-based and a multi-paradigm language.
- Which makes it compact , fast and efficient .
- Scala uses Java Virtual Machine (JVM) for compilation.
- Firstly Scala Compiler compiles the Scala code and generates the byte code for the same.
- After that, it transferred to the Java Virtual Machine to generate the Output .
Platforms And Compilers
Scala runs on the Java Platform(JVM) . So it is compatible with all the existing Java programs . Even its compatibility with JAVA makes it well-suited to Android development . Because Android applications typically write in Java and translate from Java bytecode into Dalvik bytecode when packaged.
Let’s talk about some Scala compilers:-
Scala.js:-
- Scala.js is a Scala compiler.
- It compiles to JavaScript .
- Scala.js compiler makes it possible to write Scala programs that can run in web browsers and also in Node.js .
- The compiler was in development since 2013. But launched in 2015 with the version name(v0.6).
Scala native:-
- Scala native is also a Scala compiler.
- That mainly targets the LLVM compiler infrastructure to create executable code.
- It uses a lightweight managed runtime. Its first release was 0.1 on 14 March 2017 .
- The motive behind developing scala native is being faster than JIT compilation for the JVM.
- It achieved by eliminating the initial runtime compilation of code and also providing the ability to call native routines directly.
Key Features Of Scala
- In Scala, there are no static variables or methods. Scala uses singleton object , which is essentially class with only one object in the source file.
- Here you don,t require to mention data type and function return type explicitly. Scala is enough smart to deduce the type of data.
- In Scala evaluation is lazy by default. Scala evaluates expressions only when they are required.
- Scala provides a standard library that includes the actor model. So you can write concurrency control.
- Higher-order functions are the function that either takes a function as an argument or returns a function.
What is Scala Used for?
Scala has the capability to interpolate with the existingJava code and libraries. It is mainly a static type language. There is no concept of primitive type data in Scala. It is a multi-paradigm language that supports multi-core architecture. The main uses of Scala are described below.
Statically Typed language:-
Scala feels dynamic but it is strongly statically language. It provides type interface for variables and functions. A statically typed language avoids the mistake in code and also helps programmers to write proper code and debug code easily. But in dynamic languages, errors are visible only when you run a program.
Example:-
def dup[T](x: T, n: Int): List[T] = { if (n == ) Nil else x :: dup(x, n - 1) } println(dup[Int](3, 4)) println(dup("three", 3))
Method dup is parameterized with type T and value parameters x: T and n: Int . Scala type system automatically infers the type of the parameter according to the value.
Built-in Practices And Pattern:-
Scala was developed with the intention of creating new innovations in programming language research to mainstream languages like Java . This language already has some best practices and patterns that are built in the language. It also offers to adopt new languages like Python, Ruby, etc to implement functional programming.
Pattern matchinglets check a value against a pattern. You can use this in place of a switch statement or a series of if-else statements in Java.
import scala.util.Random val x: Int = Random.nextInt(10) x match { case => "zero" case 1 => "one" case 2 => "two" case _ => "many" }
More Expressive:-
Scala is inherently more expressive than Java. One who learns Scala after Java finds it easier as well as interesting . Let understand it by this example:-
Java code: Public class wordcount{ Public static void main(String [] args){ StringToken st= new StringToken(args[ ]); Map<String, Integer> map= new HashMap<String, Integer>(); while(st.hasMoreTokens()) { String word= st.nextToken(); Integer count= map.get(word); If(count == null) map.put(word, count+1); } System.out.println(map); } } Scala code: Object WordCountScala extends App { Println( args( ).split(“ ”).groupBy(x => x).map(t => t._1 -> t._2.length)) }
Multi-paradigm
Scala supports both kinds of programming object-oriented programming as well as functional programming . you can easily explore both sides of functional and OOP together. You are able to define different types associated with both data attributes and behavioral attributes. Scala functions allow you to pass values and also supports anonymous functions.
Inter-Operability:-
The interoperability feature of Java is one of the best options in Scala. This enables Scala developers to use all libraries of Java directly from Scala code. It is also possible to call Scala code from Java and the user can easily write any part of a program in Scala and rest in Java.
Demanding:-
The main use of scala is better growth and job. Scala will help to increase your demand and will make you even more marketable . Many companies like Twitter, LinkedIn, Foursquare, etc are using Scala.
Frameworks:-
Scala provides various libraries that can be used to build many frameworks . There are already many frameworks that have come to existence like Lift and Play . Akka is another Scala-based framework. IT is concurrent and established like a toolkit.
Precise Syntax:-
Scala has a very precise syntax as compared to Java. Java has very long syntax. Scala’s, this feature makes it more readable and concise . ScalaC can generate and work for better code.
Easy To Learn:-
Scala’s object-oriented functionality makes it easy to learn. Scala has clean syntax , nice librarie s as well as good online documentation .
Advantage and Disadvantage
- Highly functional
- Great for analytics
- Good IDE support
- Inherently immutable object
- Limited community presence
- Hard to learn
- Lack of ease of adoption
- Limited backward compatibility
Scala Frameworks And Libraries
How to write Scala programs
Here I am giving you a small example of how you can write Scala program . Firstly, you need to install Scala on your machine. And you must have to install jdk on your machine because Scala compiler creates .class file which is byte code. Scala interpreter executes this byte code by using jvm.
Example:-
object ScalaExample{ def main(args:Array[String]){ println "Hello Scala" } }
In the above code, you created an object ScalaExample. It contains a main method and display message using printIn method.
This file is saved with the name ScalaExample.scala .
Command to compile the code is : scalac ScalaExample.scala
Command to execute the compiled code is: scala ScalaExample
After executing code it will give the following output:-
Hello Scala
By using functional approach
def scalaExample{ println("Hello Scala") } scalaExample // Calling of function
Output
Hello Scala
Versions Of Scala
By now many versions of Scala has been launched. At present Scala 2.13 is in use. Scala 3.0 will release in start of 2020. Features it will contain are:-
- Scala 3.0 will promote programming idioms .
- It will be more simplified .
- It will consolidate language constructs .
- This will improve consistency , performance, and safety .
Scala Vs Other Languages
Scalahas been in a tough fight to overthrow Java. Java has remained one of the most popular , versatile programming languages since the time it was created. Scala was introduced with the intent of addressing many of the concerns developers have with Java.
Where many programming languages trying to compete with Java have failed, Scala has succeeded.
The language, built on top of the JVM , consists:
-Compatibility with Java
-Interoperability
Same as Java, Scala often compared with python . Python and Scala are the two major languages for Data Science , Big Data. Python and Scala both are object-oriented languages .
Let’s go throw some more specific points about Java, Scala, and Python:-
Java
- Java is a programming language developed by Sun microsystems in 1995 .
- Developers choose Java because it has great libraries , widely used Excellent tooling and a huge amount of documentation available.
- Some cons of Java are verbosity , NullpointerException , Overcompexity is praised in community culture and bloat .
-
Companies that use Java:-
Airbnb
Uber Technologies
NetFlix
Spotify
Instagram
Pinterest
Scala
- Scala is an acronym for Scalable language . that means is Scala grows with you.
- This language provides Static typing , Pattern matching , types . Scala uses Java Virtual Machine for compilation. Overall Scala is a fun language to use.
- Cons of Scala are slow compilation time , too few developers available , Multiple ropes to hang yourself.
-
Companies that use Scala:-
Twitter
Coursera
9GAG
Asana
Groupon
Reddit
Python
- Python is a general-purpose programming language, created by Guido Van Rossum .
- It has great libraries , Readable and beautiful code . In Python development is rapid .
- Python cons are:- still divided between python 2 and python 3 , poor syntax for functions . Package management is a mess .
-
Companies that use Python:-
Dropbox
Uber Technologies
Netflix
Instagram
Spotify
Technologies Written In Scala
- Apache Spark
- Scalding
- Apache Kafka
- Finagle
- Akka
- ADAM
- Liches
Future Scope
As mention, Scala has many uses once you learn it, In the future, it will surely increase Scala developer’s value in the market. They will be able to develop new frameworks and use the dynamic feature of this language. Scala being trending largely all over the world.
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
python学习手册(原书第5版)
马克·卢茨 / 机械工业出版社 / 2018-10 / 219
如果你想动手编写高效、高质量并且很容易与其他语言和工具集成的代码,本书将快速地帮助你利用Python提高效率。本书基于Python专家的流程培训课程编写,内容通俗易懂。本书包含很多注释的例子和插图,以帮助你开始使用Python2.7和3.3。每章都包含关于Python语言的重要组成部分的一节课。本书主要内容:了解Python的主要内置对象类型,如数字、列表和字典;创建和处理对象的Python语句,......一起来看看 《python学习手册(原书第5版)》 这本书的介绍吧!