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

Drawer ECC200 in Font COLLECTIONS AND THE JOY OF IMMUTABILITY

CHAPTER 3 COLLECTIONS AND THE JOY OF IMMUTABILITY
Generating Data Matrix 2d Barcode In None
Using Barcode encoder for Font Control to generate, create Data Matrix 2d barcode image in Font applications.
www.OnBarcode.com
Generate GTIN - 13 In None
Using Barcode creation for Font Control to generate, create European Article Number 13 image in Font applications.
www.OnBarcode.com
When I was programming in Java, I never gave constructs like this a second thought. After programming in Ruby and Scala, I find that thinking about what s happening to my data and what s the flow of control in my program at the same time is very challenging. My brain has morphed into thinking about what is business logic for transforming input to output I find this focuses me on the business logic task at hand. As we continue to explore transformations, let s look at other ways to transform a List.
Encoding Code 128 Code Set A In None
Using Barcode maker for Font Control to generate, create USS Code 128 image in Font applications.
www.OnBarcode.com
Barcode Encoder In None
Using Barcode generation for Font Control to generate, create Barcode image in Font applications.
www.OnBarcode.com
Transformation
Encode EAN / UCC - 14 In None
Using Barcode drawer for Font Control to generate, create GS1 128 image in Font applications.
www.OnBarcode.com
Data Matrix ECC200 Generator In None
Using Barcode maker for Font Control to generate, create Data Matrix image in Font applications.
www.OnBarcode.com
The map method on List (and Seq), transforms each element of a collection based on a function. For example, if we have a List[String] and want to convert it to all lowercase:
Barcode Generator In None
Using Barcode printer for Font Control to generate, create Barcode image in Font applications.
www.OnBarcode.com
Painting USPS OneCode Solution Barcode In None
Using Barcode printer for Font Control to generate, create Intelligent Mail image in Font applications.
www.OnBarcode.com
scala> List("A", "Cat").map(s => s.toLowerCase)
DataMatrix Maker In None
Using Barcode printer for Online Control to generate, create Data Matrix ECC200 image in Online applications.
www.OnBarcode.com
Draw Data Matrix ECC200 In None
Using Barcode printer for Office Word Control to generate, create Data Matrix 2d barcode image in Office Word applications.
www.OnBarcode.com
res29: List[java.lang.String] = List(a, cat)
Code39 Reader In .NET Framework
Using Barcode reader for VS .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Barcode Drawer In .NET
Using Barcode drawer for .NET framework Control to generate, create Barcode image in .NET framework applications.
www.OnBarcode.com
We can shorten the function so the code reads:
Barcode Scanner In Java
Using Barcode reader for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Draw UPC-A In Objective-C
Using Barcode maker for iPad Control to generate, create UPC-A Supplement 5 image in iPad applications.
www.OnBarcode.com
scala> List("A", "Cat").map(_.toLowerCase)
DataMatrix Creation In Objective-C
Using Barcode drawer for iPhone Control to generate, create Data Matrix 2d barcode image in iPhone applications.
www.OnBarcode.com
DataMatrix Printer In Java
Using Barcode creator for Android Control to generate, create Data Matrix image in Android applications.
www.OnBarcode.com
res30: List[java.lang.String] = List(a, cat)
Paint Barcode In None
Using Barcode encoder for Software Control to generate, create Barcode image in Software applications.
www.OnBarcode.com
Making EAN13 In None
Using Barcode generator for Online Control to generate, create EAN / UCC - 13 image in Online applications.
www.OnBarcode.com
The number of elements in the returned collection is the same as the number of elements in the original collection, but the types may be different. If the function passed into map returns a different type, then the resulting collection is a collection of the type returned from the function. For example, we can take a List[String] and calculate the length of each String, which will result in a List[Int]:
Paint European Article Number 13 In Objective-C
Using Barcode creation for iPhone Control to generate, create EAN / UCC - 13 image in iPhone applications.
www.OnBarcode.com
GS1 128 Creator In .NET Framework
Using Barcode maker for Reporting Service Control to generate, create GTIN - 128 image in Reporting Service applications.
www.OnBarcode.com
scala> List("A", "Cat").map(_.length)
res31: List[Int] = List(1, 3)
map provides a very powerful and uniform way to transform data from one type to another. We can transform our Strings to lowercase, to a List of their length, and we can
CHAPTER 3 COLLECTIONS AND THE JOY OF IMMUTABILITY
extract data from a collection of complex objects. For example, if we have a database query that returns records of type Person defined as having a first method that returns a String containing the person s first name, we can create a List of the first names of the people in the List:
scala> trait Person {def first: String }
defined trait Person
scala> scala> scala> scala>
val d = val e = val a = List(a,
new Person {def first = "David" } new Person {def first = "Elwood"} new Person {def first = "Archer"} d, e).map(_.first)
res35: List[String] = List(Archer, David, Elwood)
Or, if we re writing a web app, we can create an <li> (an HTML list element) containing the first name of each Person in our List:
scala> List(a,d,e).map(n => <li>{n.first}</li>)
List(<li>Archer</li>, <li>David</li>, <li>Elwood</li>)
List also has a sort method, which takes a function that compares the two parameters. We can sort a List[Int]: scala> List(99, 2, 1, 45).sort(_ < _)
res47: List[Int] = List(1, 2, 45, 99)
CHAPTER 3 COLLECTIONS AND THE JOY OF IMMUTABILITY
We can sort a List[String]:
scala> List("b", "a", "elwood", "archer").sort(_ < _)
res48: List[java.lang.String] = List(a, archer, b, elwood)
And we can sort longest to shortest:
scala> List("b", "a", "elwood", "archer"). sort(_.length > _.length)
res49: List(archer, elwood, a, b)
We can combine the operations. Let s update our Person trait:
trait Person { def age: Int def first: String def valid: Boolean }
Now we can write the code shown in Listing 3-3 to find all the valid Person records, sort by age, and return the first names.
Listing 3-3. First Name of Valid Persons, Sorted by Age def validByAge(in: List[Person]) = in.filter(_.valid). sort(_.age < _.age). map(_.first)
Transformation vs. Mutation
While sometimes you may do complex logic like this in the database, other times you may have a collection of records in memory and you need to perform different transformations on those records. Let s, once again, compare the Scala code in Listing 3-3 to the Java implementation in Listing 3-4.
CHAPTER 3 COLLECTIONS AND THE JOY OF IMMUTABILITY
Listing 3-4. Java Implementation of First Name of Valid Persons, Sorted by Age public ArrayList<String> validByAge(ArrayList<Person> in) { ArrayList<Person> valid = new ArrayList<Person>(); for (Person p: in) { if (p.valid()) valid.add(p); } Person[] people = valid.toArray(new Person[0]); Arrays.sort(people, new Comparator<Person>() { public int compare(Person a, Person b) { return a.age() - b.age(); } } ); ArrayList<String> ret = new ArrayList<String>(); for (Person p: people) { ret.add(p.first()); } return ret; }
Our code expanded from 4 lines to 16 lines (not including blank lines). While we can still discern the intent of the Java code, it requires mentally filtering out the boilerplate loops and looking inside them. You have to look past the how in order to understand the what. On the other hand, even the Scala beginner should be able to read the lines in the Scala code to understand the meaning. We even see a hint of functional programming in the Java code with the construction of Comparator, which consumes four lines of code rather than Scala s one line of code. Interestingly, under the covers, Scala is creating anonymous inner classes for each of the functions and passing them to filter, map, and sort. Just as it makes sense in Java not to have lots of sorting routines floating around code, so there s a set of sort methods (and other helper methods) on Arrays, Scala puts the looping constructs into map, filter, and so on as well so that your code contains more logic and less boilerplate.
Copyright © OnBarcode.com . All rights reserved.