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

scala(9)A Tour of Scala: Compound Types

 
阅读更多
scala(9)A Tour of Scala: Compound Types
Compound Types
trait Cloneable extends java.lang.Cloneable {
     override def clone(): Cloneable = { super.clone(); this }
}

trait Resetable {
     def reset: Unit
}

def cloneAndReset(obj: Cloneable with Resetable): Cloneable = {
     //...
}

Sequence Comprehensions
object ComprehensionTest1 extends App {
     def even(from: Int, to: Int): List[Int] =
          for (i <- List.range(from, to) if i % 2 == 0) yieldi
     Console.println(even(0, 20)) // List(0, 2, 4, 6, 8, 10, 12, 14, 16, 18)
}

object ComprehensionTest2 extends App {
  def foo(n: Int, v: Int) =
    for (i <- 0 until n;
         j <- i + 1 until n if i + j == v) yield
      Pair(i, j);
  foo(20, 32) foreach {
    case (i, j) =>
      println("(" + i + ", " + j + ")")
  }
}

Another easy way to implement this.

object ComprehensionTest3 extends App {
  for (i <- Iterator.range(0, 20);
       j <- Iterator.range(i + 1, 20) if i + j == 32)
  println("(" + i + ", " + j + ")")
}

Extractor Objects

object Twice{
  def apply(x:Int): Int = x * 2
  def unapply(z: Int): Option[Int] = if (z%2 == 0) Some(z/2) else None
}

object TwiceTest extends App{
     valx = Twice(21)
     println("apply value: " + x)    //apply value: 42
     xmatch { case Twice(n) => Console.println("unapply value: " + n) } //unapply value: 21
}

There are 2 syntactic conventions at work here:
1. case Twice(n) will cause an invocation of Twice.unapplly
2. val x = Twice(21) expands to val x = Twice.apply(21).

Generic Classes
class Stack[T] {
     var elems: List[T] = Nil
     def push(x: T) { elems = x::elems }
     def top: T = elems.head
     def pop() { elems = elems.tail }
}

Here is the parameterized with types classes, like in Java 5.

object GenericsTest extends App {
  val stack = new Stack[Int]
  stack.push(1)
  stack.push('a')
  println(stack.top) // 97
  stack.pop()
  println(stack.top) // 1
}

Implicit Parameters(Implicit in Scala)
There are 2 implicit in Scala, implicit transfer and implicit parameters.

implicit transfer
scala> def foo(msg: String) = println(msg)
foo: (msg: String)Unit

scala> foo(5)
<console>:9: error: type mismatch;
found : Int(5)
required: String
              foo(5)
                  ^

scala> implicit def intToString(x: Int) = x.toString
warning: there were 1 feature warnings; re-run with -feature for details
intToString: (x: Int)String

scala> foo(5)
5

But if we have 2 implicit transfer, it will give us some error messages:
Note that implicit conversions are not applicable because they are ambiguous:
both method int2String of type (x: Int)String
and method int2String2 of type (x: Int)String
are possible conversion functions from Int(5) to String
              foo(5)

implicit Parameters
cala> def findAnInt(implicit x : Int) = x
findAnInt: (implicit x: Int)Int

scala> findAnInt
<console>:9: error: could not find implicit value for parameter x: Int
              findAnInt
              ^

scala> implicit val test = 100
test: Int = 100

scala> findAnInt
res1: Int = 100

References:
http://www.scala-lang.org/node/110
http://www.scala-lang.org/node/112
http://www.scala-lang.org/node/114

http://www.zhangyf.net/2012/11/implicit-in-scala/

分享到:
评论

相关推荐

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

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

    Scala语法简明教程

    ### Scala语法简明教程知识点详解 #### Scala语言简史 - **诞生历史**:Scala起源于瑞士联邦理工学院洛桑(EPFL),由Martin Odersky在2001年开始设计,其灵感来源于Funnel——一种结合了函数式编程思想与Petri网...

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

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