c# barcode generator library open source COLLECTIONS AND THE JOY OF IMMUTABILITY in Font

Printing Data Matrix in Font COLLECTIONS AND THE JOY OF IMMUTABILITY

CHAPTER 3 COLLECTIONS AND THE JOY OF IMMUTABILITY
ECC200 Creation In None
Using Barcode creation for Font Control to generate, create ECC200 image in Font applications.
www.OnBarcode.com
QR Code Drawer In None
Using Barcode printer for Font Control to generate, create QR Code image in Font applications.
www.OnBarcode.com
Listing 3-6. Convert a Map to a Person def personFromParams(p: Map[String, String]): Option[Person] = for {name <- p.get("name") ageStr <- p.get("age") age <- toInt(ageStr) validStr <- p.get("valid") valid <- toBool(validStr)} yield new Person(name, age, valid)
Code128 Creation In None
Using Barcode creator for Font Control to generate, create Code 128 Code Set C image in Font applications.
www.OnBarcode.com
UPC-A Encoder In None
Using Barcode drawer for Font Control to generate, create UPC-A Supplement 2 image in Font applications.
www.OnBarcode.com
In my code, any method that can logically fail (e.g., looking something up in a Map, converting a String to an Int) returns an Option. It is up to the calling code to determine what to do. Let s compare this to the Java implementation shown in Listing 3-7.
Create PDF417 In None
Using Barcode generation for Font Control to generate, create PDF417 image in Font applications.
www.OnBarcode.com
Drawing GS1-128 In None
Using Barcode creator for Font Control to generate, create GS1 128 image in Font applications.
www.OnBarcode.com
Listing 3-7. Java Implementation of Convert a Map to a Person public Person personFromParams(Map<String, String> p) { try { if (!p.containsKey("name")) return null; String name = p.get("name"); if (!p.containsKey("age")) return null; String ageStr = p.get("age"); int age = Integer.parseInt(ageStr); if (!p.containsKey("valid")) return null; String validStr = p.get("valid"); bool valid = Boolean.parseBoolean(validStr); return new Person(name, age, valid); } catch (Exception e) { return null; } }
Data Matrix 2d Barcode Encoder In None
Using Barcode generation for Font Control to generate, create DataMatrix image in Font applications.
www.OnBarcode.com
USPS PLANET Barcode Creator In None
Using Barcode creator for Font Control to generate, create USPS Confirm Service Barcode image in Font applications.
www.OnBarcode.com
The Java code has to explicitly test each key. This increases the number of lines of code and reduces the readibility. The Java code has multiple return paths. This makes it more difficult to understand under what conditions the code will continue to flow. At a glance, it s difficult to determine the conditions under which the method will return non-null. Option has some methods to allow chaining. For example, if the parameter name that maps to age used to be called years , we can express the code as ageStr <- p.get("age") orElse p.get("years"). If p.get("age") returns None, p.get("years") will be called. You can chain orElse just like chaining || in the if conditional. The right side of the orElse expression will be evaluated only if the left side is None.
Creating Data Matrix 2d Barcode In Java
Using Barcode generation for Eclipse BIRT Control to generate, create Data Matrix ECC200 image in Eclipse BIRT applications.
www.OnBarcode.com
Data Matrix ECC200 Generator In Visual Studio .NET
Using Barcode printer for ASP.NET Control to generate, create ECC200 image in ASP.NET applications.
www.OnBarcode.com
CHAPTER 3 COLLECTIONS AND THE JOY OF IMMUTABILITY
EAN / UCC - 13 Drawer In Visual Studio .NET
Using Barcode generator for ASP.NET Control to generate, create EAN 128 image in ASP.NET applications.
www.OnBarcode.com
USS Code 128 Encoder In C#.NET
Using Barcode printer for .NET Control to generate, create Code 128C image in .NET framework applications.
www.OnBarcode.com
You can retrieve the contents of an Option with the get method:
Code 128 Code Set B Scanner In None
Using Barcode scanner for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
Encoding Matrix 2D Barcode In Java
Using Barcode creator for Java Control to generate, create Matrix Barcode image in Java applications.
www.OnBarcode.com
scala> Some(3).get
Draw GTIN - 13 In Visual Basic .NET
Using Barcode printer for VS .NET Control to generate, create UPC - 13 image in Visual Studio .NET applications.
www.OnBarcode.com
ECC200 Creator In .NET Framework
Using Barcode printer for ASP.NET Control to generate, create Data Matrix image in ASP.NET applications.
www.OnBarcode.com
res57: Int = 3
Create 2D In .NET Framework
Using Barcode printer for VS .NET Control to generate, create Matrix image in .NET applications.
www.OnBarcode.com
Recognizing Barcode In Visual Basic .NET
Using Barcode Control SDK for .NET framework Control to generate, create, read, scan barcode image in .NET applications.
www.OnBarcode.com
But be careful, because if the Option is None, an exception will be raised:
UPCA Printer In Visual Studio .NET
Using Barcode generator for Reporting Service Control to generate, create UCC - 12 image in Reporting Service applications.
www.OnBarcode.com
UPC-A Supplement 5 Scanner In Java
Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
scala> None.get
java.util.NoSuchElementException: None.get
Like Map, Option has a getOrElse method that returns a default value if the contents are undefined:
scala> Some(3).getOrElse(44)
res59: Int = 3
scala> None.getOrElse(44)
res60: Int = 44
Option is a very useful class for passing or returning values that may or may not be defined. Because Option has map, flatMap, filter, and foreach methods, it can be used in for comprehensions. Option provides a great way to avoid null problems and has methods that work conveniently with other Scala collections.
Wrapping Up List, Tuple, Map, and Option
We ve covered a lot of ground in this section. We ve seen some of Scala s foundation collection classes and the methods that allow for simple and powerful manipulation of these collections. These classes form the basis for much of my development. They also
CHAPTER 3 COLLECTIONS AND THE JOY OF IMMUTABILITY
form the basis for other classes in Scala. For web development, XML is a very important mechanism for data exchange. Scala s immutable collections form the basis for Scala s XML support.
XML Creation and Manipulation
Scala has language-level support for defining XML constants. XML in Scala is immutable, just like Strings in Java. This means you can cache XML without worrying about copying it so it s not modified out from under you. In this section, we ll explore creating XML, parsing XML, and mutating XML. I m covering XML in this chapter because it s an immutable data structure and because Scala treats XML as a collection on Nodes, so it gives us a chance to explore some more of the cool collections stuff we ve been working on.
XML in Your Scala Code
Scala has XML literals built into the language syntax, just like Java has String literals built in. That means you can write
scala> <b>Hello World</b>
res0: scala.xml.Elem = <b>Hello World</b>
You can include attributes:
scala> <b id="greeting">Hello World</b>
res1: scala.xml.Elem = <b id="greeting">Hello World</b>
And the XML can span multiple lines:
scala> <b id="greeting"> <span>Hello</span> World! </b>
CHAPTER 3 COLLECTIONS AND THE JOY OF IMMUTABILITY
res2: scala.xml.Elem = <b id="greeting"> <span>Hello</span> World! </b>
The XML elements can contain prefixes:
scala> <ns:b>Hello World from a namespace</ns:b>
res0: scala.xml.Elem = <ns:b>Hello World from a namespace</ns:b>
You can even have prefixes on the attributes:
Copyright © OnBarcode.com . All rights reserved.