围炉网

一行代码,一篇日志,一个梦想,一个世界

scala 课堂学习笔记

  • on JVM seamless interoperability with JAVA
  • static, strong typing
  • object == singleton ??
  • function definitions:

def fun(x: Int): Int = 

result 

}

def fun = result

  • function calls

obj meth arg # only one arg

  • Call By Value(CBV)  vs  Call By Name(CBN)

def func(x: Int, y:Int) =x*x

func ( 2+2, deadloop)

def func(x:Int, y: => Int)         y: => Int Call by name , default CBV

  • scala/match

never fall through next case

  • pattern types

constant ==

variable  every value, refers to the value in the right hand side

wildcard _

  • 大写 默认认为 常量    否则用`pi`
  • Pattern Guard

case p if guard => expr

Constructor Pattern

  • case class

a factory method

immutable arguments

toString/hashCode/equals

copy

  • sealed Class

To make the superclass of case classes sealed.

A sealed class cannot have any new subclasses added except the ones in the same file.

You only need to worry about the subclasses you already know about.

definition of HOF high order function

there can be up to 22 parameters in the function

  • anonymous function

var f1=(x:Int) => x*x

var f2:Int=>Int = x => x*x

sum((x:Int)=>2*x,a,b)

->sum(x=>x*x,a,b)

->sum(2*_,a,b)  _与函数列表一一对应

  • curring 多参数列表
  • Operators

alphanumeric (including "_")

symbolic

alphanumeric_symbolic

def unary_!=

only +-!~ can be unary!

  • class trait extends with
  • object acts like static or singleton, you can invoke a method directly from an object without "new"
  • 伴生对象

class A {}

object A {}

  • Important Types

Any, AnyRef(java.lang.Object), ScalaObject, AnyVal

Type Nothing

{throw exception} is of type Nothing

useful for empty collections

Null Type

null 

  • List are immutable
  • List

a +: b

a :+ b

  • stream — delay evaluation / lazy evalution

lazy val  VS val VS def

  • partial function
  • actors  receive 

while vs loop

you can treat every native thread as actor

react

  • SBT (simple build tool)

manage dependencies     maven ivy?

typesafe.com

  

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

沪ICP备15009335号-2