- 浏览: 2551648 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
scala(11)A Tour of Scala: Polymorphic Methods
Polymorphic Methods
Methods in Scala can be parameterized with both values and types.
value parameters are enclosed in a pair of parentheses.
type parameters are declared within a pair of brackets.
object PolyTest extends App {
def dup[T](x: T, n: Int): List[T] =
if (n == 0) {
Nil
} else {
x :: dup(x, n - 1)
}
println(dup[Int](3, 4))//List(3, 3, 3, 3)
println(dup[String]("three", 3))//List(three, three, three)
println(dup("three", 3))
}
Scala Option, Some and None idiom
We return Null or Object in java, Null stands for failure, Object stands for succeed.
In Scala, we can return Option, where the Option object is either:
1. An instance of the Scala Some class
2. An instance of the Scala None class
Both Some and None are both children of Option.
Here are the example of None, Some and Option
package com.sillycat.easyscala.start.tour.tour3
object OptionSomeNoneTest {
def toInt(in: String): Option[Int] = {
try {
Some(Integer.parseInt(in.trim()))
} catch {
case e: NumberFormatException => None
}
}
def main(args: Array[String]): Unit = {
toInt("100") match {
case Some(i) => println(i)
case None => println("That didn't work.")
} // 100
toInt("hello") match {
case Some(i) => println(i)
case None => println("That didn't work.")
} //That didn't work.
val bag = List("1", "2", "foo", "3", "bar")
valsum = bag.flatMap(toInt).sum //flatMap know how to handle with None, Option, Some
println(sum) //6
}
}
Regular Expression Patterns
…snip…
Sealed Classes
…snip…
Traits
Almost like interface, but Scala allows traits to be partially implemented. In contrast to classes, traits may not have constructor parameters.
The example of Traits will be
package com.sillycat.easyscala.start.tour.tour3
trait Similarity {
def isSimilar(x: Any): Boolean
def isNotSimilar(x: Any): Boolean = !isSimilar(x)
}
We only implemented the isNotSimilar in the trait, the class extends the trait needs to be implemented the isSimilar method.
The test trait class will be as follow:
package com.sillycat.easyscala.start.tour.tour3
class Point(xc:Int, yc: Int) extends Similarity{
var x: Int = xc
var y: Int = yc
def isSimilar(obj:Any):Boolean = {
obj.isInstanceOf[Point] &&
obj.asInstanceOf[Point].x == x
}
}
object TraitsTest extends App{
val p1 = new Point(2,3)
val p2 = new Point(2,4)
val p3 = new Point(3,3)
println(p1.isNotSimilar(p2)) //false
println(p1.isNotSimilar(p3)) // true
println(p1.isNotSimilar(3)) //true
}
Map & FlatMap
Map works by applying a function to each element in the list.
def main(args: Array[String]): Unit = {
val l1 = List(1,2,3,4,5)
var l2 = l1.map(x => x*2)
println(l1 + " " + l2)
var l3 = l1.map(x => f(x))
println(l1 + " " + l3)
}
def f(x: Int) = if(x > 2) Some(x) else None
flagMap works applying a function that returns a sequence for each element in the list, and flattening the results into the original list.
def g(v:Int) = List(v-1, v, v+1)
var l4 = l1.map(x => g(x))
//List(1, 2, 3, 4, 5)
//List(List(0, 1, 2), List(1, 2, 3), List(2, 3, 4), List(3, 4, 5), List(4, 5, 6))
println (l1 + " " + l4)
var l5 = l1.flatMap(x=>g(x))
//List(1, 2, 3, 4, 5)
//List(0, 1, 2, 1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 5, 6)
println (l1 + " " + l5)
Option class will also consider as sequence that is either empty or has 1 item.
var l6 = l3.flatMap(x => x )
println (l3 + " " + l6)
//List(None,None,Some(3),Some(4),Some(5)) List(3,4,5)
References:
http://www.scala-lang.org/node/121
http://www.scala-lang.org/node/122
http://www.scala-lang.org/node/123
http://www.scala-lang.org/node/126
http://alvinalexander.com/scala/using-scala-option-some-none-idiom-function-java-null
http://www.brunton-spall.co.uk/post/2011/12/02/map-map-and-flatmap-in-scala/
http://richard.dallaway.com/in-praise-of-flatmap
Polymorphic Methods
Methods in Scala can be parameterized with both values and types.
value parameters are enclosed in a pair of parentheses.
type parameters are declared within a pair of brackets.
object PolyTest extends App {
def dup[T](x: T, n: Int): List[T] =
if (n == 0) {
Nil
} else {
x :: dup(x, n - 1)
}
println(dup[Int](3, 4))//List(3, 3, 3, 3)
println(dup[String]("three", 3))//List(three, three, three)
println(dup("three", 3))
}
Scala Option, Some and None idiom
We return Null or Object in java, Null stands for failure, Object stands for succeed.
In Scala, we can return Option, where the Option object is either:
1. An instance of the Scala Some class
2. An instance of the Scala None class
Both Some and None are both children of Option.
Here are the example of None, Some and Option
package com.sillycat.easyscala.start.tour.tour3
object OptionSomeNoneTest {
def toInt(in: String): Option[Int] = {
try {
Some(Integer.parseInt(in.trim()))
} catch {
case e: NumberFormatException => None
}
}
def main(args: Array[String]): Unit = {
toInt("100") match {
case Some(i) => println(i)
case None => println("That didn't work.")
} // 100
toInt("hello") match {
case Some(i) => println(i)
case None => println("That didn't work.")
} //That didn't work.
val bag = List("1", "2", "foo", "3", "bar")
valsum = bag.flatMap(toInt).sum //flatMap know how to handle with None, Option, Some
println(sum) //6
}
}
Regular Expression Patterns
…snip…
Sealed Classes
…snip…
Traits
Almost like interface, but Scala allows traits to be partially implemented. In contrast to classes, traits may not have constructor parameters.
The example of Traits will be
package com.sillycat.easyscala.start.tour.tour3
trait Similarity {
def isSimilar(x: Any): Boolean
def isNotSimilar(x: Any): Boolean = !isSimilar(x)
}
We only implemented the isNotSimilar in the trait, the class extends the trait needs to be implemented the isSimilar method.
The test trait class will be as follow:
package com.sillycat.easyscala.start.tour.tour3
class Point(xc:Int, yc: Int) extends Similarity{
var x: Int = xc
var y: Int = yc
def isSimilar(obj:Any):Boolean = {
obj.isInstanceOf[Point] &&
obj.asInstanceOf[Point].x == x
}
}
object TraitsTest extends App{
val p1 = new Point(2,3)
val p2 = new Point(2,4)
val p3 = new Point(3,3)
println(p1.isNotSimilar(p2)) //false
println(p1.isNotSimilar(p3)) // true
println(p1.isNotSimilar(3)) //true
}
Map & FlatMap
Map works by applying a function to each element in the list.
def main(args: Array[String]): Unit = {
val l1 = List(1,2,3,4,5)
var l2 = l1.map(x => x*2)
println(l1 + " " + l2)
var l3 = l1.map(x => f(x))
println(l1 + " " + l3)
}
def f(x: Int) = if(x > 2) Some(x) else None
flagMap works applying a function that returns a sequence for each element in the list, and flattening the results into the original list.
def g(v:Int) = List(v-1, v, v+1)
var l4 = l1.map(x => g(x))
//List(1, 2, 3, 4, 5)
//List(List(0, 1, 2), List(1, 2, 3), List(2, 3, 4), List(3, 4, 5), List(4, 5, 6))
println (l1 + " " + l4)
var l5 = l1.flatMap(x=>g(x))
//List(1, 2, 3, 4, 5)
//List(0, 1, 2, 1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 5, 6)
println (l1 + " " + l5)
Option class will also consider as sequence that is either empty or has 1 item.
var l6 = l3.flatMap(x => x )
println (l3 + " " + l6)
//List(None,None,Some(3),Some(4),Some(5)) List(3,4,5)
References:
http://www.scala-lang.org/node/121
http://www.scala-lang.org/node/122
http://www.scala-lang.org/node/123
http://www.scala-lang.org/node/126
http://alvinalexander.com/scala/using-scala-option-some-none-idiom-function-java-null
http://www.brunton-spall.co.uk/post/2011/12/02/map-map-and-flatmap-in-scala/
http://richard.dallaway.com/in-praise-of-flatmap
发表评论
-
NodeJS12 and Zlib
2020-04-01 07:44 475NodeJS12 and Zlib It works as ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 336Traefik 2020(1)Introduction and ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 435Private Registry 2020(1)No auth ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 384Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 478NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 423Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 337Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 247GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 450GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 328GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 314Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 317Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 293Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 312Serverless with NodeJS and Tenc ... -
NodeJS MySQL Library and npmjs
2020-02-07 06:21 288NodeJS MySQL Library and npmjs ... -
Python Library 2019(1)requests and aiohttp
2019-12-18 01:12 261Python Library 2019(1)requests ... -
NodeJS Installation 2019
2019-10-20 02:57 573NodeJS Installation 2019 Insta ... -
Monitor Tool 2019(2)Monit on Multiple Instances and Email Alerts
2019-10-18 10:57 265Monitor Tool 2019(2)Monit on Mu ... -
Sqlite Database 2019(1)Sqlite3 Installation and Docker phpsqliteadmin
2019-09-05 11:24 368Sqlite Database 2019(1)Sqlite3 ... -
Supervisor 2019(2)Ubuntu and Multiple Services
2019-08-19 10:53 370Supervisor 2019(2)Ubuntu and Mu ...
相关推荐
赠送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...
赠送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依赖信息...
标签:11、parser、scala、combinators_2、lang、modules、jar包、java、API文档、中文版; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构...
很大篇幅都放在,使用scala实现scala默认库文件的API中,通过对简单的函数式编程逻辑的介绍和实践,主要是实践,建立起来一个比较明晰的scala思维模式,或者叫函数式编程的思维模式。 2 无副作用的函数式编程,同时...
赠送jar包:scala-reflect-2.12.10.jar; 赠送原API文档:scala-reflect-2.12.10-javadoc.jar; 赠送源代码:scala-reflect-2.12.10-sources.jar; 赠送Maven依赖信息文件:scala-reflect-2.12.10.pom; 包含翻译后...
Learn Scala is split into four parts: a tour of Scala, a comparison between Java and Scala, Scala-specific features and functional programming idioms, and finally a discussion about adopting Scala in...
标题提到的"scala-2.12.14.zip&scala-2.12.11.tgz"是Scala的不同版本,针对不同的操作系统进行了打包。Windows用户可以使用".zip"格式的文件,而Linux用户则适合使用".tgz"格式的文件。这里我们主要关注Linux版本的...
赠送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; 包含...
赠送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; 包含翻译后...
赠送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; 包含翻译后...
赠送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; 包含翻译后...
该项目是一款名为DAF4J的Java与Scala混合编写的...项目总文件量为135个,其中包含112个Java源文件、11个Scala源文件、4个XML配置文件、3个Markdown文件、2个文本文件、1个Git忽略文件、1个许可证文件和1个属性文件。
赠送jar包:scala-reflect-2.12.7.jar; 赠送原API文档:scala-reflect-2.12.7-javadoc.jar; 赠送源代码:scala-reflect-2.12.7-sources.jar; 赠送Maven依赖信息文件:scala-reflect-2.12.7.pom; 包含翻译后的API...
Scala编程详解:数组操作之数组转换 共5页第11讲-Scala编程详解:Map与Tuple 共8页第12讲-Scala编程详解:面向对象编程之类 共12页第13讲-Scala编程详解:面向对象编程之对象 共9页第14讲-Scala编程详解:面向对象...
赠送jar包:scala-reflect-2.11.8.jar; 赠送原API文档:scala-reflect-2.11.8-javadoc.jar; 赠送源代码:scala-reflect-2.11.8-sources.jar; 赠送Maven依赖信息文件:scala-reflect-2.11.8.pom; 包含翻译后的API...
赠送jar包:scala-reflect-2.11.12.jar; 赠送原API文档:scala-reflect-2.11.12-javadoc.jar; 赠送源代码:scala-reflect-2.11.12-sources.jar; 赠送Maven依赖信息文件:scala-reflect-2.11.12.pom; 包含翻译后...
赠送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...
赠送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; 包含翻译后...
赠送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; 包含翻译后...
标签:11、xml_2、scala、lang、modules、jar包、java、中文文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准...