- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
c# barcode generator library open source FUN WITH FUNCTIONS, AND NEVER HAVING TO CLOSE THAT JDBC CONNECTION in Font
CHAPTER 4 FUN WITH FUNCTIONS, AND NEVER HAVING TO CLOSE THAT JDBC CONNECTION Data Matrix ECC200 Creation In None Using Barcode printer for Font Control to generate, create ECC200 image in Font applications. www.OnBarcode.comGenerating Barcode In None Using Barcode maker for Font Control to generate, create Barcode image in Font applications. www.OnBarcode.comAnd you would call this code: UPC-A Supplement 5 Drawer In None Using Barcode generator for Font Control to generate, create GTIN - 12 image in Font applications. www.OnBarcode.comCode 39 Drawer In None Using Barcode generator for Font Control to generate, create Code 39 Extended image in Font applications. www.OnBarcode.comlog(INFO, "The value is "+value) USS Code 128 Generation In None Using Barcode encoder for Font Control to generate, create ANSI/AIM Code 128 image in Font applications. www.OnBarcode.comPrint Barcode In None Using Barcode encoder for Font Control to generate, create Barcode image in Font applications. www.OnBarcode.comThe Scala version passes "The value is "+value as a function that is evaluated each time it is accessed in the log method. The log method will access it only if the log message is going to be printed. Your code is cleaner because you don t have to repeatedly test the log level, but it performs as well as the previous Java code that has the inline test. In order to make something call-by-name, just put => before the type. So, foo(s: String) is call-by-reference, and foo(s: => String) is call-by-name. You may be wondering how the code could possibly perform as well if a function object is being created and handed off to the log method. In the JVM, the cost of creating an object that never escapes the current thread and is very short-lived is zero or very near zero. The JVM may also inline the log method such that the test is performed without an actual method call. The result is that your code will run as quickly with the Scala code as it will with the Java code that has the repeated test for log level. The first use of call-by-name is passing an expression that takes a long time to evaluate that may not be evaluated. The second use for call-by-name is the situation where we want to evaluate the expression many times in the target method, for example, if we want to evaluate an expression until some condition is met. That condition could be until the expression returns false or until the expression returns null. For example, we could collect all the Strings returned from an expression until we encounter a null: QR Code 2d Barcode Encoder In None Using Barcode creator for Font Control to generate, create Quick Response Code image in Font applications. www.OnBarcode.comMSI Plessey Creator In None Using Barcode maker for Font Control to generate, create MSI Plessey image in Font applications. www.OnBarcode.comdef allStrings(expr: => String): List[String] = expr match { case null => Nil case s => s :: allStrings(expr) } Data Matrix Scanner In .NET Framework Using Barcode recognizer for VS .NET Control to read, scan read, scan image in .NET applications. www.OnBarcode.comPainting DataMatrix In Objective-C Using Barcode drawer for iPhone Control to generate, create Data Matrix ECC200 image in iPhone applications. www.OnBarcode.comWe can test this method: Data Matrix ECC200 Generation In Visual Basic .NET Using Barcode printer for .NET framework Control to generate, create DataMatrix image in .NET applications. www.OnBarcode.comPrint GTIN - 13 In Visual Studio .NET Using Barcode creator for Reporting Service Control to generate, create EAN 13 image in Reporting Service applications. www.OnBarcode.comscala> import java.io._ Generating USS Code 39 In Visual Studio .NET Using Barcode encoder for Visual Studio .NET Control to generate, create Code-39 image in VS .NET applications. www.OnBarcode.comMaking Barcode In Java Using Barcode printer for Android Control to generate, create Barcode image in Android applications. www.OnBarcode.comimport java.io._ Making ANSI/AIM Code 39 In Java Using Barcode generator for Java Control to generate, create USS Code 39 image in Java applications. www.OnBarcode.comRead PDF-417 2d Barcode In None Using Barcode reader for Software Control to read, scan read, scan image in Software applications. www.OnBarcode.comscala> val br = new BufferedReader(new FileReader("foo.txt")) Barcode Drawer In None Using Barcode creator for Software Control to generate, create Barcode image in Software applications. www.OnBarcode.comScanning Barcode In Java Using Barcode decoder for Java Control to read, scan read, scan image in Java applications. www.OnBarcode.combr: java.io.BufferedReader = java.io.BufferedReader@2bfa91
Create Code-128 In C#.NET Using Barcode drawer for .NET framework Control to generate, create Code 128 Code Set C image in Visual Studio .NET applications. www.OnBarcode.comGTIN - 13 Encoder In None Using Barcode generation for Online Control to generate, create EAN-13 image in Online applications. www.OnBarcode.comCHAPTER 4 FUN WITH FUNCTIONS, AND NEVER HAVING TO CLOSE THAT JDBC CONNECTION
scala> allStrings(br.readLine) res0: List[String] = List(import scala.xml._, , object Morg {, ) Each time the call-by-name parameter, expr, is accessed, it is applied. If it is passed as a parameter that is also call-by-name, it will be passed without evaluation. In the previous code, we pattern match against the application of expr. If it s null, we return an empty List, a Nil. If it s not null, we return a List that is the current String and the result of allStrings(expr). Call-by-name is a very useful construct. In the next section, we ll use it to build complex control structures. Build Your Own Control Structures
In this section, we ll use call-by-name variables and functions to create our own control structures. Scala has very limited control structures: try/catch/finally, if/else, and while. Most languages have a plethora of control structures including for, foreach, and so on. C# even has the using control structure. using provides an auto-close feature where the parameter passed to using will be closed when using s code block is exited. For example: using (TextReader textReader = new StreamReader(filename)) { return textReader.ReadLine(); } This provides a convenient mechanism to make sure that files, database connections, TCP/IP connections, and so on are closed without having to write a try/finally block for each thing you want to close. Scala does not have a using statement, but we can write one. I ll show you all the code and then step through the pieces: object Control { def using[A <: {def close(): Unit}, B](param: A)(f: A => B): B = try { f(param) } finally { param.close() } import scala.collection.mutable.ListBuffer CHAPTER 4 FUN WITH FUNCTIONS, AND NEVER HAVING TO CLOSE THAT JDBC CONNECTION
def bmap[T](test: => Boolean)(block: => T): List[T] = { val ret = new ListBuffer[T] while(test) ret += block ret.toList } } Let s step through the code. First, we define the Control singleton object: object Control { Next, we define the using method. It takes two type parameters: A and B. The B type parameter is much like what we ve seen in the past: it can be any type. We have put a structural type bound on A. A can be an instance of any class as long as that class has a close method on it. This is called structural typing. Scala allows you to define types based on their structure rather than their class. This is familiar to ECMAScript developers. It s also closer to the duck typing of Ruby and Python, but in the case of Scala, you have to declare the methods of the duck as part of the parameter. The param parameter is one of these A ducks that has a close method. The f parameter is something that takes the A and transforms it to a B. def using[A <: {def close(): Unit}, B](param: A)(f: A => B): B = try { f(param) } finally { param.close() } The code is pretty simple. It wraps the function application in a try/finally block and makes sure that param is closed before the method returns. We could call the code like using(new BufferedReader(otherReader)) { reader => reader.readLine() } The next control statement we ll build is something that loops as long as a test is true. In each iteration, the method will collect the output of a pass-by-name value and append it to the list accumulator. First, let s import ListBuffer so we can accumulate the results:
|
|