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

scala(6)Learn CH8 Programming in Scala from others

 
阅读更多
scala(6)Learn CH8 Programming in Scala from others
Function can be used as an parameter, because function is also an object. It can be changed during processing.

package com.sillycat.easyscala.lesson6

object TestMutableFunction {
  def main(args: Array[String]): Unit = {
    //defined a function x+1
    var increase = (x: Int) => x + 1
    println(increase(10))

    //redefined the function to x+5
    increase = (x: Int) => x + 5
    println(increase(10))

    val l = List(1, 3, 5, 7, 9, 11)
    println(l)

    val l2 = l.filter((x: Int) => x > 2)
    println(l2)

    val l3 = l.filter(x => x > 3)
    println(l3)

    val l4 = l.filter(_ > 5)
    println(l4)

    val f = (_: Int) + (_: Double)
    val v5 = f(3, 5)
    println(v5)

    //sum with multiple parameters
    val asum = sum _
    val v6 = asum(1, 2, 3)
    println(v6)

    val psum = sum(3, _: Int, 5)
    val v7 = psum(7)
    println(v7)
  }

  def sum(a: Int, b: Int, c: Int): Int = {
    a + b + c
  }
}

Some kinds of Closure like this:
package com.sillycat.easyscala.lesson6

object TestClosure {

  def main(args: Array[String]): Unit = {
    var more = 0
    //in function, we can use the variable defined outside the function
    val mf = (_: Int) + more
    println(mf(10))

    //the variable can be changed
    more = 10
    println(mf(10))

    val l = List(1, 3, 4, 5, 9)
    var sum = 0
    l.foreach(sum += _)
    println(sum)
  }

}

Our parameters for functions can be multiple and changeable.
package com.sillycat.easyscala.lesson6

object RepeatedParams {
  def main(args: Array[String]): Unit = {
    echo()
    echo("Hello")
    echo("Hello", "World")

    val a = Array("Hello", "world")
    // take all the elements in a to parameters pass to echo
    echo(a: _*)
  }

  // The parameters are multiple and changeable
  def echo(args: String*): Unit = {
    for (arg <- args)
      println(arg)
  }
}

Sometimes, it is useful to give names to the parameters in functions. We do not need to care about the order of the parameters if we provide the parameter names.
package com.sillycat.easyscala.lesson6

object NamedArgument {
  def main(args: Array[String]): Unit = {
    val a = speed(distance = 100, time = 9)
    println(a) //11.1111111111

    //give the parameter names during calling the functions
    val b = speed(time = 10, distance = 101)
    println(b) //10.1
   
    println(speed(1,2)) //0.5
  }

  //define a name for the function
  def speed(distance: Double, time: Double) = distance / time
}



References:
http://snowriver.org/blog/2011/03/20/programming-in-scala-ch8-function-and-closer/
http://snowriver.org/blog/tag/scala/


分享到:
评论

相关推荐

    Programming In Scala 中文版及英文版

    《Programming In Scala》是一本权威的Scala编程语言教程,它由Martin Odersky(Scala的创造者)、Lex Spoon 和 Bill Venners 共同编写。中文版包含了1到13章的内容,这些章节涵盖了Scala的基础知识和基本应用,适合...

    programming in scala, 3nd edition

    总之,《Programming in Scala》是一本非常有价值的书籍,无论你是初学者还是有经验的程序员,都能从中获得宝贵的知识和技能。通过本书的学习,你可以更好地理解和运用Scala这一强大而优雅的编程语言。

    Functional Programming in Scala

    WHAT'S INSIDE:, Functional programming from square one in clear, readable language, No prior Scala experience needed, Learn both the hows and whys of FP, No prior experience with FP or Scala is ...

    function programming in scala 英文原版 scala函数式编程

    英文原版 scala函数式编程 function programming in scala

    programming in Scala_ch

    #### Scala 编程语言简介 Scala是一种多范式编程语言,它融合了面向对象编程和函数式编程的特性。该语言旨在提高代码的可读性和表达能力,同时支持大规模应用程序的开发。Scala语言由Martin Odersky创建,并由他领导...

    programming in scala(第三版)

    《Programming in Scala》第三版是一本深入探讨Scala编程语言的权威指南,由Martin Odersky、Lex Spoon 和 Bill Venners合著。这本书以高清英文原版的形式呈现,旨在帮助读者全面理解Scala的精髓,无论你是初学者...

    Programming.in.Scala.pdf

    《Programming in Scala.pdf》是一本专注于Scala编程语言的书籍。Scala是一种现代的多范式编程语言,专为可扩展性而设计。该书旨在为读者提供深入理解Scala语言的教程。 ### 描述 描述中提及的内容表明,...

    Programming in Scala 2nd Edition

    《Programming in Scala》第二版是一本全面详尽的Scala编程指南,由Scala语言的主要设计者Martin Odersky、Lex Spoon和Bill Venners共同撰写。本书是为Scala 2.8版本更新的,因此包含了最新的特性和技术改进。 ####...

    Programming in Scala.pdf

    《Programming in Scala》是Scala编程语言的一本权威指南,由Martin Odersky、Lex Spoon 和 Bill Venners 合著,被广泛认为是学习Scala的首选教材。这本书深入介绍了Scala语言,涵盖了从基础语法到高级特性的全方位...

    Programming Scala- Scalability = Functional Programming + Objects, 2 edition

    注意是Programming Scala的第二版,而不是Programming in Scala的第二版,更注重于与Spark相关的知识!强烈推荐!Programming Scala- Scalability = Functional Programming + Objects, 2 edition

    Programming in Scala

    Programming in Scala is the definitive book on Scala, the new language for the Java Platform that blends object-oriented and functional programming concepts into a unique and powerful tool for ...

    Programming in Scala 3rd edition英文版+代码

    通过阅读《Programming in Scala》第三版并实践提供的源代码,学习者将能够掌握Scala的核心概念,包括其面向对象和函数式编程的融合、强大的类型系统、模式匹配以及并发处理能力。这将为开发者打开新的编程视角,...

    Programming in scala-3rd.pdf

    《Programming in Scala, Third Edition》是一本关于Scala编程语言的权威指南,由Martin Odersky、Lex Spoon和Bill Venners共同编写。这本书通过详细的解释和实例,为读者提供了一个深入理解Scala语言的机会,包括它...

    Scala 编程英文第三版;Programming in Scala, 3rd edition

    总之,《Programming in Scala, 3rd edition》被认为是一本全面覆盖Scala语言及其库的书籍,如容器和actor库。这本书不仅适合作为新手学习Scala的教材,也适合那些希望更深入理解Scala内部工作原理的程序员。它能够...

    Programming in Scala 3rd 第3版 pdf 0分

    Programming in Scala 3rd 英文pdf

    Learning+Concurrent+Programming+in+Scala,+2nd+Edition-2017.mobi

    Concurrent and parallel programming have progressed from niche disciplines, of interest only to kernel programming and high-performance computing, to something that every competent programmer must ...

    Functional Programming in Scala正式版

    综上所述,《Functional Programming in Scala》是一本全面覆盖函数式编程基础知识到高级技术的权威指南。无论你是初学者还是有一定经验的开发者,都可以从中获得丰富的知识和实践经验。通过本书的学习,你不仅能够...

    Programming in Scala 英文版

    《Programming in Scala》介绍了一种新的编程语言,它把面向对象和函数式编程概念有机地结合为整体,从而形成一种完整统一、语义丰富的新思维体系。《Scala编程》循序渐进,由浅入深,经作者精心组织、仔细编排,将...

Global site tag (gtag.js) - Google Analytics