Becoming Functional

Becoming Functional

Joshua Backfield

Language: English

Pages: 152

ISBN: 1449368174

Format: PDF / Kindle (mobi) / ePub

Becoming Functional

Joshua Backfield

Language: English

Pages: 152

ISBN: 1449368174

Format: PDF / Kindle (mobi) / ePub


If you have an imperative (and probably object-oriented) programming background, this hands-on book will guide you through the alien world of functional programming. Author Joshua Backfield begins slowly by showing you how to apply the most useful implementation concepts before taking you further into functional-style concepts and practices.

In each chapter, you’ll learn a functional concept and then use it to refactor the fictional Xxy company’s imperative-style legacy code, writing and testing the functional code yourself. As you progress through the book, you’ll migrate from Java 7 to Groovy and finally to Scala as the need for better functional language support gradually increases.

  • Learn why today’s finely tuned applications work better with functional code
  • Transform imperative-style patterns into functional code, following basic steps
  • Get up to speed with Groovy and Scala through examples
  • Understand how first-class functions are passed and returned from other functions
  • Convert existing methods into pure functions, and loops into recursive methods
  • Change mutable variables into immutable variables
  • Get hands-on experience with statements and nonstrict evaluations
  • Use functional programming alongside object-oriented design

Superintelligence: Paths, Dangers, Strategies

PC Magazin (April 2012)

The Naked Society

Computer Architecture: A Quantitative Approach (5th Edition)

This Book Is Overdue!: How Librarians and Cybrarians Can Save Us All

The Great Accelerator

 

 

 

 

 

 

 

 

 

 

 

 

 

end up with some really bad issues if you have to recurse thousands of times. The main issue with recursing this many times is that you eventually run out of space on the stack. Remember, each function call pushes information back onto the stack. But, of course, there are instances for which we need to iterate thousands of times while keeping track of some state. This is where tail recursion comes in! Tail Recursion Tail recursion is very close to recursion, the difference being that there are

So, strict evaluation means that we will create and evaluate the setting of the variable at the time we define it. This is how we normally think of variables, so let’s go ahead and initialize our enabledContacts member during the creation of the Customer object, as shown in Example 6-3. Example 6-3. All enabled contacts member being set in constructor this.enabledContacts = contacts.findAll { contact -> contact.enabled } Awesome—now we have our enabledContacts member, which can be accessed as

public Contact(Integer contact_id, String firstName, String lastName, String email, Boolean enabled) { this.contact_id = contact_id; this.firstName = firstName; this.lastName = lastName; this.email = email; this.enabled = enabled; } public static List setNameAndEmailForContactAndCustomer( Integer customer_id, Integer contact_id, String name, String email) { Customer.updateContactForCustomerContact( customer_id, contact_id, { contact -> new Contact( contact.contact_id, contact.firstName,

constitutes the most basic components of sending an email. Example 9-8 shows this refactoring. Example 9-8. The Email object extracting the send function with common functionality object Email { def send(to : String, subject : String, body : String) : Boolean = { println("To: " + to + "\nSubject: " + subject + "\nBody: " + body) true } def send(msg : Email) : Boolean = { msg match { case Email(address, subject, body, true, name) => send(address, subject, "Dear " + name + ",\n" + body) case

of objects extending from an abstract object without actually defining every type. We can also more easily implement patterns, such as the Visitor pattern, where Object A accepts Object B, and Object B does some operation on Object A. Let’s assume that we have a class Foo that has an accept method. But we’re not going to accept another class; instead, we just accept the function that performs the visitor work we want to do. The visitor just becomes a simple function that we’re passing to Foo.

Download sample

Download