Better Software - November 2007 - (Page 14) Code Craft “A precursor to generic programming, functional programming is especially powerful in constructing succinct, flexible solutions that process sequences of data regardless of type.” def union(s,t): return reduce(lambda sofar, x: sofar + ([] if x in s else [x]), t, s) Listing 5 def intersection(s,t): return reduce(lambda sofar,x: sofar + ([x] if x in s else []), t, []) Listing 6 If there are no functions in the list funs, then compose returns the identity function. Otherwise, it returns a series of nested invocations on the free parameter x—just what we wanted, and all in one line! An example that validates compose is shown in listing 8, which prints the first ten odd numbers by applying 2x+1 to tion as a parameter but return a computed value, not a function. In an ambitious effort to convince you of the power of functional programming (and of Python!), the next example illustrates both. Consider how you would formulate the composition of an arbitrary number of unary functions. Given the list of functions [f,g,h], we want to create a new function that, when given the argument x, computes f(g(h(x)))—but for any number of functions. Since reduce traverses sequences left to right, we’ll need to reverse the list of functions so that h is applied first. The accumulator function should take the current accumulated result as its first parameter, the next function in the list as a second parameter, and apply the latter to the former as the new accumulated result. The return value should itself be a function that takes a single parameter (x above). All the above verbiage can be reduced to the one-line function shown in listing 7. the list [0,1,…,9]. Summary The object paradigm is just one of many that a software developer should have in his arsenal. A precursor to generic programming, functional programming is especially powerful in constructing succinct, flexible solutions that process sequences of data regardless of type. I have shown examples in Python, but many languages support functional programming. Languages—such as C++, Python, and the increasingly popular D—that support multiple styles of programming tend to get you to an acceptable software solution more directly than those that don’t. {end} Chuck Allison developed software for twenty years before becoming a professor of computer science at Utah Valley State College. He was senior editor of the C/C++ Users Journal and is def compose(funs): founding editor of The C++ return lambda x: reduce(lambda z,f: f(z), list(reversed(funs)), x) Source. Chuck is also the author of two C++ books and gives onListing 7 site training in C++, Python, and design patterns. def add1(stuff): return [x+1 for x in stuff] def mult2(stuff): return [x*2 for x in stuff] c = compose([add1, mult2]) print c(range(10)) # [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] Which paradigms do you regularly use? Have you noticed that the language you use influences the solutions you create? M Listing 8 Follow the link on the StickyMinds.com homepage to join the conversation. 14 BETTER SOFTWARE NOVEMBER 2007 www.StickyMinds.com http://www.StickyMinds.com http://www.StickyMinds.com
Table of Contents Feed for the Digital Edition of Better Software - November 2007 Better Software - November 2007 Contents MarkYour Calendar Technically Speaking What’s Happening @StickyMinds.com Code Craft Test Connection Management Chronicles The Measure of a Management System Behind the Scenes A Story About User Stories and Test-Driven Development Product Announcements The Last Word Ad Index Better Software - November 2007 Better Software - November 2007 - (Page Intro) Better Software - November 2007 - Better Software - November 2007 (Page Cover1) Better Software - November 2007 - Better Software - November 2007 (Page Cover2) Better Software - November 2007 - Better Software - November 2007 (Page 1) Better Software - November 2007 - Better Software - November 2007 (Page 2) Better Software - November 2007 - Contents (Page 3) Better Software - November 2007 - MarkYour Calendar (Page 4) Better Software - November 2007 - MarkYour Calendar (Page 5) Better Software - November 2007 - MarkYour Calendar (Page 6) Better Software - November 2007 - Technically Speaking (Page 7) Better Software - November 2007 - Technically Speaking (Page 8) Better Software - November 2007 - What’s Happening @StickyMinds.com (Page 9) Better Software - November 2007 - What’s Happening @StickyMinds.com (Page 10) Better Software - November 2007 - What’s Happening @StickyMinds.com (Page 11) Better Software - November 2007 - Code Craft (Page 12) Better Software - November 2007 - Code Craft (Page 13) Better Software - November 2007 - Code Craft (Page 14) Better Software - November 2007 - Code Craft (Page 15) Better Software - November 2007 - Test Connection (Page 16) Better Software - November 2007 - Test Connection (Page 17) Better Software - November 2007 - Management Chronicles (Page 18) Better Software - November 2007 - Management Chronicles (Page 19) Better Software - November 2007 - The Measure of a Management System (Page 20) Better Software - November 2007 - The Measure of a Management System (Page 21) Better Software - November 2007 - The Measure of a Management System (Page 22) Better Software - November 2007 - The Measure of a Management System (Page 23) Better Software - November 2007 - The Measure of a Management System (Page 24) Better Software - November 2007 - The Measure of a Management System (Page 25) Better Software - November 2007 - The Measure of a Management System (Page 26) Better Software - November 2007 - The Measure of a Management System (Page 27) Better Software - November 2007 - Behind the Scenes (Page 28) Better Software - November 2007 - Behind the Scenes (Page 29) Better Software - November 2007 - Behind the Scenes (Page 30) Better Software - November 2007 - Behind the Scenes (Page 31) Better Software - November 2007 - Behind the Scenes (Page 32) Better Software - November 2007 - Behind the Scenes (Page 33) Better Software - November 2007 - A Story About User Stories and Test-Driven Development (Page 34) Better Software - November 2007 - A Story About User Stories and Test-Driven Development (Page 35) Better Software - November 2007 - A Story About User Stories and Test-Driven Development (Page 36) Better Software - November 2007 - A Story About User Stories and Test-Driven Development (Page 37) Better Software - November 2007 - A Story About User Stories and Test-Driven Development (Page 38) Better Software - November 2007 - A Story About User Stories and Test-Driven Development (Page 39) Better Software - November 2007 - A Story About User Stories and Test-Driven Development (Page 40) Better Software - November 2007 - A Story About User Stories and Test-Driven Development (Page 41) Better Software - November 2007 - A Story About User Stories and Test-Driven Development (Page 42) Better Software - November 2007 - Product Announcements (Page 43) Better Software - November 2007 - Product Announcements (Page 44) Better Software - November 2007 - Product Announcements (Page 45) Better Software - November 2007 - Product Announcements (Page 46) Better Software - November 2007 - The Last Word (Page 47) Better Software - November 2007 - Ad Index (Page 48) Better Software - November 2007 - Ad Index (Page Cover3) Better Software - November 2007 - Ad Index (Page Cover4)
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.