PART 0Beginnings CHAPTER 1: Primerc01.indd 1c01.indd 1 10/1/2010 3:20:31 PM10/1/2010 3:20:31 PMCOPYRIGHTED MATERIAL
10 x CHAPTER 1 PRIMER return null; }}which works, for now. Unfortunately, although this solves the immediate problem, what happens when
Strategy x 11The type constraint is still necessary, because the Database needs to return “null” in the event that the search fails. But now at least
12 x CHAPTER 1 PRIMERTHE DELEGATE STRATEGYThe whole interface-based Strategy approach can be eliminated in favor of a well-defi ned delegate type
The Delegate Strategy x 13 private List<T> data = new List<T>(); // ... public T[] FindAll(SearchProc<T> algorithm) {
14 x CHAPTER 1 PRIMER public Database() { } public void Add(T i) { data.Add(i); } public IEnumerable<T> Filter(Predicate<T>
The Delegate Strategy x 15 { foreach (T it in data) yield return (transform(it)); }In terms of their purpose, Filter is the e
16 x CHAPTER 1 PRIMER { return acc + 1;
Lambda Calculus (Briefl y) x 17As can be surmised from the preceding code, the LINQ Aggregate extension method is the moral equivalent of the Reduce w
18 x CHAPTER 1 PRIMER { return op(l, r); } static void MathExamples() { int x = 2; int y = 3; // using ex
Lambda Calculus (Briefl y) x 19 get { return m_majorApproval; } }}class Class{ // ... public bool Assign(Student s) { if (s.
c01.indd 2c01.indd 2 10/1/2010 3:20:34 PM10/1/2010 3:20:34 PM
20 x CHAPTER 1 PRIMERIn the lambda calculus, this operation can be thought of as asking a function to take an int and another function, where tha
Lambda Calculus (Briefl y) x 21 static Op<T1, Op<T2, U>> Curry<T1, T2, U>(Op<T1, T2, U> fn) { return delegate(T1
22 x CHAPTER 1 PRIMERwritten in small chunks, even as small as simple operations and then composed together, such as what might be needed for inp
Type Inference x 23C# 3.0 provides some of this, via the automatic property declaration — it assumes the task of creat-ing a fi eld and the get/set lo
24 x CHAPTER 1 PRIMEROf course, types need not be the only thing inferred by the compiler; because public is the most common access modifi er for
Type Inference x 25Unfortunately, the syntax is getting tricky to parse, particularly if the constructor body is now implicitly “inside” the class. I
26 x CHAPTER 1 PRIMEROn top of all this, the language can start to make the explicit “generic” declarations of the earlier C# operations less nec
Expressions, Not Statements x 27Of course, now that the situation has reversed itself, if the programmer does want the ability to modify the internal
28 x CHAPTER 1 PRIMERIf every (or most every) language construct is an expression, it means the language takes a more input-yields-output style t
1 PrimerWHAT’S IN THIS CHAPTER? ‰Understanding strategies ‰Reviewing Lambda calculus ‰Infering types ‰Understanding mutability ‰Creating your own bind
4 x CHAPTER 1 PRIMERand these types are frequently collected together in a large bundle known as a framework or class library. And despite the re
Setup x 5programming techniques available to the developer using off-the-shelf tools and technology, but as the problems surmount, so will the desire
6 x CHAPTER 1 PRIMER { get { return m_major; } set { m_major = value; } }}class Instructor : Person{ private string m_dept
It’s That Time of Year Again… x 7 static Program() { Instructors.Add(new Instructor(“Al”, “Scherer”, 38, “Compute
8 x CHAPTER 1 PRIMER if (s.FirstName == first && s.LastName == last) { // ... Do something
It’s That Time of Year Again… x 9 public Student Find(string first, string last) { foreach (Student i in data) { if (i
Comments to this Manuals