:: Appends the object to the head of the list.The head part of an existing list[subclass X] is placed in another object A, which is a subclass Y, and if both a object and the original list object inherit from the same parent class, the list is displayed as the list[parent class].The parameters of the function are contravariant so that covariance cannot occur, so the nether is used to solve the problem. The object to which the:: Method returns is programmed as a list of upper bound object B.inter
Default value:def saymyname (name:string = "Jack") {println (name)}Variable parameters:def summoreparameters (elem:int*) = { var sum = 0for (e Use of underscores:def add (a:int,b:int) = a+bdef add2 = Add (_:int,2)Recursive functions: Need to explicitly give the return type of the functiondef FAC (n:int): Int = if (n function currying:Write this argument independently, this is curryingdef mulitply (X:int) (y:int) = x*ydef m2 = mulitply (2) _;Curry is the parameter can be separated, some of the
Sublime3 Compile run Scala program
Using Sublime3 plus repl to invoke the command line, it is still convenient to run some code snippets. But direct compilation runs Scala programs, you need to add some settings.
1. Configuration:
Select Tools->build system->new build system and replace with:
{"cmd": ["D:\\scala\\bin\\scala.bat", "$file"]}
Save file:scala.
From Scala wizard Chapter 1 functional objects
With the basic Scala knowledge gained from the previous chapters, you are ready to explore how to design objects with more comprehensive features in Scala. This chapter focuses on defining functional objects, that is, classes of objects without any variable states. As an example of running, we will create several Cl
NothingNothing is a subclass of all types, and is also a subclass of NULL. Nothing has objects, but can be used to define types. For example, if a method throws an exception, the return value type of the exception is nothing (although it is not returned).def get(index:Int):Int = { if0thrownew Exception(...) else ....}If the statement is an expression with a return value, there must be a return value type, if x NullNull is a subclass of all Anyref, in Scala's type system, Anyref is a subcla
it's so ....Come back at night suddenly saw into the huge manuscript, can't help to the three ye, and then nothing did 23 o'clock ... 88th: Scala uses the for expression to implement map, FLATMAP, filterGoal:For expression Implementation Map/flatmap/filterGains:Map/flatmap/filter is a generic typeMORE:。。。。。。------------------------------------------------------------------------------------information from DT Big Data Dream Factory public account:Dt_s
One of the core features of functional programming is to pass functions as parameters to functions, to define functions inside functions, and so on.1, hands-on Scala high-order functionsDeclares a list collection instance:
The package that contains the list collection is already pre-defined and automatically imported, so there is no need to import the package here, and this is where the list instantiation object is used, which is actually the App
This is a Scala by Example from the official website.The example of how Scala is used in more detail after tutorial1.Programing with Actors and Message2.Expressions and Simple Functions3.first-class Functions4.Class and Objects5.Case Classes and Pattern Matching6.Generic Types and Methods7.Lists8.for-comprehensions9.Mutable State10.Computing with Streams11.Lazy Values12.Implicit Parameters and Conversions13
Basic content1. Array definitionsDefinition of fixed length, variable array, element addition, deletion, sorting, summation and other common operationsImport Scala.collection.mutable.ArrayBufferimport scala.math.bigdecimal.roundingmode/** * @author Xwolf * @date 2017-04-09 18:23 * @since 1.8 */object Collection {def main (arg:array[string]): Unit ={Multiplearray ()}/** * Array Creation */private def create (): unit={val a = new array[string] (1) A (0) = "Hello" println (A.tobuffer) Val A b =a
The take in list is implemented with Listbuffer:version 2.10.xHowever, in the 2.11.x version, it is not:override def take (n:int): list[A] = if (IsEmpty | | n val h = new::(head, Nil)var t = hvar rest = tailvar i = 1while ({if (rest. isEmpty) return this; i i + = 1val NX = new::(rest. Head, Nil)t.tl = NXT = NXrest = rest. Tail}h}final Case class:: [B] (override Val head:b, private [Scala] var tl:list[b]) extends List[b]declared as VAR allows listbuffe
Final Case class:: [B] (private var hd:b, Private[scala] var tl:list[b]) extends List[b]TL is limited access through private to ensure the immutability of TL, so that in the building of the head element HD different and then the list of the same new list can be directly pointed to the TL, efficient.This ensures both functional programming and efficient creation of new objects.interested friends please pay attention to DT Big Data Dream Factory public
Note: This study originates from: DT Big Data DreamWorks (public number: Dt_spark)1.in the previous section we learned about Scala Hello World, specifically defining a function as follows:650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/70/40/wKiom1W1CI_DmfpAAAGAJtFJ734715.jpg "title=" 1.png " alt= "Wkiom1w1ci_dmfpaaagajtfj734715.jpg"/>2.define a function without parameters, as follows:650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M
3, hands on the abstract class in ScalaThe definition of an abstract class requires the use of the abstract keyword:
The above code defines and implements the abstract method, it is important to note that we put the direct running code in the trait subclass of the app, about the inside of the app helps us implement the Main method and manages the code written by the engineer;Here's a look at the use of uninitialized variables in an abstract class:
4, hands-on trait in ScalaTrait
it's so ....Come back at night suddenly saw into the huge manuscript, can't help to the three ye, and then nothing did 23 o'clock ... 85th: The powerful expressive battle of the for expression in ScalaGoal:Comparison and contact of higher order functions (FLATMAP,MAP) and for loops (initial)Gains:The back of the For loop is actually called map, but it is more likely to be programmed with a for loop if the statement is concise and expressive two.MORE:Run the instance code over and try it out.----
Scala Learning six: special characters in Scala
special characters in Scala instructions for use:
Object Scalaoperator {def main (args:array[string]) {///::: Three colons: 2 collections merged println ("-------[:::]") Val list1 = List (1, 2) Val list2 = List (3, 4) Val listcombine = List1::: List2 Listcombin
E.foreach (x = {println (x)}) println ("-----
The Akka provides scalable, real-time transaction processing capabilities.The Akka is based on actor and provides location transparency.The 1GB heap can have 2.5 million actors.Horizontal expansion, vertical expansion, fault tolerance 3 ways to solve.The actor of the tree structure, each actor has state and behavior.DT Big Data Dream Factory public account: Dt_spark.DT Big Data Dream Factory's public number is Dt_spark, every day will have big data actual combat video release, please continue to
= { //similar to defining a static method initialization, using C to refer to a method var c=c () c.apply () c.test () }}3. Use apply to achieve a singleton mode, let's do a test. Reference the associated object C with C and C1, respectivelyPackage Smart.iotclass Applyclass { }class a{ def Test () =println ("function test") def apply () =println ( "Hello class A");} Object b{ def apply () =println ("Hello object B");} Similar to a factory method, use the C apply method
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.