c# barcode generator library open source scala> val x = List(1,2,3) scala> 99 :: x in Font

Generation DataMatrix in Font scala> val x = List(1,2,3) scala> 99 :: x

scala> val x = List(1,2,3) scala> 99 :: x
Data Matrix Creation In None
Using Barcode creator for Font Control to generate, create DataMatrix image in Font applications.
www.OnBarcode.com
Paint PDF417 In None
Using Barcode maker for Font Control to generate, create PDF-417 2d barcode image in Font applications.
www.OnBarcode.com
res0: List[Int] = List(99, 1, 2, 3)
Code 39 Full ASCII Drawer In None
Using Barcode drawer for Font Control to generate, create ANSI/AIM Code 39 image in Font applications.
www.OnBarcode.com
Quick Response Code Printer In None
Using Barcode printer for Font Control to generate, create Denso QR Bar Code image in Font applications.
www.OnBarcode.com
Note that the list referred to by the variable x is unchanged, but a new List is created with a new head and the old tail. This is a very fast, constant-time, O(1), operation.
USS Code 128 Creator In None
Using Barcode generation for Font Control to generate, create Code 128 Code Set B image in Font applications.
www.OnBarcode.com
GTIN - 128 Creation In None
Using Barcode generation for Font Control to generate, create GTIN - 128 image in Font applications.
www.OnBarcode.com
CHAPTER 3 COLLECTIONS AND THE JOY OF IMMUTABILITY
European Article Number 13 Drawer In None
Using Barcode creation for Font Control to generate, create EAN-13 image in Font applications.
www.OnBarcode.com
Delivery Point Barcode (DPBC) Creation In None
Using Barcode maker for Font Control to generate, create Postnet image in Font applications.
www.OnBarcode.com
You can also merge two lists to form a new List. This operation is O(n) where n is the number of elements in the first List:
DataMatrix Drawer In .NET Framework
Using Barcode creator for .NET Control to generate, create ECC200 image in .NET applications.
www.OnBarcode.com
Decode ECC200 In Visual C#.NET
Using Barcode recognizer for Visual Studio .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
scala> val x = List(1,2,3) scala> val y = List(99, 98, 97) scala> x ::: y
Making UPC Code In .NET
Using Barcode generation for Reporting Service Control to generate, create GS1 - 12 image in Reporting Service applications.
www.OnBarcode.com
Recognizing EAN 13 In .NET
Using Barcode reader for .NET framework Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
res3: List[Int] = List(1, 2, 3, 99, 98, 97)
EAN / UCC - 14 Drawer In None
Using Barcode creation for Online Control to generate, create UCC.EAN - 128 image in Online applications.
www.OnBarcode.com
Barcode Decoder In Java
Using Barcode Control SDK for Eclipse BIRT Control to generate, create, read, scan barcode image in Eclipse BIRT applications.
www.OnBarcode.com
Getting Functional
Encoding UPC-A In None
Using Barcode creation for Online Control to generate, create GTIN - 12 image in Online applications.
www.OnBarcode.com
Printing Barcode In Objective-C
Using Barcode creator for iPad Control to generate, create Barcode image in iPad applications.
www.OnBarcode.com
The power of List and other collections in Scala come when you mix functions with the collection operators. Let s say we want to find all the odd numbers in a List. It s easy:
EAN / UCC - 14 Creation In Visual Basic .NET
Using Barcode maker for .NET Control to generate, create EAN / UCC - 14 image in .NET applications.
www.OnBarcode.com
Scanning UPC Symbol In None
Using Barcode scanner for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
scala> List(1,2,3).filter(x => x % 2 == 1)
Barcode Decoder In Java
Using Barcode Control SDK for Java Control to generate, create, read, scan barcode image in Java applications.
www.OnBarcode.com
Read Barcode In None
Using Barcode decoder for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
res4: List[Int] = List(1, 3)
The filter method iterates over the collection and applies the function, in this case, an anonymous function, to each of the elements. If the function returns true, the element is included in the resulting collection. If the function returns false, the element is not included in the resulting collection. The resulting collection is the same type of collection that filter was invoked on. If you invoke filter on a List[Int], you get a List[Int]. If you invoke filter on an Array[String], you get an Array[String] back. In this case, we ve written a function that performs mod 2 on the parameter and tests to see whether the result is 1, which indicates that the parameter is odd. There s a corresponding remove method, which removes elements that match the test function:
scala> List(1,2,3).remove(x => x % 2 == 1)
res5: List[Int] = List(2)
We can also write a method called isOdd and pass the isOdd method as a parameter (Scala will promote the method to a function):
scala> def isOdd(x: Int) = x % 2 == 1
CHAPTER 3 COLLECTIONS AND THE JOY OF IMMUTABILITY
isOdd: (Int)Boolean
scala> List(1,2,3,4,5).filter(isOdd)
res6: List[Int] = List(1, 3, 5)
filter works with any collections that contain any type. For example: scala> "99 Red Balloons".toList.filter(Character.isDigit)
res9: List[Char] = List(9, 9)
In this case, we re converting a String to a List[Char] using the toList method and filtering the numbers. The Scala compiler promotes the isDigit static method on Character to a function, thus demonstrating interoperability with Java and that Scala methods are not magic. Another useful method for picking the right elements out of a List is takeWhile, which returns all the elements until it encounters an element that causes the function to return false. For example, let s get all the characters up to the first space in a String:
scala> "Elwood eats mice".takeWhile(c => c != ' ')
res12: Seq[Char] = ArrayBuffer(E, l, w, o, o, d)
Contrast with Java
I grew up writing machine code and later assembly language. When I wrote this code, I was telling the machine exactly what to do: load this register with this value, test the value, branch if some condition was met, and so on. I directed the steps that the CPU took in order to perform my task. Contrast this with writing formula functions in Excel. In Excel, we describe how to solve some problem using the formula functions and cell addresses, and it s up to Excel to determine what cells need to be recalculated and the order for the recalculation.
CHAPTER 3 COLLECTIONS AND THE JOY OF IMMUTABILITY
Directing the machine s steps is termed imperative coding. Imperative coding describes, as we saw earlier, the how. Writing functions describing the what the goal to be achieved and allowing the computer to figure out the how is termed functional programming. Scala allows you to express code in a way that s further toward the functional end of the coding spectrum. Let s do a little exploring of the differences between the two. Let s compare filter in Scala to the Java implementation shown in Listing 3-1.
Listing 3-1. Java Implementation of Odd Filtering int[] x = {1,2,3,4,5}; ArrayList<Integer> res = new ArrayList<Integer>(); for (int v : x) { if (v % 2 == 1) res.add(new Integer(v)); }
In this code, the logic gets lost in the boilerplate. There are two pieces of logic that we are concerned with: what operation is being performed and the formula for that logic. When you first write the previous code, you know what the intent is. If you didn t comment your code with // filtering the odd array elements, you, or someone who picks up the code in a year or two, will have to puzzle about the meaning of the loop. The filter logic is buried in the middle of the code. Compare the Java code to the line of Scala code: filter(v => v % 2 == 1), where the very essence of the operation appears in the code and nothing else. As the complexity of your code increases and the time between writing and maintaining a particular module increases, removing boilerplate while maintaining visible business logic makes code easier to maintain and decreases defects. Let s look at the takeWhile Java translation in Listing 3-2.
Listing 3-2. Java Implementation of Take Until First Space char[] x = "Elwood Eats Mice".toCharArray(); ArrayList<Character> res = new ArrayList<Character>(); for (char c : x) { if (c != ' ') res.add(new Character(c)); else break; }
Once again, one line in Scala expresses what takes many lines in Java. This example also demonstrates the mental shift that is common in imperative code. In one line in the loop, we mutate or change the res variable. In the next line, in the else, we have the break flow of control statement. Your brain has to think about two distinct concepts: what variable is being mutated and what s the next statement the program is going to execute all rolled into two lines.
Copyright © OnBarcode.com . All rights reserved.