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

scala(7)Learn CH9 Programming in Scala from others

 
阅读更多
scala(7)Learn CH9 Programming in Scala from others
Control Abstraction  File Top Function
package com.sillycat.easyscala.lesson7
import java.io.FileReader

object SafeFile {
  def main(args: Array[String]): Unit = {
    safeFileReaderOp("./readme.md", print)
  }

  //Function passed to the upper function
  def print(reader: FileReader) = {
    val i = reader.read
    println(i.toChar)
  }

  def safeFileReaderOp(filename: String,
    op: FileReader => Unit) = {
    val reader = new FileReader(filename)
    try {
      op(reader)
    } finally {
      reader.close()
    }
  }
}

A lot of top functions are provided in Scala. They are really helpful.
package com.sillycat.easyscala.lesson7

object CollectionFunctions {
  def main(args: Array[String]): Unit = {
    val l = List(1, 3, 5, 7, -1)

    // check the elements greater than 0
    val hasNeg = l.exists((n: Int) => n > 0)
    println("has neg " + hasNeg) //has neg true

    // check if there is odd element
    val hasOdd = l.exists(_ % 2 == 0)
    println("has odd " + hasOdd) //has odd false

    // count the elements greater than 2
    val largeThen2 = l.count(_ > 2)
    println("count of > 2 = " + largeThen2) //count of > 2 = 3

    // copy the elements greater than 0 to another newly list
    val pos = l.filter(_ > 0)
    println("all positive " + pos) //List(1, 3, 5, 7)

    // check if all the elements are greater than 0
    val allPos = l.forall(_ > 0)
    println("is all positive " + allPos) //is all positive false

    // just print every element
    l.foreach(print _)  //1357-1
    println

    // sum all the element
    var sum = 0
    l.foreach(sum += _)
    println("sum is " + sum) //sum is 15

    //square all the elements
    val square = l.map((n: Int) => n * n)
    println("squared list " + square) //List(1, 9, 25, 49, 1)

    // reverse all the element
    val reverse = l.reverse
    println("reversed list " + reverse) //List(-1, 7, 5, 3, 1)

    // sort
    val sort = l.sortWith((a, b) => a > b)
    println("sorted list " + sort) //(7, 5, 3, 1, -1)
  }
}

Make some parameter constant, from my understanding, they are new functions with constant parameters.
package com.sillycat.easyscala.lesson7

object Curry {
  def main(args: Array[String]): Unit = {
    //take 2 group of parameters
    val v = sum(3)(4)
    println(v) //7

    //make the first parameter constant
    val s1 = sum(1) _
    val v1 = s1(3)
    println(v1) //4, equal to sum(1)(3)

    //make the first parameter constant
    val s2 = sum3(1) _
    val v2 = s2(2)(3)
    println(v2)   //6

    // make the first and second parameter constant
    val s3 = sum3(1)(2) _
    val v3 = s3(3)
    println(v3) // 6

    //s2=sum3(1)_
    val s4 = s2(2)   // s4 = sum3(1)(2)
    val v4 = s4(3)   // v4 = sum3(1)(2)(3)
    println(v4)    //6

    val v5 = sum2(1)(2, 3)   //6

    val s6 = sum2(1) _
    val v6 = s6(2, 3)    //sum2(1)(2,3) 6
  }

  def sum(x: Int)(y: Int) = x + y

  def sum3(x: Int)(y: Int)(z: Int) = x + y + z

  def sum2(x: Int)(y: Int, z: Int) = x + y + z
}

Pass the functions as parameters byName, byValue
package com.sillycat.easyscala.lesson7

object CallByName {
  def main(args: Array[String]): Unit = {
    val o1 = new O()
    val o2 = new O()
    byName(o1 > o2)
    byValue(o1 > o2)
  }

  //a class named O, what an useless name
  class O {
    def >(o: O): Boolean = {
      println("compare")
      true
    }
  }

  // by name parameter, predicate is the name of parameter, actually, it is
  // predicate
  def byName(predicate: => Boolean) = {
    println("before predicate byName")    //before predicate byName
    predicate                             //compare
    println("after predicate byName")     //after predicate byName
  }

  // by Value
  def byValue(predicate: Boolean) = {
    println("before predicate byValue")    //compare
    predicate                              //before predicate byValue
    println("after predicate byValue")     //after predicate byValue
  }
}



References:
http://snowriver.org/blog/2011/03/21/programming-in-scala-ch9-control-abstraction/



分享到:
评论

相关推荐

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

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

    Programming in Scala 2nd Edition

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

    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