`

scala - a note on methods and operators

阅读更多

In this post, we are going to examine the some general rules/law that guide through the scala functions and operators. 

Let's check out the souce code, which should have abundant code that can help to explain the code within.. 

// operators_and_methods.scala

// in this file, we will examine the operator and method
// in essens, there is no dedicated operator (there might be, I don't know) and 
// every operator is actually a method call 

val sum = 1 + 2

val sumMore = (1).+(2)




val s = "Hello, world!"
s indexOf 'o'

// the operator is as the call of the following method call
s.indexOf('o')

// if you call a method that takes multiple arguments using operator notation 
// you have place those argument in parenthesis

s indexOf ('o', 5)
// which is equivalent 
s.indexOf('o', 5)

// below we will examine the  unary operator
// and teh unary operator can be a prefix or postfix operator. 



// when you have !variable or -variable, it is actually a method call 

-2.0
(2.0).unary_-

// and only +, -, !, ~  (other identifiers) can be used for the unary prefix operator 

// while postfix oerator are just method call wihtout paremters. 

val s =  "Hello, world"

s.toLowerCase
  
// and you can call in operator notation 
s toLowerCase


// there are some bitwise operator 
// hwere it is unique in Scala

-1 >> 31

-1 >>> 31

// == in scala is actually carefully crafted
1 == 2
1 != 2


List(1, 2, 3) == List( 1, 2, 3)


List(1, 2, 3) == null
null == List(1, 2, 3)


// if you desire refcerence  equality test, you can use the 'eq' or 'ne' operator 
List(1, 2, 3) eq List(1, 2, 3)

List(1, 2, 3) ne List(1, 2, 3)



// oeprator precedence and associativity 

// table of precedence
// (all other special characters) 
// * / %
// + - 
// : 
// = !
// < >
// & 
// ^
// | 
// (all leters) 
// ( all assignment operators) 

2 << 2  + 2 // resut is 32, think of why 


// one except to the rule above 
// if an operator ends in '=' 
// such as *= 
// and the operator is not one of the <=, >=, !=, ==
// then the operator is the same as the simple assignment (=)
var x = 10
val y = 3
x *= y + 1

x *= (y + 1)

// the associativity of the operator determines the way operators are grouped 
// the associativity of an operator in Scala is determined by the last character. 
// any operator that ends in ':' are invoked on the right operand, passing in the left operand
// but still the operand are evaluate from left to right .. though the associativity might be 
//var a = 5
//var b = 6
//a ::: b 
// is actually equal to 
//{ val x = a; b.:::(x)}


// rich wrapper
/// there is a concept that is called 
// implicit conversions, 
// and it is implicit conversion that endow the primitives 
// with additional methods 

// e.g. of some rich wrapper functions 
0 max 5

0 min 5

-2.7 abs

-2.7 isInfinity

4 to 6

"bob" capitalize

"robert " drop 2 

 

分享到:
评论

相关推荐

    scala-parser-combinators_2.12-1.1.0-API文档-中英对照版.zip

    赠送jar包:scala-parser-combinators_2.12-1.1.0.jar; 赠送原API文档:scala-parser-combinators_2.12-1.1.0-javadoc.jar; 赠送源代码:scala-parser-combinators_2.12-1.1.0-sources.jar; 赠送Maven依赖信息...

    scala-compiler-2.11.8-API文档-中英对照版.zip

    赠送jar包:scala-compiler-2.11.8.jar; 赠送原API文档:scala-compiler-2.11.8-javadoc.jar; 赠送源代码:scala-compiler-2.11.8-sources.jar; 赠送Maven依赖信息文件:scala-compiler-2.11.8.pom; 包含翻译后...

    scala-parser-combinators-2.11-1.0.4-API文档-中文版.zip

    赠送jar包:scala-parser-combinators_2.11-1.0.4.jar; 赠送原API文档:scala-parser-combinators_2.11-1.0.4-javadoc.jar; 赠送源代码:scala-parser-combinators_2.11-1.0.4-sources.jar; 赠送Maven依赖信息...

    scala-java8-compat_2.11-0.7.0-API文档-中英对照版.zip

    赠送jar包:scala-java8-compat_2.11-0.7.0.jar; 赠送原API文档:scala-java8-compat_2.11-0.7.0-javadoc.jar; 赠送源代码:scala-java8-compat_2.11-0.7.0-sources.jar; 赠送Maven依赖信息文件:scala-java8-...

    scala-java8-compat_2.11-0.7.0-API文档-中文版.zip

    赠送jar包:scala-java8-compat_2.11-0.7.0.jar; 赠送原API文档:scala-java8-compat_2.11-0.7.0-javadoc.jar; 赠送源代码:scala-java8-compat_2.11-0.7.0-sources.jar; 赠送Maven依赖信息文件:scala-java8-...

    scala-xml_2.12-1.0.6-API文档-中文版.zip

    赠送jar包:scala-xml_2.12-1.0.6.jar; 赠送原API文档:scala-xml_2.12-1.0.6-javadoc.jar; 赠送源代码:scala-xml_2.12-1.0.6-sources.jar; 赠送Maven依赖信息文件:scala-xml_2.12-1.0.6.pom; 包含翻译后的API...

    scala-xml_2.11-1.0.4-API文档-中英对照版.zip

    赠送jar包:scala-xml_2.11-1.0.4.jar; 赠送原API文档:scala-xml_2.11-1.0.4-javadoc.jar; 赠送源代码:scala-xml_2.11-1.0.4-sources.jar; 赠送Maven依赖信息文件:scala-xml_2.11-1.0.4.pom; 包含翻译后的API...

    scala-parser-combinators_2.11-1.0.4-API文档-中英对照版.zip

    赠送jar包:scala-parser-combinators_2.11-1.0.4.jar; 赠送原API文档:scala-parser-combinators_2.11-1.0.4-javadoc.jar; 赠送源代码:scala-parser-combinators_2.11-1.0.4-sources.jar; 包含翻译后的API...

    scala-2.12.14.zip&scala-2.12.11.tgz Linux版本压缩包.rar

    标题提到的"scala-2.12.14.zip&scala-2.12.11.tgz"是Scala的不同版本,针对不同的操作系统进行了打包。Windows用户可以使用".zip"格式的文件,而Linux用户则适合使用".tgz"格式的文件。这里我们主要关注Linux版本的...

    scala-xml_2.11-1.0.1-API文档-中文版.zip

    赠送jar包:scala-xml_2.11-1.0.1.jar; 赠送原API文档:scala-xml_2.11-1.0.1-javadoc.jar; 赠送源代码:scala-xml_2.11-1.0.1-sources.jar; 赠送Maven依赖信息文件:scala-xml_2.11-1.0.1.pom; 包含翻译后的API...

    mongo-scala-drive的使用demo

    在 Scala 中与 MongoDB 进行交互,通常我们会使用 `mongo-scala-driver`,而不是 `mongo-java-driver`,因为 Scala 驱动提供了更符合 Scala 语言特性的 API 设计。本示例将详细介绍如何使用 `mongo-scala-driver` ...

    scala-compiler-2.11.12-API文档-中文版.zip

    赠送jar包:scala-compiler-2.11.12.jar; 赠送原API文档:scala-compiler-2.11.12-javadoc.jar; 赠送源代码:scala-compiler-2.11.12-sources.jar; 赠送Maven依赖信息文件:scala-compiler-2.11.12.pom; 包含...

    scala-ide-plugin-eclipse

    scala eclipse插件.对应scala版本:2.10--2.11,对应eclipes版本:4.4--...update site:http://download.scala-ide.org/sdk/lithium/e44/scala211/stable/site 下载地址:http://scala-ide.org/download/current.html

    scala-compiler-2.11.0-API文档-中文版.zip

    赠送jar包:scala-compiler-2.11.0.jar; 赠送原API文档:scala-compiler-2.11.0-javadoc.jar; 赠送源代码:scala-compiler-2.11.0-sources.jar; 赠送Maven依赖信息文件:scala-compiler-2.11.0.pom; 包含翻译后...

    scala-library-2.11.8-API文档-中文版.zip

    赠送jar包:scala-library-2.11.8.jar; 赠送原API文档:scala-library-2.11.8-javadoc.jar; 赠送源代码:scala-library-2.11.8-sources.jar; 赠送Maven依赖信息文件:scala-library-2.11.8.pom; 包含翻译后的API...

    scala-intellij-bin-2016.3.9

    "scala-intellij-bin-2016.3.9"是针对Scala语言的一个特定版本的IntelliJ IDEA插件,该版本为2016.3.9。这个插件是专门为Scala开发者设计的,旨在提高他们在IntelliJ IDEA中的开发体验。 Scala是一种多范式编程语言...

    scala-intellij-bin-2018.3.2.zip

    scala-intellij-bin-2018.3.2.zip插件,亲测可用!!!scala-intellij-bin-2018.3.2.zip插件,亲测可用!!!scala-intellij-bin-2018.3.2.zip插件,亲测可用!!!

    scala-compiler-2.12.7-API文档-中英对照版.zip

    赠送jar包:scala-compiler-2.12.7.jar; 赠送原API文档:scala-compiler-2.12.7-javadoc.jar; 赠送源代码:scala-compiler-2.12.7-sources.jar; 赠送Maven依赖信息文件:scala-compiler-2.12.7.pom; 包含翻译后...

    scala-xml_2.11-1.0.5-API文档-中英对照版.zip

    赠送jar包:scala-xml_2.11-1.0.5.jar; 赠送原API文档:scala-xml_2.11-1.0.5-javadoc.jar; 赠送源代码:scala-xml_2.11-1.0.5-sources.jar; 赠送Maven依赖信息文件:scala-xml_2.11-1.0.5.pom; 包含翻译后的API...

    scala-compiler-2.12.7-API文档-中文版.zip

    赠送jar包:scala-compiler-2.12.7.jar; 赠送原API文档:scala-compiler-2.12.7-javadoc.jar; 赠送源代码:scala-compiler-2.12.7-sources.jar; 赠送Maven依赖信息文件:scala-compiler-2.12.7.pom; 包含翻译后...

Global site tag (gtag.js) - Google Analytics