`
sillycat
  • 浏览: 2551294 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

scala(12)A Tour of Scala: Upper Type Bounds

 
阅读更多
scala(12)A Tour of Scala: Upper Type Bounds

InMutableList
head - return the first element of the List
tail    - return the rest of the List

  def main(args: Array[String]): Unit = {
    var l1 = List(1,3,5,7)
    println(l1.head + " " + l1.tail) //1 List(3,5,7)
  }

Upper Type Bounds
An upper type bound T <: A  declares that type variable T refers to a subtype of type A.
trait Similar {
  def isSimilar(x: Any): Boolean
}


case class MyInt(x: Int) extends Similar {
  def isSimilar(m: Any): Boolean = {
    m.isInstanceOf[MyInt] && m.asInstanceOf[MyInt].x == x
  }
}


object ListUpperTypeBound {


  def findSimilar[T <: Similar](e: T, xs: List[T]): Boolean = {
    if (xs.isEmpty) false
    else if (e.isSimilar(xs.head)) true
    else findSimilar[T](e, xs.tail)
  }


  def main(args: Array[String]): Unit = {
    var l1 = List(1, 3, 5, 7)
    println(l1.head + " " + l1.tail) //1 List(3,5,7)


    val list: List[MyInt] = List(MyInt(1), MyInt(2), MyInt(3))
    println(findSimilar[MyInt](MyInt(4), list))  //false
    println(findSimilar[MyInt](MyInt(2), list))  //true
  }


}

Lower Type Bounds
The term T >: A

Tips: Recall Some Old Scala Syntax
Array()
List()  Manipulate the List()  list1:::list2, 13::list1, list1.::(13), Remember the differences among :::, ::, .::
(1,2,3) Tuple
Set()  Manipulate the Set      set1 += 3, the operator is +=
HashSet()     Manipulate the HashSet   hashSet1.+=(5)   .+=
Map()           Manipulate the Map map1.+=("4" -> "1000")

For Statement
for(file <- files)        for(file <- files if (file.getName.endsWith("bin"))  )
for(
     file <- files
     if(file.isFile)
     if(file.getName.endsWith("gitignore"))
)

val array_files = for (file <- files if(file.isFile)) yield file

parameter match {
     case "1" =>
          …
     case "2" =>
          …
     case _ =>
          ….
}

def speed(distance: Double, time: Double) = distance / time
val a = speed(distance = 100, time = 9)
Magic Collection Function List()    list1.exists((n:Int) => n >0), list1.exists(_%2 == 0)   list1.count(_>2)
                                                  list1.filter(_>0)
Generic Classes    Stack[T]

class Point(xc:Int, yc:Int) extends Similarity {}
flatMap will flat the results into the original list.

References:
http://www.scala-lang.org/node/136
http://www.scala-lang.org/node/137

http://www.scala-lang.org/api/current/index.html#scala.collection.mutable.MutableList

scala 2 ~ 11
scala(2) http://sillycat.iteye.com/blog/1536386
scala(3) http://sillycat.iteye.com/blog/1536391
scala(4) http://sillycat.iteye.com/blog/1536392
scala(5) http://sillycat.iteye.com/blog/1735124
scala(6) http://sillycat.iteye.com/blog/1748794
scala(7) http://sillycat.iteye.com/blog/1748937
scala(8) http://sillycat.iteye.com/blog/1775456
scala(9) http://sillycat.iteye.com/blog/1775972
scala(10) http://sillycat.iteye.com/blog/1775984
scala(11) http://sillycat.iteye.com/blog/1821490
分享到:
评论

相关推荐

    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-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-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函数式编程

    很大篇幅都放在,使用scala实现scala默认库文件的API中,通过对简单的函数式编程逻辑的介绍和实践,主要是实践,建立起来一个比较明晰的scala思维模式,或者叫函数式编程的思维模式。 2 无副作用的函数式编程,同时...

    2018 Scala for Java Developers: A Practical Primer

    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-reflect-2.12.10-API文档-中文版.zip

    赠送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; 包含翻译后...

    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-2.12.12 windows版本安装包

    有需要下载 scala-2.12.12.msi 的小伙伴,可以直接下载,官网下载实在是太慢了,o(╥﹏╥)o

    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-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-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-reflect-2.12.7-API文档-中文版.zip

    赠送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-reflect-2.11.8-API文档-中文版.zip

    赠送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...

    scala-reflect-2.11.12-API文档-中英对照版.zip

    赠送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; 包含翻译后...

    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-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; 包含翻译后...

    基于Java和Scala的DAF4J设计源码:数据访问简化与统一接口实现

    该项目是一款名为DAF4J的Java与Scala混合编写的开源数据访问框架源码,旨在简化并统一Java的数据访问层结构。该框架提供了一套简洁易读的领域特定语言(DSL),以实现数据访问的统一接口。项目总文件量为135个,其中...

    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.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...

    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...

Global site tag (gtag.js) - Google Analytics