- 浏览: 2539901 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
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/
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/
发表评论
-
NodeJS12 and Zlib
2020-04-01 07:44 467NodeJS12 and Zlib It works as ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 328Traefik 2020(1)Introduction and ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 428Private Registry 2020(1)No auth ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 376Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 464NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 413Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 330Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 242GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 443GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 320GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 306Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 310Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 285Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 302Serverless with NodeJS and Tenc ... -
NodeJS MySQL Library and npmjs
2020-02-07 06:21 276NodeJS MySQL Library and npmjs ... -
Python Library 2019(1)requests and aiohttp
2019-12-18 01:12 254Python Library 2019(1)requests ... -
NodeJS Installation 2019
2019-10-20 02:57 564NodeJS Installation 2019 Insta ... -
Monitor Tool 2019(2)Monit on Multiple Instances and Email Alerts
2019-10-18 10:57 255Monitor Tool 2019(2)Monit on Mu ... -
Sqlite Database 2019(1)Sqlite3 Installation and Docker phpsqliteadmin
2019-09-05 11:24 356Sqlite Database 2019(1)Sqlite3 ... -
Supervisor 2019(2)Ubuntu and Multiple Services
2019-08-19 10:53 363Supervisor 2019(2)Ubuntu and Mu ...
相关推荐
《Programming In Scala》是一本权威的Scala编程语言教程,它由Martin Odersky(Scala的创造者)、Lex Spoon 和 Bill Venners 共同编写。中文版包含了1到13章的内容,这些章节涵盖了Scala的基础知识和基本应用,适合...
总之,《Programming in Scala》是一本非常有价值的书籍,无论你是初学者还是有经验的程序员,都能从中获得宝贵的知识和技能。通过本书的学习,你可以更好地理解和运用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 ...
英文原版 scala函数式编程 function programming in scala
#### Scala 编程语言简介 Scala是一种多范式编程语言,它融合了面向对象编程和函数式编程的特性。该语言旨在提高代码的可读性和表达能力,同时支持大规模应用程序的开发。Scala语言由Martin Odersky创建,并由他领导...
《Programming in Scala》第三版是一本深入探讨Scala编程语言的权威指南,由Martin Odersky、Lex Spoon 和 Bill Venners合著。这本书以高清英文原版的形式呈现,旨在帮助读者全面理解Scala的精髓,无论你是初学者...
《Programming in Scala.pdf》是一本专注于Scala编程语言的书籍。Scala是一种现代的多范式编程语言,专为可扩展性而设计。该书旨在为读者提供深入理解Scala语言的教程。 ### 描述 描述中提及的内容表明,...
《Programming in Scala》是Scala编程语言的一本权威指南,由Martin Odersky、Lex Spoon 和 Bill Venners 合著,被广泛认为是学习Scala的首选教材。这本书深入介绍了Scala语言,涵盖了从基础语法到高级特性的全方位...
《Programming in Scala》第二版是一本全面详尽的Scala编程指南,由Scala语言的主要设计者Martin Odersky、Lex Spoon和Bill Venners共同撰写。本书是为Scala 2.8版本更新的,因此包含了最新的特性和技术改进。 ####...
注意是Programming Scala的第二版,而不是Programming in Scala的第二版,更注重于与Spark相关的知识!强烈推荐!Programming Scala- Scalability = Functional Programming + Objects, 2 edition
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》第三版并实践提供的源代码,学习者将能够掌握Scala的核心概念,包括其面向对象和函数式编程的融合、强大的类型系统、模式匹配以及并发处理能力。这将为开发者打开新的编程视角,...
《Programming in Scala, Third Edition》是一本关于Scala编程语言的权威指南,由Martin Odersky、Lex Spoon和Bill Venners共同编写。这本书通过详细的解释和实例,为读者提供了一个深入理解Scala语言的机会,包括它...
总之,《Programming in Scala, 3rd edition》被认为是一本全面覆盖Scala语言及其库的书籍,如容器和actor库。这本书不仅适合作为新手学习Scala的教材,也适合那些希望更深入理解Scala内部工作原理的程序员。它能够...
Programming in Scala 3rd 英文pdf
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》是一本全面覆盖函数式编程基础知识到高级技术的权威指南。无论你是初学者还是有一定经验的开发者,都可以从中获得丰富的知识和实践经验。通过本书的学习,你不仅能够...
《Programming in Scala》介绍了一种新的编程语言,它把面向对象和函数式编程概念有机地结合为整体,从而形成一种完整统一、语义丰富的新思维体系。《Scala编程》循序渐进,由浅入深,经作者精心组织、仔细编排,将...