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/