- 浏览: 2551899 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
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 476NodeJS12 and Zlib It works as ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 337Traefik 2020(1)Introduction and ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 436Private Registry 2020(1)No auth ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 385Buffer 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 451GraphQL 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 318Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 294Serverless 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 ...
相关推荐
labview程序代码参考学习使用,希望对你有所帮助。
毕设和企业适用springboot生鲜鲜花类及数据处理平台源码+论文+视频.zip
毕设和企业适用springboot企业数据智能分析平台类及汽车管理平台源码+论文+视频
毕设和企业适用springboot社区物业类及企业创新研发平台源码+论文+视频
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Floating Text Example</title> <style> .floating-text { font-size: 24px; position: relative; animation: float 3s ease-in-out infinite; } @keyframes float { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-20px); } } </style> </head> <body> <div class="floating-text">Hello, I'm floating!</div> <script> document.addEventListener('DOMContentLoaded', function() {
毕设和企业适用springboot社交媒体分析平台类及智慧医疗管理平台源码+论文+视频
毕设和企业适用springboot生鲜鲜花类及餐饮管理平台源码+论文+视频
毕设和企业适用springboot人工智能客服系统类及用户行为分析平台源码+论文+视频
毕设和企业适用springboot全渠道电商平台类及个性化广告平台源码+论文+视频
毕设和企业适用springboot社交互动平台类及线上图书馆源码+论文+视频
毕设和企业适用springboot企业知识管理平台类及供应链优化平台源码+论文+视频
毕设和企业适用springboot企业健康管理平台类及数据处理平台源码+论文+视频.zip
内容概要:本文档是一份面向初学者的详细指南,重点介绍如何利用Vue.js 2.0快速创建和运行简单的Todo List应用。首先指导安装必需的Node.js、npm/yarn等环境准备,接着通过Vue CLI工具生成新的Vue项目,再详细介绍项目目录和组件的构建方式。最后提供了具体的方法实现添加和删除待办事项,并指导如何使用命令启动应用,查看结果。 适合人群:具备基础Web开发技能的前端开发新手,尤其是对Vue框架感兴趣的学习者。 使用场景及目标:作为初学者入门级的学习资料,本文档的目标是让读者能够在最短时间内掌握Vue.js的基础概念和技术栈的应用方式,以便日后可以独立地构建更加复杂的Vue应用。 其他说明:除了学习如何构建应用程序之外,本文档还涵盖了Vue的基本语法和数据绑定、事件处理机制等重要概念,对于理解Vue框架的工作原理十分有帮助。
毕设和企业适用springboot企业健康管理平台类及智能化系统源码+论文+视频.zip
毕设和企业适用springboot企业健康管理平台类及远程医疗平台源码+论文+视频.zip
毕设和企业适用springboot数据可视化类及数据智能化平台源码+论文+视频
毕设和企业适用springboot生鲜鲜花类及用户体验优化平台源码+论文+视频.zip
毕设和企业适用springboot人工智能客服系统类及虚拟银行平台源码+论文+视频
毕设和企业适用springboot社交应用平台类及云计算资源管理平台源码+论文+视频
毕设和企业适用springboot企业数据监控平台类及线上图书馆源码+论文+视频