c# barcode generator library open source import scala.collection.mutable.ListBuffer in Font

Print DataMatrix in Font import scala.collection.mutable.ListBuffer

import scala.collection.mutable.ListBuffer
DataMatrix Creation In None
Using Barcode maker for Font Control to generate, create Data Matrix 2d barcode image in Font applications.
www.OnBarcode.com
Encoding ANSI/AIM Code 128 In None
Using Barcode creation for Font Control to generate, create Code-128 image in Font applications.
www.OnBarcode.com
Now we declare the Boolean Map, bmap, method. It takes test as a call-by-name parameter and block, the code block, as a call-by-name parameter. As long as the test results is true, the result of block will be appended to the ret accumulator. Finally, the accumulator is converted to a List and returned.
Barcode Encoder In None
Using Barcode generator for Font Control to generate, create Barcode image in Font applications.
www.OnBarcode.com
QR Code Generation In None
Using Barcode printer for Font Control to generate, create Quick Response Code image in Font applications.
www.OnBarcode.com
CHAPTER 4 FUN WITH FUNCTIONS, AND NEVER HAVING TO CLOSE THAT JDBC CONNECTION
Print ECC200 In None
Using Barcode generator for Font Control to generate, create Data Matrix image in Font applications.
www.OnBarcode.com
UPCA Generator In None
Using Barcode drawer for Font Control to generate, create GS1 - 12 image in Font applications.
www.OnBarcode.com
def bmap[T](test: => Boolean)(block: => T): List[T] = { val ret = new ListBuffer[T] while(test) ret += block ret.toList } }
Create PDF 417 In None
Using Barcode drawer for Font Control to generate, create PDF-417 2d barcode image in Font applications.
www.OnBarcode.com
Making MSI Plessey In None
Using Barcode printer for Font Control to generate, create MSI Plessey image in Font applications.
www.OnBarcode.com
We ve built a couple of control structures. Now let s see how they work in real-world code.
Data Matrix Creation In Java
Using Barcode creator for Java Control to generate, create DataMatrix image in Java applications.
www.OnBarcode.com
Drawing Data Matrix In Objective-C
Using Barcode printer for iPhone Control to generate, create ECC200 image in iPhone applications.
www.OnBarcode.com
JDBC Looping
QR Code Recognizer In Visual Studio .NET
Using Barcode recognizer for Visual Studio .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
Making QR Code 2d Barcode In Objective-C
Using Barcode generator for iPhone Control to generate, create QR Code 2d barcode image in iPhone applications.
www.OnBarcode.com
In this section we ll apply the new control structures, using and bmap, to the real-world problem of performing a JDBC query, collecting the results, and closing the Statement and ResultSet. First, we ll write the code in Java and then see how it gets cleaner and more maintainable in Scala. Here s the Java code:
Print Quick Response Code In Objective-C
Using Barcode generator for iPad Control to generate, create QR Code 2d barcode image in iPad applications.
www.OnBarcode.com
Generate Matrix 2D Barcode In Visual Basic .NET
Using Barcode drawer for .NET framework Control to generate, create Matrix 2D Barcode image in .NET applications.
www.OnBarcode.com
import java.sql.*; import java.util.ArrayList; public class Person { private String name; private int age; private boolean valid; public Person(String n, int a, boolean v) { name = n; age = a; valid = v; } public static ArrayList<Person> findPeople(Connection conn) throws SQLException { Statement st = conn.createStatement(); try { ResultSet rs = st.executeQuery("SELECT * FROM person"); try { ArrayList<Person> ret = new ArrayList<Person>(); while (rs.next()) { ret.add(new Person(rs.getString("name"), rs.getInt("age"), rs.getBoolean("valid"))); }
Encode PDF417 In C#
Using Barcode generator for .NET framework Control to generate, create PDF-417 2d barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Creating Quick Response Code In Java
Using Barcode printer for BIRT reports Control to generate, create QR Code JIS X 0510 image in BIRT reports applications.
www.OnBarcode.com
CHAPTER 4 FUN WITH FUNCTIONS, AND NEVER HAVING TO CLOSE THAT JDBC CONNECTION
Universal Product Code Version A Creation In Visual C#.NET
Using Barcode creator for VS .NET Control to generate, create UPC Code image in Visual Studio .NET applications.
www.OnBarcode.com
Drawing Barcode In VS .NET
Using Barcode drawer for ASP.NET Control to generate, create Barcode image in ASP.NET applications.
www.OnBarcode.com
return ret; } finally { rs.close(); } } finally { st.close(); } } }
Draw Data Matrix In None
Using Barcode generator for Online Control to generate, create DataMatrix image in Online applications.
www.OnBarcode.com
ECC200 Printer In Java
Using Barcode drawer for Java Control to generate, create DataMatrix image in Java applications.
www.OnBarcode.com
The previous code is pretty straightforward. We define a Person class with a constructor. The class has a static method that queries the database for all of the person records. Let s see how the code looks in Scala. First, let s define a Person class. In a single line, we define the class, its constructor, and its fields. It even gets toString, hashCode, and equals methods:
case class Person(name: String, age: Int, valid: Boolean)
Next, let s define a method, findPeople, that will take a JDBC connection and return a List[Person]. The code creates a Statement, executes a query on that Statement, and as long as there are more rows available on the ResultSet, a Person will be created. The method will close the ResultSet and Statement, and it will return the List[Person].
object Person { import Control._ import java.sql._ def findPeople(conn: Connection): List[Person] = using(conn.createStatement){st => using (st.executeQuery("SELECT * FROM person")){rs => bmap(rs.next){ new Person(rs.getString("name"), rs.getInt("age"), rs.getBoolean("valid")) } } } }
With all the boilerplate of try/finally, and so on, the Java code is much longer and more difficult to read. More important, if the developer forgets to write the try/finally block, the ResultSet or Statement may not be closed correctly, causing a hard-to-diagnose issue where the database runs out of resources. While this is something that can be caught in a code review, it s easier to have it built into the control structures. In this example, we ve used the generic control structures, using and bmap, to work with JDBC. We can use the same methods for file IO. In any case, Scala gives you the tools to make your code more concise, more understandable, and easier to maintain.
CHAPTER 4 FUN WITH FUNCTIONS, AND NEVER HAVING TO CLOSE THAT JDBC CONNECTION
Summary
In this chapter, we explored creating and manipulating functions in Scala. In 3, we saw how passing functions to Scala library methods such as List.map and List.filter provided mechanisms for writing clear, understandable code with far less boilerplate than is required in Java code. We explored how to write the same kind of code so that you can abstract away the boilerplate of closing resources and collecting the calculations in loops. In the next chapter, we re going to explore pattern matching, another cornerstone of the functional style of programming. We ll learn how to construct complex logic in a declarative format and how patterns are functions.
Copyright © OnBarcode.com . All rights reserved.