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

scala(11)A Tour of Scala: Polymorphic Methods

 
阅读更多
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
分享到:
评论

相关推荐

    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

    标签:11、parser、scala、combinators_2、lang、modules、jar包、java、API文档、中文版; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构...

    Scala函数式编程

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

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

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

    该项目是一款名为DAF4J的Java与Scala混合编写的...项目总文件量为135个,其中包含112个Java源文件、11个Scala源文件、4个XML配置文件、3个Markdown文件、2个文本文件、1个Git忽略文件、1个许可证文件和1个属性文件。

    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编程详解 第11讲-Scala编程详解:Map与Tuple 共8页.pptx

    Scala编程详解:数组操作之数组转换 共5页第11讲-Scala编程详解:Map与Tuple 共8页第12讲-Scala编程详解:面向对象编程之类 共12页第13讲-Scala编程详解:面向对象编程之对象 共9页第14讲-Scala编程详解:面向对象...

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

    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

    标签:11、xml_2、scala、lang、modules、jar包、java、中文文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准...

Global site tag (gtag.js) - Google Analytics