├── Chapter2 ├── .DS_Store ├── AnonymousFunctions │ ├── .DS_Store │ ├── Lambda │ │ ├── .DS_Store │ │ └── Customer.java │ └── Closures │ │ └── Closure.java ├── Introduction │ └── Customer.java └── FunctionsAsObjects │ └── Customer.java ├── Chapter3 ├── .DS_Store ├── SideEffects │ ├── Foreach1.java │ ├── Function1.java │ ├── .DS_Store │ ├── FunctionalConcepts.java │ ├── Contract.java │ └── Customer.java ├── OutputDependsOnInput │ ├── Foreach1.java │ ├── Function1.java │ ├── .DS_Store │ ├── Contract.java │ └── Customer.java └── Conclusion │ └── StartingAMovement │ └── Customer.java ├── Chapter10 ├── NewDesignPatterns │ ├── Option.java │ ├── None.java │ ├── Some.java │ └── Temp.java └── PuttingItAllTogether │ ├── CommandLineOption.scala │ ├── CommandLine.scala │ ├── fDB.scala │ ├── Record.scala │ ├── Database.scala │ └── Table.scala ├── Chapter6 ├── LazyEvaluation │ ├── Temp1.groovy │ ├── Temp4.groovy │ ├── Temp2.groovy │ ├── Temp3.groovy │ └── Temp5.groovy └── LazinessCanCreateProblems │ ├── Example2.groovy │ ├── Example1.groovy │ ├── Example1.scala │ └── Example3.groovy ├── Chapter7 ├── Introduction │ ├── Test.java │ └── Test2.java └── TakingThePlunge │ ├── Contract.scala │ ├── Contact.scala │ ├── Contract.java │ ├── Contact.java │ ├── Customer.scala │ └── Customer.java ├── README.md ├── Chapter9 ├── StaticEncapsulation │ ├── Contact.scala │ └── Email.scala ├── ObjectsAsContainers │ ├── Contact.scala │ └── Email.scala └── CodeAsData │ ├── Contract.scala │ ├── Email.scala │ ├── CommandLine.scala │ ├── Contact.scala │ └── Customer.scala ├── Chapter4 ├── Mutability │ └── Contact.java └── Immutability │ ├── Contract.java │ └── Customer.java └── Chapter8 ├── Conclusion ├── Contract.scala ├── Contact.scala └── Customer.scala ├── ExtractingLists ├── Contract.scala ├── Contact.scala └── Customer.scala ├── SimpleMatches ├── Contract.scala ├── Contact.scala └── Customer.scala ├── ExtractingObjects ├── Contract.scala ├── Contact.scala └── Customer.scala └── SimplePatternExtraction ├── Contract.scala ├── Contact.scala └── Customer.scala /Chapter2/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jbackfield/BecomingFunctional/HEAD/Chapter2/.DS_Store -------------------------------------------------------------------------------- /Chapter3/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jbackfield/BecomingFunctional/HEAD/Chapter3/.DS_Store -------------------------------------------------------------------------------- /Chapter3/SideEffects/Foreach1.java: -------------------------------------------------------------------------------- 1 | public interface Foreach1 { 2 | public void call(A1 in1); 3 | } -------------------------------------------------------------------------------- /Chapter3/OutputDependsOnInput/Foreach1.java: -------------------------------------------------------------------------------- 1 | public interface Foreach1 { 2 | public void call(A1 in1); 3 | } -------------------------------------------------------------------------------- /Chapter3/SideEffects/Function1.java: -------------------------------------------------------------------------------- 1 | public interface Function1 { 2 | public B call(A1 in1); 3 | } 4 | -------------------------------------------------------------------------------- /Chapter10/NewDesignPatterns/Option.java: -------------------------------------------------------------------------------- 1 | public interface Option { 2 | 3 | public T getOrElse(T defObj); 4 | 5 | } -------------------------------------------------------------------------------- /Chapter3/OutputDependsOnInput/Function1.java: -------------------------------------------------------------------------------- 1 | public interface Function1 { 2 | public B call(A1 in1); 3 | } 4 | -------------------------------------------------------------------------------- /Chapter3/SideEffects/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jbackfield/BecomingFunctional/HEAD/Chapter3/SideEffects/.DS_Store -------------------------------------------------------------------------------- /Chapter10/PuttingItAllTogether/CommandLineOption.scala: -------------------------------------------------------------------------------- 1 | class CommandLineOption(val name : String, val exec : Database => Database) -------------------------------------------------------------------------------- /Chapter2/AnonymousFunctions/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jbackfield/BecomingFunctional/HEAD/Chapter2/AnonymousFunctions/.DS_Store -------------------------------------------------------------------------------- /Chapter3/OutputDependsOnInput/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jbackfield/BecomingFunctional/HEAD/Chapter3/OutputDependsOnInput/.DS_Store -------------------------------------------------------------------------------- /Chapter2/AnonymousFunctions/Lambda/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jbackfield/BecomingFunctional/HEAD/Chapter2/AnonymousFunctions/Lambda/.DS_Store -------------------------------------------------------------------------------- /Chapter10/NewDesignPatterns/None.java: -------------------------------------------------------------------------------- 1 | public class None implements Option { 2 | 3 | public T getOrElse(T defObj) { 4 | return obj; 5 | } 6 | 7 | } -------------------------------------------------------------------------------- /Chapter6/LazyEvaluation/Temp1.groovy: -------------------------------------------------------------------------------- 1 | 2 | class TestClass { 3 | 4 | def all = [1,2,3,4,5,6] 5 | def odd = all.findAll { num -> num%2 == 1 } 6 | 7 | } 8 | 9 | println(new TestClass().odd) 10 | -------------------------------------------------------------------------------- /Chapter7/Introduction/Test.java: -------------------------------------------------------------------------------- 1 | 2 | public class Test { 3 | 4 | public static void main(String[] args) { 5 | Integer x = 0; 6 | System.out.println("X is " + (x = 1).toString()); 7 | } 8 | 9 | } 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | BecomingFunctional 2 | ================== 3 | 4 | Source Code for my book Becoming Functional. Some of the sections have been renamed and so I'll be going through and changing the directory names to be correct. 5 | -------------------------------------------------------------------------------- /Chapter7/Introduction/Test2.java: -------------------------------------------------------------------------------- 1 | 2 | public class Test { 3 | 4 | public static void main(String[] args) { 5 | Integer x = 1; 6 | System.out.println("X is: " + ((x > 0) ? "positive" : "negative")); 7 | } 8 | 9 | } 10 | -------------------------------------------------------------------------------- /Chapter6/LazyEvaluation/Temp4.groovy: -------------------------------------------------------------------------------- 1 | 2 | class TestClass { 3 | 4 | def all = [1,2,3,4,5,6] 5 | @Lazy def odd = all.findAll { num -> num%2 == 1 } 6 | 7 | } 8 | 9 | def tc = new TestClass() 10 | 11 | tc.all = [1,2,3] 12 | 13 | println(tc.odd) 14 | -------------------------------------------------------------------------------- /Chapter6/LazyEvaluation/Temp2.groovy: -------------------------------------------------------------------------------- 1 | 2 | class TestClass { 3 | 4 | def all = [1,2,3,4,5,6] 5 | def odd = all.findAll { num -> println("Foo"); num%2 == 1; } 6 | 7 | } 8 | 9 | def tc = new TestClass() 10 | 11 | println("Bar") 12 | 13 | println(tc.odd) 14 | -------------------------------------------------------------------------------- /Chapter6/LazyEvaluation/Temp3.groovy: -------------------------------------------------------------------------------- 1 | 2 | class TestClass { 3 | 4 | def all = [1,2,3,4,5,6] 5 | @Lazy def odd = all.findAll { num -> println("Foo"); num%2 == 1; } 6 | 7 | } 8 | 9 | def tc = new TestClass() 10 | 11 | println("Bar") 12 | 13 | println(tc.odd) 14 | -------------------------------------------------------------------------------- /Chapter6/LazyEvaluation/Temp5.groovy: -------------------------------------------------------------------------------- 1 | 2 | class TestClass { 3 | 4 | def all = [1,2,3,4,5,6] 5 | @Lazy def odd = all.findAll { num -> num%2 == 1 } 6 | 7 | } 8 | 9 | def tc = new TestClass() 10 | 11 | println(tc.odd) 12 | 13 | tc.all = [1,2,3] 14 | 15 | println(tc.odd) 16 | -------------------------------------------------------------------------------- /Chapter10/NewDesignPatterns/Some.java: -------------------------------------------------------------------------------- 1 | public class Some implements Option { 2 | 3 | private final T obj; 4 | 5 | public Some(T obj) { 6 | this.obj = obj; 7 | } 8 | 9 | public T getOrElse(T defObj) { 10 | return (this.obj == null) ? defObj : this.obj; 11 | } 12 | 13 | } -------------------------------------------------------------------------------- /Chapter10/NewDesignPatterns/Temp.java: -------------------------------------------------------------------------------- 1 | 2 | public class Temp { 3 | 4 | public static void test(Option foo) { 5 | System.out.println(foo.getOrElse("die")); 6 | } 7 | 8 | public static void main(String[] args) { 9 | test(new None()); 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /Chapter9/StaticEncapsulation/Contact.scala: -------------------------------------------------------------------------------- 1 | object Contact { 2 | } 3 | 4 | class Contact(val contact_id : Integer, 5 | val firstName : String, 6 | val lastName : String, 7 | val email : String, 8 | val enabled : Boolean) { 9 | 10 | def sendEmail() = { 11 | new Email(email, "My Subject", "My Body").send() 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /Chapter9/StaticEncapsulation/Email.scala: -------------------------------------------------------------------------------- 1 | object Email { 2 | 3 | def send(msg : Email) : Boolean = { 4 | println("To: " + msg.address + "\nSubject: " + msg.subject + "\nBody: " + msg.body) 5 | true 6 | } 7 | 8 | } 9 | 10 | case class Email(val address : String, 11 | val subject : String, 12 | val body : String) { 13 | 14 | def send() : Boolean = Email.send(this) 15 | 16 | } -------------------------------------------------------------------------------- /Chapter9/ObjectsAsContainers/Contact.scala: -------------------------------------------------------------------------------- 1 | object Contact { 2 | } 3 | 4 | class Contact(val contact_id : Integer, 5 | val firstName : String, 6 | val lastName : String, 7 | val email : String, 8 | val enabled : Boolean) { 9 | 10 | def sendEmail() = { 11 | new Email(this.email, "My Subject", "My Body", true, this.firstName).send() 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /Chapter2/AnonymousFunctions/Closures/Closure.java: -------------------------------------------------------------------------------- 1 | 2 | public class Closure { 3 | 4 | public String foo = ""; 5 | 6 | public static Closure process(final Closure t) { 7 | System.out.println(t.toString() + " = " + t.foo); 8 | t.foo = "bar"; 9 | new Runnable() { 10 | public void run() { 11 | System.out.println(t.toString() + " = " + t.foo); 12 | t.foo = "baz"; 13 | } 14 | }.run(); 15 | System.out.println(t.toString() + " = " + t.foo); 16 | return t; 17 | } 18 | 19 | public static void main(String[] args) { 20 | process(new Closure()); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Chapter4/Mutability/Contact.java: -------------------------------------------------------------------------------- 1 | public class Contact { 2 | 3 | public Integer contact_id = 0; 4 | public String firstName = ""; 5 | public String lastName = ""; 6 | public String email = ""; 7 | public Boolean enabled = true; 8 | 9 | public Contact(Integer contact_id, 10 | String firstName, 11 | String lastName, 12 | String email, 13 | Boolean enabled) { 14 | this.contact_id = contact_id; 15 | this.firstName = firstName; 16 | this.lastName = lastName; 17 | this.email = email; 18 | this.enabled = enabled; 19 | } 20 | } -------------------------------------------------------------------------------- /Chapter10/PuttingItAllTogether/CommandLine.scala: -------------------------------------------------------------------------------- 1 | object CommandLine { 2 | 3 | def wrapOutput(wrapper : String, output : String) : Unit = { 4 | println(wrapper) 5 | print(output) 6 | println(wrapper) 7 | } 8 | 9 | def optionPrompt(options : Map[String, CommandLineOption]) : 10 | Option[CommandLineOption] = { 11 | println() 12 | println("----[Options]----") 13 | options.foreach(option => println(option._1 + ") " + option._2.name)) 14 | options.get(prompt("Action").toLowerCase) 15 | } 16 | 17 | def prompt(msg : String) : String = { 18 | print(msg + ": ") 19 | readLine() 20 | } 21 | 22 | } -------------------------------------------------------------------------------- /Chapter4/Immutability/Contract.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | import java.util.Calendar; 3 | import java.util.concurrent.ThreadPoolExecutor; 4 | import java.util.concurrent.TimeUnit; 5 | import java.util.concurrent.LinkedBlockingQueue; 6 | 7 | public class Contract { 8 | 9 | public final Calendar begin_date; 10 | public final Calendar end_date; 11 | public final Boolean enabled = true; 12 | 13 | public Contract(Calendar begin_date, Boolean enabled) { 14 | this.begin_date = begin_date; 15 | this.end_date = this.begin_date.getInstance(); 16 | this.end_date.setTimeInMillis(this.begin_date.getTimeInMillis()); 17 | this.end_date.add(Calendar.YEAR, 2); 18 | this.enabled = enabled; 19 | } 20 | 21 | } -------------------------------------------------------------------------------- /Chapter3/OutputDependsOnInput/Contract.java: -------------------------------------------------------------------------------- 1 | import java.util.Calendar; 2 | 3 | public class Contract { 4 | 5 | public Calendar begin_date; 6 | public Calendar end_date; 7 | public Boolean enabled = true; 8 | 9 | public Contract(Calendar begin_date) { 10 | this.begin_date = begin_date; 11 | this.end_date = this.begin_date.getInstance(); 12 | this.end_date.setTimeInMillis(this.begin_date.getTimeInMillis()); 13 | this.end_date.add(Calendar.YEAR, 2); 14 | } 15 | 16 | public static void setContractDisabledForCustomer(Integer customer_id) { 17 | for(Customer customer : Customer.allCustomers) { 18 | if(customer.id == customer_id) { 19 | customer.contract.enabled = false; 20 | } 21 | } 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Chapter8/Conclusion/Contract.scala: -------------------------------------------------------------------------------- 1 | import java.util.Calendar 2 | 3 | object Contract { 4 | 5 | def setContractForCustomerList(ids : List[Integer], 6 | status : Boolean) : List[Customer] = { 7 | Customer.updateContractForCustomerList(ids, { contract => 8 | new Contract(contract.begin_date, contract.end_date, status) 9 | }) 10 | } 11 | 12 | } 13 | 14 | class Contract(val begin_date : Calendar, 15 | val end_date : Calendar, 16 | val enabled : Boolean) { 17 | 18 | def this(begin_date : Calendar, enabled : Boolean) = this(begin_date, { 19 | val c = Calendar.getInstance() 20 | c.setTimeInMillis(begin_date.getTimeInMillis) 21 | c.add(Calendar.YEAR, 2) 22 | c 23 | }, enabled) 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Chapter9/CodeAsData/Contract.scala: -------------------------------------------------------------------------------- 1 | import java.util.Calendar 2 | 3 | object Contract { 4 | 5 | def setContractForCustomerList(ids : List[Integer], 6 | status : Boolean) : List[Customer] = { 7 | Customer.updateContractForCustomerList(ids, { contract => 8 | new Contract(contract.begin_date, contract.end_date, status) 9 | }) 10 | } 11 | 12 | } 13 | 14 | class Contract(val begin_date : Calendar, 15 | val end_date : Calendar, 16 | val enabled : Boolean) { 17 | 18 | def this(begin_date : Calendar, enabled : Boolean) = this(begin_date, { 19 | val c = Calendar.getInstance() 20 | c.setTimeInMillis(begin_date.getTimeInMillis) 21 | c.add(Calendar.YEAR, 2) 22 | c 23 | }, enabled) 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Chapter7/TakingThePlunge/Contract.scala: -------------------------------------------------------------------------------- 1 | import java.util.Calendar 2 | 3 | object Contract { 4 | 5 | def setContractForCustomerList(ids : List[Integer], 6 | status : Boolean) : List[Customer] = { 7 | Customer.updateContractForCustomerList(ids, { contract => 8 | new Contract(contract.begin_date, contract.end_date, status) 9 | }) 10 | } 11 | 12 | } 13 | 14 | class Contract(val begin_date : Calendar, 15 | val end_date : Calendar, 16 | val enabled : Boolean) { 17 | 18 | def this(begin_date : Calendar, enabled : Boolean) = this(begin_date, { 19 | val c = Calendar.getInstance() 20 | c.setTimeInMillis(begin_date.getTimeInMillis) 21 | c.add(Calendar.YEAR, 2) 22 | c 23 | }, enabled) 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Chapter8/ExtractingLists/Contract.scala: -------------------------------------------------------------------------------- 1 | import java.util.Calendar 2 | 3 | object Contract { 4 | 5 | def setContractForCustomerList(ids : List[Integer], 6 | status : Boolean) : List[Customer] = { 7 | Customer.updateContractForCustomerList(ids, { contract => 8 | new Contract(contract.begin_date, contract.end_date, status) 9 | }) 10 | } 11 | 12 | } 13 | 14 | class Contract(val begin_date : Calendar, 15 | val end_date : Calendar, 16 | val enabled : Boolean) { 17 | 18 | def this(begin_date : Calendar, enabled : Boolean) = this(begin_date, { 19 | val c = Calendar.getInstance() 20 | c.setTimeInMillis(begin_date.getTimeInMillis) 21 | c.add(Calendar.YEAR, 2) 22 | c 23 | }, enabled) 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Chapter8/SimpleMatches/Contract.scala: -------------------------------------------------------------------------------- 1 | import java.util.Calendar 2 | 3 | object Contract { 4 | 5 | def setContractForCustomerList(ids : List[Integer], 6 | status : Boolean) : List[Customer] = { 7 | Customer.updateContractForCustomerList(ids, { contract => 8 | new Contract(contract.begin_date, contract.end_date, status) 9 | }) 10 | } 11 | 12 | } 13 | 14 | class Contract(val begin_date : Calendar, 15 | val end_date : Calendar, 16 | val enabled : Boolean) { 17 | 18 | def this(begin_date : Calendar, enabled : Boolean) = this(begin_date, { 19 | val c = Calendar.getInstance() 20 | c.setTimeInMillis(begin_date.getTimeInMillis) 21 | c.add(Calendar.YEAR, 2) 22 | c 23 | }, enabled) 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Chapter9/CodeAsData/Email.scala: -------------------------------------------------------------------------------- 1 | object Email { 2 | 3 | def send(to : String, subject : String, body : String) : Boolean = { 4 | println("To: " + to + "\nSubject: " + subject + "\nBody: " + body) 5 | true 6 | } 7 | 8 | def send(msg : Email) : Boolean = { 9 | msg match { 10 | case Email(address, subject, body, true, name) => 11 | send(address, subject, "Dear " + name + ",\n" + body) 12 | case Email(address, subject, body, _, _) => 13 | send(address, subject, body) 14 | } 15 | true 16 | } 17 | } 18 | 19 | case class Email(val address : String, 20 | val subject : String, 21 | val body : String, 22 | val isDearReader : Boolean, 23 | val name : String) { 24 | 25 | def send() : Boolean = Email.send(this) 26 | 27 | } -------------------------------------------------------------------------------- /Chapter8/ExtractingObjects/Contract.scala: -------------------------------------------------------------------------------- 1 | import java.util.Calendar 2 | 3 | object Contract { 4 | 5 | def setContractForCustomerList(ids : List[Integer], 6 | status : Boolean) : List[Customer] = { 7 | Customer.updateContractForCustomerList(ids, { contract => 8 | new Contract(contract.begin_date, contract.end_date, status) 9 | }) 10 | } 11 | 12 | } 13 | 14 | class Contract(val begin_date : Calendar, 15 | val end_date : Calendar, 16 | val enabled : Boolean) { 17 | 18 | def this(begin_date : Calendar, enabled : Boolean) = this(begin_date, { 19 | val c = Calendar.getInstance() 20 | c.setTimeInMillis(begin_date.getTimeInMillis) 21 | c.add(Calendar.YEAR, 2) 22 | c 23 | }, enabled) 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Chapter8/SimplePatternExtraction/Contract.scala: -------------------------------------------------------------------------------- 1 | import java.util.Calendar 2 | 3 | object Contract { 4 | 5 | def setContractForCustomerList(ids : List[Integer], 6 | status : Boolean) : List[Customer] = { 7 | Customer.updateContractForCustomerList(ids, { contract => 8 | new Contract(contract.begin_date, contract.end_date, status) 9 | }) 10 | } 11 | 12 | } 13 | 14 | class Contract(val begin_date : Calendar, 15 | val end_date : Calendar, 16 | val enabled : Boolean) { 17 | 18 | def this(begin_date : Calendar, enabled : Boolean) = this(begin_date, { 19 | val c = Calendar.getInstance() 20 | c.setTimeInMillis(begin_date.getTimeInMillis) 21 | c.add(Calendar.YEAR, 2) 22 | c 23 | }, enabled) 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Chapter9/ObjectsAsContainers/Email.scala: -------------------------------------------------------------------------------- 1 | object Email { 2 | 3 | def send(to : String, subject : String, body : String) : Boolean = { 4 | println("To: " + to + "\nSubject: " + subject + "\nBody: " + body) 5 | true 6 | } 7 | 8 | def send(msg : Email) : Boolean = { 9 | msg match { 10 | case Email(address, subject, body, true, name) => 11 | send(address, subject, "Dear " + name + ",\n" + body) 12 | case Email(address, subject, body, _, _) => 13 | send(address, subject, body) 14 | } 15 | true 16 | } 17 | } 18 | 19 | case class Email(val address : String, 20 | val subject : String, 21 | val body : String, 22 | val isDearReader : Boolean, 23 | val name : String) { 24 | 25 | def send() : Boolean = Email.send(this) 26 | 27 | } -------------------------------------------------------------------------------- /Chapter8/Conclusion/Contact.scala: -------------------------------------------------------------------------------- 1 | object Contact { 2 | 3 | def setNameAndEmailForContactAndCustomer( 4 | customer_id : Integer, 5 | contact_id : Integer, 6 | name : String, 7 | email : String) : List[Customer] = { 8 | Customer.updateContactForCustomerContact( 9 | customer_id, 10 | contact_id, 11 | { contact => 12 | new Contact( 13 | contact.contact_id, 14 | contact.firstName, 15 | name, 16 | email, 17 | contact.enabled 18 | ) 19 | } 20 | ) 21 | } 22 | } 23 | 24 | class Contact(val contact_id : Integer, 25 | val firstName : String, 26 | val lastName : String, 27 | val email : String, 28 | val enabled : Boolean) { 29 | 30 | def sendEmail() = { 31 | println("Sending Email") 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Chapter7/TakingThePlunge/Contact.scala: -------------------------------------------------------------------------------- 1 | object Contact { 2 | 3 | def setNameAndEmailForContactAndCustomer( 4 | customer_id : Integer, 5 | contact_id : Integer, 6 | name : String, 7 | email : String) : List[Customer] = { 8 | Customer.updateContactForCustomerContact( 9 | customer_id, 10 | contact_id, 11 | { contact => 12 | new Contact( 13 | contact.contact_id, 14 | contact.firstName, 15 | name, 16 | email, 17 | contact.enabled 18 | ) 19 | } 20 | ) 21 | } 22 | } 23 | 24 | class Contact(val contact_id : Integer, 25 | val firstName : String, 26 | val lastName : String, 27 | val email : String, 28 | val enabled : Boolean) { 29 | 30 | def sendEmail() = { 31 | println("Sending Email") 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Chapter8/ExtractingLists/Contact.scala: -------------------------------------------------------------------------------- 1 | object Contact { 2 | 3 | def setNameAndEmailForContactAndCustomer( 4 | customer_id : Integer, 5 | contact_id : Integer, 6 | name : String, 7 | email : String) : List[Customer] = { 8 | Customer.updateContactForCustomerContact( 9 | customer_id, 10 | contact_id, 11 | { contact => 12 | new Contact( 13 | contact.contact_id, 14 | contact.firstName, 15 | name, 16 | email, 17 | contact.enabled 18 | ) 19 | } 20 | ) 21 | } 22 | } 23 | 24 | class Contact(val contact_id : Integer, 25 | val firstName : String, 26 | val lastName : String, 27 | val email : String, 28 | val enabled : Boolean) { 29 | 30 | def sendEmail() = { 31 | println("Sending Email") 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Chapter8/ExtractingObjects/Contact.scala: -------------------------------------------------------------------------------- 1 | object Contact { 2 | 3 | def setNameAndEmailForContactAndCustomer( 4 | customer_id : Integer, 5 | contact_id : Integer, 6 | name : String, 7 | email : String) : List[Customer] = { 8 | Customer.updateContactForCustomerContact( 9 | customer_id, 10 | contact_id, 11 | { contact => 12 | new Contact( 13 | contact.contact_id, 14 | contact.firstName, 15 | name, 16 | email, 17 | contact.enabled 18 | ) 19 | } 20 | ) 21 | } 22 | } 23 | 24 | class Contact(val contact_id : Integer, 25 | val firstName : String, 26 | val lastName : String, 27 | val email : String, 28 | val enabled : Boolean) { 29 | 30 | def sendEmail() = { 31 | println("Sending Email") 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Chapter8/SimpleMatches/Contact.scala: -------------------------------------------------------------------------------- 1 | object Contact { 2 | 3 | def setNameAndEmailForContactAndCustomer( 4 | customer_id : Integer, 5 | contact_id : Integer, 6 | name : String, 7 | email : String) : List[Customer] = { 8 | Customer.updateContactForCustomerContact( 9 | customer_id, 10 | contact_id, 11 | { contact => 12 | new Contact( 13 | contact.contact_id, 14 | contact.firstName, 15 | name, 16 | email, 17 | contact.enabled 18 | ) 19 | } 20 | ) 21 | } 22 | } 23 | 24 | class Contact(val contact_id : Integer, 25 | val firstName : String, 26 | val lastName : String, 27 | val email : String, 28 | val enabled : Boolean) { 29 | 30 | def sendEmail() = { 31 | println("Sending Email") 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Chapter8/SimplePatternExtraction/Contact.scala: -------------------------------------------------------------------------------- 1 | object Contact { 2 | 3 | def setNameAndEmailForContactAndCustomer( 4 | customer_id : Integer, 5 | contact_id : Integer, 6 | name : String, 7 | email : String) : List[Customer] = { 8 | Customer.updateContactForCustomerContact( 9 | customer_id, 10 | contact_id, 11 | { contact => 12 | new Contact( 13 | contact.contact_id, 14 | contact.firstName, 15 | name, 16 | email, 17 | contact.enabled 18 | ) 19 | } 20 | ) 21 | } 22 | } 23 | 24 | class Contact(val contact_id : Integer, 25 | val firstName : String, 26 | val lastName : String, 27 | val email : String, 28 | val enabled : Boolean) { 29 | 30 | def sendEmail() = { 31 | println("Sending Email") 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Chapter6/LazinessCanCreateProblems/Example2.groovy: -------------------------------------------------------------------------------- 1 | 2 | class Customer { 3 | final Integer id 4 | final Boolean enabled 5 | public Customer(id, enabled) { this.id = id; this.enabled = enabled; } 6 | } 7 | 8 | class CustomerContainer { 9 | public List customers = [] 10 | public List onlyEnabled = [] 11 | public CustomerContainer() { this([]) } 12 | public CustomerContainer(customers) { 13 | this.customers = customers 14 | this.onlyEnabled = customers.findAll { customer -> customer.enabled } 15 | } 16 | def addCustomer(c) { 17 | new CustomerContainer(customers.plus(customers.size(), [c])) 18 | } 19 | def removeCustomer(c) { 20 | new CustomerContainer(customers.findAll { customer -> customer.id != c.id }) 21 | } 22 | } 23 | 24 | def cc = new CustomerContainer() 25 | cc = cc.addCustomer(new Customer(1, true)) 26 | cc = cc.addCustomer(new Customer(2, false)) 27 | println(cc.customers) 28 | -------------------------------------------------------------------------------- /Chapter3/SideEffects/FunctionalConcepts.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | 4 | public class FunctionalConcepts { 5 | 6 | private FunctionalConcepts() {} 7 | 8 | public static List map(List inList, Function1 func) { 9 | ArrayList outList = new ArrayList(); 10 | for(A1 obj : inList) { 11 | outList.add(func.call(obj)); 12 | } 13 | return outList; 14 | } 15 | 16 | public static void foreach(ArrayList inList, Foreach1 func) { 17 | for(A obj : inList) { 18 | func.call(obj); 19 | } 20 | } 21 | 22 | public static ArrayList filter(ArrayList inList, 23 | Function1 test) { 24 | ArrayList outList = new ArrayList(); 25 | for(A obj : inList) { 26 | if(test.call(obj)) { 27 | outList.add(obj); 28 | } 29 | } 30 | return outList; 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /Chapter6/LazinessCanCreateProblems/Example1.groovy: -------------------------------------------------------------------------------- 1 | 2 | class Customer { 3 | final Integer id 4 | final Boolean enabled 5 | public Customer(id, enabled) { this.id = id; this.enabled = enabled; } 6 | } 7 | 8 | class CustomerContainer { 9 | public List customers = [] 10 | @Lazy public volatile List onlyEnabled = { 11 | customers.findAll { customer -> 12 | customer.enabled 13 | } 14 | }() 15 | public CustomerContainer() { this([]) } 16 | public CustomerContainer(customers) { this.customers = customers } 17 | def addCustomer(c) { 18 | new CustomerContainer(customers.plus(customers.size(), [c])) 19 | } 20 | def removeCustomer(c) { 21 | new CustomerContainer(customers.findAll { customer -> customer.id != c.id }) 22 | } 23 | } 24 | 25 | def cc = new CustomerContainer() 26 | cc = cc.addCustomer(new Customer(1, true)) 27 | cc = cc.addCustomer(new Customer(2, false)) 28 | println(cc.customers) 29 | -------------------------------------------------------------------------------- /Chapter10/PuttingItAllTogether/fDB.scala: -------------------------------------------------------------------------------- 1 | import scala.annotation.tailrec 2 | 3 | object fDB { 4 | 5 | val options = Map[String, CommandLineOption]( 6 | "create" -> new CommandLineOption("Create Table", Database.createTable), 7 | "describe" -> new CommandLineOption("Describe Table", Database.describeTable), 8 | "insert" -> new CommandLineOption("Insert Record", Database.insert), 9 | "delete" -> new CommandLineOption("Delete Record", Database.delete), 10 | "select" -> new CommandLineOption("Select Record", Database.select), 11 | "exit" -> new CommandLineOption("Exit", db => sys.exit) 12 | ) 13 | 14 | @tailrec 15 | def mainLoop(database : Database) : Unit = mainLoop( 16 | CommandLine.optionPrompt(options) match { 17 | case Some(opt) => opt.exec(database) 18 | case _ => { println("Invalid option"); database } 19 | } 20 | ) 21 | 22 | def main(args : Array[String]) = { 23 | mainLoop(new Database(Map())) 24 | } 25 | 26 | } -------------------------------------------------------------------------------- /Chapter10/PuttingItAllTogether/Record.scala: -------------------------------------------------------------------------------- 1 | object Record { 2 | 3 | def create(fields : List[String], 4 | fieldValues : Map[String, String]) : Record = fields match { 5 | case List() => new Record(fieldValues) 6 | case f :: fs => create( 7 | fs, 8 | fieldValues + (f -> CommandLine.prompt("Value [" + f + "]")) 9 | ) 10 | } 11 | 12 | def print(fields : List[String], id : Long, record : Record) : Unit = { 13 | def _print(fieldList : List[String], output : String) : String = fieldList match { 14 | case List() => output 15 | case f :: fs => _print(fs, output + f + ": " + record.fieldValues(f) + "\n") 16 | } 17 | CommandLine.wrapOutput("------------", "id: " + id + "\n" + _print(fields, "")) 18 | } 19 | 20 | } 21 | 22 | case class Record(fieldValues : Map[String, String]) { 23 | 24 | def print(fields : List[String], id : Long) : Unit = { 25 | Record.print(fields, id, this) 26 | } 27 | 28 | } -------------------------------------------------------------------------------- /Chapter7/TakingThePlunge/Contract.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.List; 3 | import java.util.Calendar; 4 | 5 | public class Contract { 6 | 7 | public final Calendar begin_date; 8 | public final Calendar end_date; 9 | public final Boolean enabled = true; 10 | 11 | public Contract(Calendar begin_date, Calendar end_date, Boolean enabled) { 12 | this.begin_date = begin_date; 13 | this.end_date = end_date; 14 | this.enabled = enabled; 15 | } 16 | 17 | public Contract(Calendar begin_date, Boolean enabled) { 18 | this.begin_date = begin_date; 19 | this.end_date = this.begin_date.getInstance(); 20 | this.end_date.setTimeInMillis(this.begin_date.getTimeInMillis()); 21 | this.end_date.add(Calendar.YEAR, 2); 22 | this.enabled = enabled; 23 | } 24 | 25 | public static List setContractForCustomerList( 26 | List ids, 27 | Boolean status) { 28 | Customer.updateContractForCustomerList(ids) { contract -> 29 | new Contract(contract.begin_date, contract.end_date, status) 30 | } 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /Chapter3/SideEffects/Contract.java: -------------------------------------------------------------------------------- 1 | import java.util.Calendar; 2 | 3 | public class Contract { 4 | 5 | public Calendar begin_date; 6 | public Calendar end_date; 7 | public Boolean enabled = true; 8 | 9 | public Contract(Calendar begin_date) { 10 | this.begin_date = begin_date; 11 | this.end_date = this.begin_date.getInstance(); 12 | this.end_date.setTimeInMillis(this.begin_date.getTimeInMillis()); 13 | this.end_date.add(Calendar.YEAR, 2); 14 | } 15 | 16 | public Contract setBeginDate(Calendar begin_date) { 17 | this.begin_date = begin_date; 18 | return this; 19 | } 20 | 21 | public Contract setEndDate(Calendar end_date) { 22 | this.end_date = end_date; 23 | return this; 24 | } 25 | 26 | public Contract setEnabled(Boolean enabled) { 27 | this.enabled = enabled; 28 | return this; 29 | } 30 | 31 | public static void setContractEnabledForCustomer(Integer customer_id) { 32 | for(Customer customer : Customer.allCustomers) { 33 | if(customer.id == customer_id) { 34 | customer.contract.enabled = true; 35 | } 36 | } 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Chapter9/CodeAsData/CommandLine.scala: -------------------------------------------------------------------------------- 1 | case class CommandLineOption(description : String, func : () => Unit) 2 | 3 | object CommandLine { 4 | 5 | val options : Map[String, CommandLineOption] = Map( 6 | "1" -> new CommandLineOption("Add Customer", Customer.createCustomer), 7 | "2" -> new CommandLineOption("List Customers", Customer.list), 8 | "3" -> new CommandLineOption("List Enabled Contacts for Enabled Customers", 9 | () => Customer.eachEnabledContact(contact => println(contact)) 10 | ), 11 | "q" -> new CommandLineOption("Quit", sys.exit) 12 | ) 13 | 14 | def askForInput(question : String) : String = { 15 | print(question + ": ") 16 | readLine() 17 | } 18 | 19 | def prompt() = { 20 | options.foreach(option => println(option._1 + ") " + option._2.description)) 21 | options.get(askForInput("Option").trim.toLowerCase) match { 22 | case Some(CommandLineOption(_, exec)) => exec() 23 | case _ => println("Invalid input") 24 | } 25 | } 26 | 27 | def main(args : Array[String]) = { 28 | Customer.allCustomers = List() 29 | while(true) { 30 | prompt() 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /Chapter7/TakingThePlunge/Contact.java: -------------------------------------------------------------------------------- 1 | 2 | public class Contact { 3 | 4 | public final Integer contact_id = 0; 5 | public final String firstName = ""; 6 | public final String lastName = ""; 7 | public final String email = ""; 8 | public final Boolean enabled = true; 9 | 10 | public Contact(Integer contact_id, 11 | String firstName, 12 | String lastName, 13 | String email, 14 | Boolean enabled) { 15 | this.contact_id = contact_id; 16 | this.firstName = firstName; 17 | this.lastName = lastName; 18 | this.email = email; 19 | this.enabled = enabled; 20 | } 21 | 22 | public static List setNameAndEmailForContactAndCustomer( 23 | Integer customer_id, 24 | Integer contact_id, 25 | String name, 26 | String email) { 27 | Customer.updateContactForCustomerContact( 28 | customer_id, 29 | contact_id, 30 | { contact -> 31 | new Contact( 32 | contact.contact_id, 33 | contact.firstName, 34 | name, 35 | email, 36 | contact.enabled 37 | ) 38 | } 39 | ) 40 | } 41 | 42 | public void sendEmail() { 43 | println("Sending Email") 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /Chapter9/CodeAsData/Contact.scala: -------------------------------------------------------------------------------- 1 | object Contact { 2 | 3 | def setNameAndEmailForContactAndCustomer( 4 | customer_id : Integer, 5 | contact_id : Integer, 6 | name : String, 7 | email : String) : List[Customer] = { 8 | Customer.updateContactForCustomerContact( 9 | customer_id, 10 | contact_id, 11 | { contact => 12 | new Contact( 13 | contact.contact_id, 14 | contact.firstName, 15 | name, 16 | email, 17 | contact.enabled 18 | ) 19 | } 20 | ) 21 | } 22 | 23 | def createContact() : Contact = { 24 | new Contact( 25 | 0, 26 | CommandLine.askForInput("First Name"), 27 | CommandLine.askForInput("Last Name"), 28 | CommandLine.askForInput("E-Mail"), 29 | true 30 | ) 31 | } 32 | } 33 | 34 | class Contact(val contact_id : Integer, 35 | val firstName : String, 36 | val lastName : String, 37 | val email : String, 38 | val enabled : Boolean) { 39 | 40 | def sendEmail() = { 41 | new Email(this.email, "My Subject", "My Body", true, this.firstName).send() 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /Chapter6/LazinessCanCreateProblems/Example1.scala: -------------------------------------------------------------------------------- 1 | 2 | class Customer(val id : Integer, 3 | val enabled : Boolean, 4 | val contracts : List[Double]) { 5 | 6 | lazy val revenue : Double = calculateRevenue(this.contracts) 7 | 8 | def calculateRevenue(contracts : List[Double]) : Double = { 9 | var sum : Double = 0.0 10 | for(contract <- contracts) { 11 | sum += contract 12 | } 13 | sum 14 | } 15 | } 16 | 17 | 18 | class CustomerContainer(val customers : List[Customer] = List()) { 19 | 20 | val onlyEnabled = customers.filter { customer => customer.enabled } 21 | 22 | def addCustomer(c : Customer) : CustomerContainer = { 23 | new CustomerContainer(customers ::: List(c)) 24 | } 25 | 26 | def removeCustomer(c : Customer) : CustomerContainer = { 27 | new CustomerContainer(customers.filter { customer => customer.id != c.id }) 28 | } 29 | 30 | } 31 | 32 | var cc = new CustomerContainer() 33 | 34 | cc = cc.addCustomer(new Customer(1, true, List(100.0, 200.0, 300.0))) 35 | cc = cc.addCustomer(new Customer(2, false, List(100.0, 150.0, 500.0))) 36 | 37 | println(cc.customers) 38 | 39 | var sum : Double = 0.0 40 | for(customer <- cc.onlyEnabled) { 41 | sum += customer.revenue 42 | } 43 | 44 | println(s"Enabled Revenue: ${sum}") 45 | -------------------------------------------------------------------------------- /Chapter6/LazinessCanCreateProblems/Example3.groovy: -------------------------------------------------------------------------------- 1 | 2 | class Customer { 3 | final Integer id 4 | final Boolean enabled 5 | final List contracts 6 | @Lazy volatile Double revenue = calculateRevenue(this.contracts) 7 | static def calculateRevenue(contracts) { 8 | Double sum = 0.0 9 | for(Double contract : contracts) { 10 | sum += contract 11 | } 12 | sum 13 | } 14 | public Customer(id, enabled, contracts) { 15 | this.id = id 16 | this.enabled = enabled 17 | this.contracts = contracts 18 | } 19 | } 20 | 21 | class CustomerContainer { 22 | public List customers = [] 23 | public List onlyEnabled = [] 24 | public CustomerContainer() { this([]) } 25 | public CustomerContainer(customers) { 26 | this.customers = customers 27 | this.onlyEnabled = customers.findAll { customer -> customer.enabled } 28 | } 29 | def addCustomer(c) { 30 | new CustomerContainer(customers.plus(customers.size(), [c])) 31 | } 32 | def removeCustomer(c) { 33 | new CustomerContainer(customers.findAll { customer -> customer.id != c.id }) 34 | } 35 | } 36 | 37 | def cc = new CustomerContainer() 38 | cc = cc.addCustomer(new Customer(1, true, [100.0, 200.0, 300.0])) 39 | cc = cc.addCustomer(new Customer(2, false, [100.0, 150.0, 500.0])) 40 | println(cc.customers) 41 | Double sum = 0.0 42 | for(Customer customer : cc.onlyEnabled) { 43 | sum += customer.revenue 44 | } 45 | println("Enabled Revenue: ${sum}") 46 | -------------------------------------------------------------------------------- /Chapter10/PuttingItAllTogether/Database.scala: -------------------------------------------------------------------------------- 1 | 2 | object Database { 3 | 4 | def createTable(database : Database) : Database = { 5 | new Database(database.tables + 6 | (CommandLine.prompt("Table Name") -> Table.create())) 7 | } 8 | 9 | def describeTable(database : Database) : Database = { 10 | database.tables.get(CommandLine.prompt("Table Name")) match { 11 | case Some(table) => table.describe() 12 | case _ => println("Table does not exist") 13 | } 14 | database 15 | } 16 | 17 | def insert(database : Database) : Database = { 18 | val tableName = CommandLine.prompt("Table Name") 19 | database.tables.get(tableName) match { 20 | case Some(table) => { 21 | new Database(database.tables + (tableName -> table.insert())) 22 | } 23 | case _ => { println("Table does not exist"); database } 24 | } 25 | } 26 | 27 | def select(database : Database) : Database = { 28 | database.tables.get(CommandLine.prompt("Table Name")) match { 29 | case Some(table) => table.select() 30 | case _ => println("Table does not exist") 31 | } 32 | database 33 | } 34 | 35 | def delete(database : Database) : Database = { 36 | val tableName = CommandLine.prompt("Table Name") 37 | database.tables.get(tableName) match { 38 | case Some(table) => new Database( 39 | database.tables + (tableName -> table.delete())) 40 | case _ => { println("Table does not exist"); database } 41 | } 42 | } 43 | } 44 | 45 | case class Database(tables : Map[String, Table]) {} -------------------------------------------------------------------------------- /Chapter2/Introduction/Customer.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | 4 | public class Customer { 5 | 6 | static public ArrayList allCustomers = new ArrayList(); 7 | public Integer id = 0; 8 | public String name = ""; 9 | public String address = ""; 10 | public String state = ""; 11 | public String primaryContact = ""; 12 | public String domain = ""; 13 | public Boolean enabled = true; 14 | 15 | public Customer() {} 16 | 17 | public static List getEnabledCustomerNames() { 18 | ArrayList outList = new ArrayList(); 19 | for(Customer customer : Customer.allCustomers) { 20 | if(customer.enabled) { 21 | outList.add(customer.name); 22 | } 23 | } 24 | return outList; 25 | } 26 | 27 | public static List getEnabledCustomerStates() { 28 | ArrayList outList = new ArrayList(); 29 | for(Customer customer : Customer.allCustomers) { 30 | if(customer.enabled) { 31 | outList.add(customer.state); 32 | } 33 | } 34 | return outList; 35 | } 36 | 37 | public static List getEnabledCustomerPrimaryContacts() { 38 | ArrayList outList = new ArrayList(); 39 | for(Customer customer : Customer.allCustomers) { 40 | if(customer.enabled) { 41 | outList.add(customer.primaryContact); 42 | } 43 | } 44 | return outList; 45 | } 46 | 47 | public static List getEnabledCustomerDomains() { 48 | ArrayList outList = new ArrayList(); 49 | for(Customer customer : Customer.allCustomers) { 50 | if(customer.enabled) { 51 | outList.add(customer.domain); 52 | } 53 | } 54 | return outList; 55 | } 56 | 57 | /* TODO: Add a main function */ 58 | } -------------------------------------------------------------------------------- /Chapter2/AnonymousFunctions/Lambda/Customer.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | 4 | public class Customer { 5 | 6 | static public ArrayList allCustomers = new ArrayList(); 7 | public Integer id = 0; 8 | public String name = ""; 9 | public String address = ""; 10 | public String state = ""; 11 | public String primaryContact = ""; 12 | public String domain = ""; 13 | public Boolean enabled = true; 14 | 15 | public Customer() {} 16 | 17 | private interface Function1 { 18 | public B call(A1 in1); 19 | } 20 | 21 | public static List getEnabledCustomerAddresses() { 22 | return Customer.getEnabledCustomerField(new Function1() { 23 | public String call(Customer customer) { return customer.addresses; } 24 | }); 25 | } 26 | 27 | public static List getEnabledCustomerNames() { 28 | return Customer.getEnabledCustomerField(new Function1() { 29 | public String call(Customer customer) { return customer.name; } 30 | }); 31 | } 32 | 33 | public static List getEnabledCustomerStates() { 34 | return Customer.getEnabledCustomerField(new Function1() { 35 | public String call(Customer customer) { return customer.state; } 36 | }); 37 | } 38 | 39 | public static List getEnabledCustomerPrimaryContacts() { 40 | return Customer.getEnabledCustomerField(new Function1() { 41 | public String call(Customer customer) { return customer.primaryContact; } 42 | }); 43 | } 44 | 45 | public static List getEnabledCustomerDomains() { 46 | return Customer.getEnabledCustomerField(new Function1() { 47 | public String call(Customer customer) { return customer.domain; } 48 | }); 49 | } 50 | 51 | public static List getEnabledCustomerField(Function1 func) { 52 | ArrayList outList = new ArrayList(); 53 | for(Customer customer : Customer.allCustomers) { 54 | if(customer.enabled) { 55 | outList.add(func.call(customer)); 56 | } 57 | } 58 | return outList; 59 | } 60 | } -------------------------------------------------------------------------------- /Chapter3/OutputDependsOnInput/Customer.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | 4 | public class Customer { 5 | 6 | static public ArrayList allCustomers = new ArrayList(); 7 | public Integer id = 0; 8 | public String name = ""; 9 | public String address = ""; 10 | public String state = ""; 11 | public String primaryContact = ""; 12 | public String domain = ""; 13 | public Boolean enabled = true; 14 | public Contract contract; 15 | 16 | public Customer() {} 17 | 18 | private interface Function1 { 19 | public B call(A1 in1); 20 | } 21 | 22 | public static List getEnabledCustomerAddresses() { 23 | return Customer.getEnabledCustomerField(new Function1() { 24 | public String call(Customer customer) { return customer.address; } 25 | }); 26 | } 27 | 28 | public static List getEnabledCustomerNames() { 29 | return Customer.getEnabledCustomerField(new Function1() { 30 | public String call(Customer customer) { return customer.name; } 31 | }); 32 | } 33 | 34 | public static List getEnabledCustomerStates() { 35 | return Customer.getEnabledCustomerField(new Function1() { 36 | public String call(Customer customer) { return customer.state; } 37 | }); 38 | } 39 | 40 | public static List getEnabledCustomerPrimaryContacts() { 41 | return Customer.getEnabledCustomerField(new Function1() { 42 | public String call(Customer customer) { return customer.primaryContact; } 43 | }); 44 | } 45 | 46 | public static List getEnabledCustomerDomains() { 47 | return Customer.getEnabledCustomerField(new Function1() { 48 | public String call(Customer customer) { return customer.domain; } 49 | }); 50 | } 51 | 52 | public static List getEnabledCustomerField(Function1 func) { 53 | ArrayList outList = new ArrayList(); 54 | for(Customer customer : Customer.allCustomers) { 55 | if(customer.enabled) { 56 | outList.add(func.call(customer)); 57 | } 58 | } 59 | return outList; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /Chapter3/Conclusion/StartingAMovement/Customer.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | 4 | public class Customer { 5 | 6 | static public ArrayList allCustomers = new ArrayList(); 7 | public Integer id = 0; 8 | public String name = ""; 9 | public String address = ""; 10 | public String state = ""; 11 | public String primaryContact = ""; 12 | public String domain = ""; 13 | public Boolean enabled = true; 14 | public Contract contract; 15 | 16 | public Customer() {} 17 | 18 | 19 | public Customer setCustomerId(Integer customer_id) { 20 | this.customer_id = customer_id; 21 | return this; 22 | } 23 | 24 | public Customer setName(String name) { 25 | this.name = name; 26 | return this; 27 | } 28 | 29 | public Customer setState(String state) { 30 | this.state = state; 31 | return this; 32 | } 33 | 34 | public Customer setDomain(String domain) { 35 | this.domain = domain; 36 | return this; 37 | } 38 | 39 | public Customer setEnabled(Boolean enabled) { 40 | this.enabled = enabled; 41 | return this; 42 | } 43 | 44 | public Customer setContract(Contract contract) { 45 | this.contract = contract; 46 | return this; 47 | } 48 | 49 | static def EnabledCustomer = { customer -> customer.enabled == true } 50 | static def DisabledCustomer = { customer -> customer.enabled == false } 51 | 52 | public static List getDisabledCustomerNames() { 53 | Customer.allCustomers.findAll(DisabledCustomer).collect({cutomer -> 54 | cutomer.name 55 | }) 56 | } 57 | 58 | public static List getEnabledCustomerStates() { 59 | Customer.allCustomers.findAll(EnabledCustomer).collect({cutomer -> 60 | cutomer.state 61 | }) 62 | } 63 | 64 | public static List getEnabledCustomerDomains() { 65 | Customer.allCustomers.findAll(EnabledCustomer).collect({cutomer -> 66 | cutomer.domain 67 | }) 68 | } 69 | 70 | public static List getEnabledCustomerSomeoneEmail(String someone) { 71 | Customer.allCustomers.findAll(EnabledCustomer).collect({cutomer -> 72 | someone + "@" + cutomer.domain 73 | }) 74 | } 75 | 76 | public static ArrayList getCustomerById(ArrayList inList, 77 | final Integer customer_id) { 78 | inList.findAll({customer -> customer.customer_id == customer_id }) 79 | } 80 | } -------------------------------------------------------------------------------- /Chapter2/FunctionsAsObjects/Customer.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | 4 | public class Customer { 5 | 6 | static public ArrayList allCustomers = new ArrayList(); 7 | public Integer id = 0; 8 | public String name = ""; 9 | public String address = ""; 10 | public String state = ""; 11 | public String primaryContact = ""; 12 | public String domain = ""; 13 | public Boolean enabled = true; 14 | 15 | public Customer() {} 16 | 17 | private interface Function1 { 18 | public B call(A1 in1); 19 | } 20 | 21 | static private class CustomerAddress implements Function1 { 22 | public String call(Customer customer) { return customer.address; } 23 | } 24 | 25 | static private class CustomerName implements Function1 { 26 | public String call(Customer customer) { return customer.name; } 27 | } 28 | 29 | static private class CustomerState implements Function1 { 30 | public String call(Customer customer) { return customer.state; } 31 | } 32 | 33 | static private class CustomerPrimaryContact implements Function1 34 | { 35 | public String call(Customer customer) { return customer.primaryContact; } 36 | } 37 | 38 | static private class CustomerDomain implements Function1 { 39 | public String call(Customer customer) { return customer.domain; } 40 | } 41 | 42 | static private class CustomerAsCustomer implements Function1 { 43 | public Customer call(Customer customer) { return customer; } 44 | } 45 | 46 | public static List getEnabledCustomerAddresses() { 47 | return Customer.getEnabledCustomerField(new CustomerAddress()); 48 | } 49 | 50 | public static List getEnabledCustomerNames() { 51 | return Customer.getEnabledCustomerField(new CustomerName()); 52 | } 53 | 54 | public static List getEnabledCustomerStates() { 55 | return Customer.getEnabledCustomerField(new CustomerState()); 56 | } 57 | 58 | public static List getEnabledCustomerPrimaryContacts() { 59 | return Customer.getEnabledCustomerField(new CustomerPrimaryContact()); 60 | } 61 | 62 | public static List getEnabledCustomerDomains() { 63 | return Customer.getEnabledCustomerField(new CustomerDomain()); 64 | } 65 | 66 | public static List getEnabledCustomerField(Function1 func) { 67 | ArrayList outList = new ArrayList(); 68 | for(Customer customer : Customer.allCustomers) { 69 | if(customer.enabled) { 70 | outList.add(func.call(customer)); 71 | } 72 | } 73 | return outList; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /Chapter4/Immutability/Customer.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | 4 | public class Customer { 5 | 6 | static public List allCustomers = new ArrayList(); 7 | public final Integer id = 0; 8 | public final String name = ""; 9 | public final String state = ""; 10 | public final String domain = ""; 11 | public final Boolean enabled = true; 12 | public final Contract contract = null; 13 | public final List contacts = new ArrayList(); 14 | 15 | public Customer(Integer id, 16 | String name, 17 | String state, 18 | String domain, 19 | Boolean enabled, 20 | Contract contract, 21 | List contacts) { 22 | this.id = id; 23 | this.name = name; 24 | this.state = state; 25 | this.domain = domain; 26 | this.enabled = enabled; 27 | this.contract = contract; 28 | this.contacts = contacts; 29 | } 30 | 31 | 32 | public Customer setId(Integer id) { 33 | this.id = id; 34 | return this; 35 | } 36 | 37 | public Customer setName(String name) { 38 | this.name = name; 39 | return this; 40 | } 41 | 42 | public Customer setState(String state) { 43 | this.state = state; 44 | return this; 45 | } 46 | 47 | public Customer setDomain(String domain) { 48 | this.domain = domain; 49 | return this; 50 | } 51 | 52 | public Customer setEnabled(Boolean enabled) { 53 | this.enabled = enabled; 54 | return this; 55 | } 56 | 57 | public Customer setContract(Contract contract) { 58 | this.contract = contract; 59 | return this; 60 | } 61 | 62 | static def EnabledCustomer = { customer -> customer.enabled == true } 63 | static def DisabledCustomer = { customer -> customer.enabled == false } 64 | 65 | public static List getDisabledCustomerNames() { 66 | Customer.allCustomers.findAll(DisabledCustomer).collect({cutomer -> cutomer.name }) 67 | } 68 | 69 | public static List getEnabledCustomerStates() { 70 | Customer.allCustomers.findAll(EnabledCustomer).collect({cutomer -> cutomer.state}) 71 | } 72 | 73 | public static List getEnabledCustomerDomains() { 74 | Customer.allCustomers.findAll(EnabledCustomer).collect({cutomer -> cutomer.domain}) 75 | } 76 | 77 | public static List getEnabledCustomerSomeoneEmail(String someone) { 78 | Customer.allCustomers.findAll(EnabledCustomer).collect({cutomer -> someone + "@" + cutomer.domain}) 79 | } 80 | 81 | public static ArrayList getCustomerById(ArrayList inList, final Integer id) { 82 | inList.findAll({customer -> customer.id == id }) 83 | } 84 | } -------------------------------------------------------------------------------- /Chapter10/PuttingItAllTogether/Table.scala: -------------------------------------------------------------------------------- 1 | object Table { 2 | 3 | def createFields(count : Int, 4 | fields : List[String]) : List[String] = if(count <= 0) { 5 | fields 6 | } else { 7 | createFields(count - 1, fields ::: List(CommandLine.prompt("Field"))) 8 | } 9 | 10 | def create() : Table = new Table( 11 | createFields( 12 | CommandLine.prompt("Number of fields").toInt, 13 | List() 14 | ), 15 | Map(), 16 | 1 17 | ) 18 | 19 | def insert(table : Table) : Table = new Table( 20 | table.fields, 21 | table.records + (table.id -> Record.create(table.fields, Map())), 22 | table.id + 1 23 | ) 24 | 25 | def describe(table : Table) : Table = { 26 | println("(implied) id") 27 | table.fields.foreach(field => println(field)) 28 | table 29 | } 30 | 31 | def select(table : Table) : Table = { 32 | CommandLine.prompt("Filter By Field? (y/n)").toLowerCase match { 33 | case "y" => selectWithFilter(table) 34 | case "n" => selectAll(table) 35 | case _ => { println("Invalid selection"); select(table); } 36 | } 37 | } 38 | 39 | def selectAll(table : Table) : Table = { 40 | table.records.foreach(record => record._2.print(table.fields, record._1)) 41 | table 42 | } 43 | 44 | def selectWithFilter(table : Table) : Table = { 45 | performFilter( 46 | table, 47 | CommandLine.prompt("Filter Field"), 48 | CommandLine.prompt("Field Value") 49 | ).foreach(record => 50 | record._2.print(table.fields, record._1) 51 | ) 52 | table 53 | } 54 | 55 | def performFilter(table : Table, 56 | fieldName : String, 57 | fieldValue : String) : Map[Long, Record] = { 58 | if(fieldName == "id") { 59 | table.records.get(fieldValue.toLong) match { 60 | case Some(record) => Map(fieldValue.toLong -> record) 61 | case _ => Map() 62 | } 63 | } else { 64 | table.records.filter(record => 65 | record._2.fieldValues.get(fieldName) match { 66 | case Some(value) => value == fieldValue 67 | case _ => false 68 | } 69 | ) 70 | } 71 | } 72 | 73 | def delete(table : Table) : Table = { 74 | new Table(table.fields, table.records - CommandLine.prompt("ID").toLong, table.id) 75 | } 76 | 77 | } 78 | 79 | case class Table(fields : List[String], records : Map[Long, Record], id : Long) { 80 | 81 | def delete() : Table = { 82 | Table.delete(this) 83 | } 84 | 85 | def select() : Table = { 86 | Table.select(this) 87 | } 88 | 89 | def insert() : Table = { 90 | Table.insert(this) 91 | } 92 | 93 | def describe() : Table = { 94 | Table.describe(this) 95 | } 96 | 97 | } -------------------------------------------------------------------------------- /Chapter3/SideEffects/Customer.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | 4 | public class Customer { 5 | 6 | static public ArrayList allCustomers = new ArrayList(); 7 | public Integer id = 0; 8 | public String name = ""; 9 | public String address = ""; 10 | public String state = ""; 11 | public String primaryContact = ""; 12 | public String domain = ""; 13 | public Boolean enabled = true; 14 | public Contract contract; 15 | 16 | public Customer() {} 17 | 18 | 19 | public Customer setCustomerId(Integer customer_id) { 20 | this.id = customer_id; 21 | return this; 22 | } 23 | 24 | public Customer setName(String name) { 25 | this.name = name; 26 | return this; 27 | } 28 | 29 | public Customer setState(String state) { 30 | this.state = state; 31 | return this; 32 | } 33 | 34 | public Customer setDomain(String domain) { 35 | this.domain = domain; 36 | return this; 37 | } 38 | 39 | public Customer setEnabled(Boolean enabled) { 40 | this.enabled = enabled; 41 | return this; 42 | } 43 | 44 | public Customer setContract(Contract contract) { 45 | this.contract = contract; 46 | return this; 47 | } 48 | 49 | private interface Function1 { 50 | public B call(A1 in1); 51 | } 52 | 53 | public static List getEnabledCustomerAddresses() { 54 | return Customer.getEnabledCustomerField(new Function1() { 55 | public String call(Customer customer) { return customer.address; } 56 | }); 57 | } 58 | 59 | public static List getEnabledCustomerNames() { 60 | return Customer.getEnabledCustomerField(new Function1() { 61 | public String call(Customer customer) { return customer.name; } 62 | }); 63 | } 64 | 65 | public static List getEnabledCustomerStates() { 66 | return Customer.getEnabledCustomerField(new Function1() { 67 | public String call(Customer customer) { return customer.state; } 68 | }); 69 | } 70 | 71 | public static List getEnabledCustomerPrimaryContacts() { 72 | return Customer.getEnabledCustomerField(new Function1() { 73 | public String call(Customer customer) { return customer.primaryContact; } 74 | }); 75 | } 76 | 77 | public static List getEnabledCustomerDomains() { 78 | return Customer.getEnabledCustomerField(new Function1() { 79 | public String call(Customer customer) { return customer.domain; } 80 | }); 81 | } 82 | 83 | public static List map(List inList, Function1 func) { 84 | ArrayList outList = new ArrayList(); 85 | for(A1 obj : inList) { 86 | outList.add(func.call(obj)); 87 | } 88 | return outList; 89 | } 90 | 91 | public static List getEnabledCustomerField(Function1 func) { 92 | ArrayList outList = new ArrayList(); 93 | for(Customer customer : Customer.allCustomers) { 94 | if(customer.enabled) { 95 | outList.add(func.call(customer)); 96 | } 97 | } 98 | return outList; 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /Chapter7/TakingThePlunge/Customer.scala: -------------------------------------------------------------------------------- 1 | object Customer { 2 | 3 | val allCustomers = List[Customer]() 4 | 5 | def EnabledCustomer(customer : Customer) : Boolean = customer.enabled == true 6 | def DisabledCustomer(customer : Customer) : Boolean = customer.enabled == false 7 | 8 | def getDisabledCustomerNames() : List[String] = { 9 | Customer.allCustomers.filter(DisabledCustomer).map({ customer => 10 | customer.name 11 | }) 12 | } 13 | 14 | def getEnabledCustomerStates() : List[String] = { 15 | Customer.allCustomers.filter(EnabledCustomer).map({ customer => 16 | customer.state 17 | }) 18 | } 19 | 20 | def getEnabledCustomerDomains() : List[String] = { 21 | Customer.allCustomers.filter(EnabledCustomer).map({ customer => 22 | customer.domain 23 | }) 24 | } 25 | 26 | def getEnabledCustomerSomeoneEmail(someone : String) : List[String] = { 27 | Customer.allCustomers.filter(EnabledCustomer).map({ customer => 28 | someone + "@" + customer.domain 29 | }) 30 | } 31 | 32 | def getCustomerById(inList : List[Customer], 33 | customer_id : Integer) : List[Customer] = { 34 | inList.filter(customer => customer.customer_id == customer_id) 35 | } 36 | 37 | def eachEnabledContact(cls : Contact => Unit) { 38 | Customer.allCustomers.filter({ customer => 39 | customer.enabled && customer.contract.enabled 40 | }).foreach({ customer => 41 | customer.contacts.foreach(cls) 42 | }) 43 | } 44 | 45 | def updateCustomerByIdList(initialIds : List[Customer], 46 | ids : List[Integer], 47 | cls : Customer => Customer) : List[Customer] = { 48 | if(ids.size <= 0) { 49 | initialIds 50 | } else if(initialIds.size <= 0) { 51 | List() 52 | } else { 53 | val precust = initialIds.find(cust => cust.customer_id == ids(0)) 54 | val cust = if(precust.isEmpty) { List() } else { List(cls(precust.get)) } 55 | cust ::: updateCustomerByIdList( 56 | initialIds.filter(cust => cust.customer_id == ids(0)), 57 | ids.drop(1), 58 | cls 59 | ) 60 | } 61 | } 62 | 63 | def updateContactForCustomerContact(customer_id : Integer, 64 | contact_id : Integer, 65 | cls : Contact => Contact) : List[Customer] = { 66 | updateCustomerByIdList(Customer.allCustomers, List(customer_id), { customer => 67 | new Customer( 68 | customer.customer_id, 69 | customer.name, 70 | customer.state, 71 | customer.domain, 72 | customer.enabled, 73 | customer.contract, 74 | customer.contacts.map { contact => 75 | if(contact.contact_id == contact_id) { 76 | cls(contact) 77 | } else { 78 | contact 79 | } 80 | } 81 | ) 82 | }) 83 | } 84 | 85 | def updateContractForCustomerList(ids : List[Integer], 86 | cls : Contract => Contract) : List[Customer] = { 87 | updateCustomerByIdList(Customer.allCustomers, ids, { customer => 88 | new Customer( 89 | customer.customer_id, 90 | customer.name, 91 | customer.state, 92 | customer.domain, 93 | customer.enabled, 94 | cls(customer.contract), 95 | customer.contacts 96 | ) 97 | }) 98 | } 99 | 100 | def countEnabledCustomersWithNoEnabledContacts(customers : List[Customer], 101 | sum : Integer) : Integer = { 102 | if(customers.isEmpty) { 103 | sum 104 | } else { 105 | val addition = if(customers.head.enabled && 106 | customers.head.contacts.exists({ contact => 107 | contact.enabled 108 | })) { 109 | 1 110 | } else { 111 | 0 112 | } 113 | countEnabledCustomersWithNoEnabledContacts(customers.tail, addition + sum) 114 | } 115 | } 116 | } 117 | 118 | class Customer(val customer_id : Integer, 119 | val name : String, 120 | val state : String, 121 | val domain : String, 122 | val enabled : Boolean, 123 | val contract : Contract, 124 | val contacts : List[Contact]) { 125 | } 126 | -------------------------------------------------------------------------------- /Chapter7/TakingThePlunge/Customer.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | import java.util.Calendar; 4 | 5 | public class Customer { 6 | 7 | static public List allCustomers = new ArrayList(); 8 | public final Integer id = 0; 9 | public final String name = ""; 10 | public final String state = ""; 11 | public final String domain = ""; 12 | public final Boolean enabled = true; 13 | public final Contract contract = null; 14 | public final List contacts = new ArrayList(); 15 | @Lazy public List enabledContacts = contacts.findAll { contact -> 16 | contact.enabled 17 | } 18 | 19 | public Customer(Integer id, 20 | String name, 21 | String state, 22 | String domain, 23 | Boolean enabled, 24 | Contract contract, 25 | List contacts) { 26 | this.id = id; 27 | this.name = name; 28 | this.state = state; 29 | this.domain = domain; 30 | this.enabled = enabled; 31 | this.contract = contract; 32 | this.contacts = contacts; 33 | } 34 | 35 | static def EnabledCustomer = { customer -> customer.enabled == true } 36 | static def DisabledCustomer = { customer -> customer.enabled == false } 37 | 38 | public static List getDisabledCustomerNames() { 39 | Customer.allCustomers.findAll(DisabledCustomer).collect({cutomer -> 40 | cutomer.name 41 | }) 42 | } 43 | 44 | public static List getEnabledCustomerStates() { 45 | Customer.allCustomers.findAll(EnabledCustomer).collect({cutomer -> 46 | cutomer.state 47 | }) 48 | } 49 | 50 | public static List getEnabledCustomerDomains() { 51 | Customer.allCustomers.findAll(EnabledCustomer).collect({cutomer -> 52 | cutomer.domain 53 | }) 54 | } 55 | 56 | public static List getEnabledCustomerSomeoneEmail(String someone) { 57 | Customer.allCustomers.findAll(EnabledCustomer).collect({cutomer -> 58 | someone + "@" + cutomer.domain 59 | }) 60 | } 61 | 62 | public static ArrayList getCustomerById( 63 | ArrayList inList, 64 | final Integer id) { 65 | inList.findAll({customer -> customer.id == id }) 66 | } 67 | 68 | public static void eachEnabledContact(Closure cls) { 69 | Customer.allCustomers.findAll { customer -> 70 | customer.enabled && customer.contract.enabled 71 | }.each { customer -> 72 | customer.contacts.each(cls) 73 | } 74 | } 75 | 76 | public static List updateCustomerByIdList( 77 | List initialIds, 78 | List ids, 79 | Closure cls) { 80 | if(ids.size() <= 0) { 81 | initialIds 82 | } else if(initialIds.size() <= 0) { 83 | [] 84 | } else { 85 | def idx = ids.indexOf(initialIds[0].id) 86 | def cust = idx >= 0 ? cls(initialIds[0]) : initialIds[0] 87 | [cust] + updateCustomerByIdList( 88 | initialIds.drop(1), 89 | idx >= 0 ? ids.minus(initialIds[0].id) : ids, 90 | cls 91 | ) 92 | } 93 | } 94 | 95 | public static List updateContactForCustomerContact( 96 | Integer id, 97 | Integer contact_id, 98 | Closure cls) { 99 | updateCustomerByIdList(Customer.allCustomers, [id], { customer -> 100 | new Customer( 101 | customer.id, 102 | customer.name, 103 | customer.state, 104 | customer.domain, 105 | customer.enabled, 106 | customer.contract, 107 | customer.contacts.collect { contact -> 108 | if(contact.contact_id == contact_id) { 109 | cls(contact) 110 | } else { 111 | contact 112 | } 113 | } 114 | ) 115 | }) 116 | } 117 | 118 | public static List updateContractForCustomerList( 119 | List ids, 120 | Closure cls) { 121 | updateCustomerByIdList(Customer.allCustomers, ids, { customer -> 122 | new Customer( 123 | customer.id, 124 | customer.name, 125 | customer.state, 126 | customer.domain, 127 | customer.enabled, 128 | cls(customer.contract), 129 | customer.contacts 130 | ) 131 | }) 132 | } 133 | 134 | public static def countEnabledCustomersWithNoEnabledContacts = { 135 | List customers, Integer sum -> 136 | if(customers.isEmpty()) { 137 | return sum 138 | } else { 139 | int addition = (customers.head().enabled && 140 | (customers.head().contacts.find({ contact -> 141 | contact.enabled 142 | }) == null)) ? 1 : 0 143 | return countEnabledCustomersWithNoEnabledContacts.trampoline( 144 | customers.tail(), 145 | addition + sum 146 | ) 147 | } 148 | }.trampoline() 149 | } 150 | -------------------------------------------------------------------------------- /Chapter8/SimplePatternExtraction/Customer.scala: -------------------------------------------------------------------------------- 1 | import java.util.Calendar 2 | 3 | object Customer { 4 | 5 | val allCustomers = List[Customer]() 6 | 7 | def EnabledCustomer(customer : Customer) : Boolean = customer.enabled == true 8 | def DisabledCustomer(customer : Customer) : Boolean = customer.enabled == false 9 | 10 | def getDisabledCustomerNames() : List[String] = { 11 | Customer.allCustomers.filter(DisabledCustomer).map({ customer => 12 | customer.name 13 | }) 14 | } 15 | 16 | def getEnabledCustomerStates() : List[String] = { 17 | Customer.allCustomers.filter(EnabledCustomer).map({ customer => 18 | customer.state 19 | }) 20 | } 21 | 22 | def getEnabledCustomerDomains() : List[String] = { 23 | Customer.allCustomers.filter(EnabledCustomer).map({ customer => 24 | customer.domain 25 | }) 26 | } 27 | 28 | def getEnabledCustomerSomeoneEmail(someone : String) : List[String] = { 29 | Customer.allCustomers.filter(EnabledCustomer).map({ customer => 30 | someone + "@" + customer.domain 31 | }) 32 | } 33 | 34 | def getCustomerById(inList : List[Customer], 35 | customer_id : Integer) : List[Customer] = { 36 | inList.filter(customer => customer.customer_id == customer_id) 37 | } 38 | 39 | def eachEnabledContact(cls : Contact => Unit) { 40 | Customer.allCustomers.filter({ customer => 41 | customer.enabled && customer.contract.enabled 42 | }).foreach({ customer => 43 | customer.contacts.foreach(cls) 44 | }) 45 | } 46 | 47 | def updateCustomerByIdList(initialIds : List[Customer], 48 | ids : List[Integer], 49 | cls : Customer => Customer) : List[Customer] = { 50 | (initialIds, ids) match { 51 | case (List(), _) => initialIds 52 | case (_, List()) => initialIds 53 | case _ => { 54 | val precust = initialIds.find(cust => cust.customer_id == ids(0)) 55 | val cust = if(precust.isEmpty) { List() } else { List(cls(precust.get)) } 56 | cust ::: updateCustomerByIdList( 57 | initialIds.filter(cust => cust.customer_id == ids(0)), 58 | ids.drop(1), 59 | cls 60 | ) 61 | } 62 | } 63 | } 64 | 65 | def updateContactForCustomerContact(customer_id : Integer, 66 | contact_id : Integer, 67 | cls : Contact => Contact) : List[Customer] = { 68 | updateCustomerByIdList(Customer.allCustomers, List(customer_id), { customer => 69 | new Customer( 70 | customer.customer_id, 71 | customer.name, 72 | customer.state, 73 | customer.domain, 74 | customer.enabled, 75 | customer.contract, 76 | customer.contacts.map { contact => 77 | if(contact.contact_id == contact_id) { 78 | cls(contact) 79 | } else { 80 | contact 81 | } 82 | } 83 | ) 84 | }) 85 | } 86 | 87 | def updateContractForCustomerList(ids : List[Integer], 88 | cls : Contract => Contract) : List[Customer] = { 89 | updateCustomerByIdList(Customer.allCustomers, ids, { customer => 90 | new Customer( 91 | customer.customer_id, 92 | customer.name, 93 | customer.state, 94 | customer.domain, 95 | customer.enabled, 96 | cls(customer.contract), 97 | customer.contacts 98 | ) 99 | }) 100 | } 101 | 102 | def countEnabledCustomersWithNoEnabledContacts(customers : List[Customer], 103 | sum : Integer) : Integer = { 104 | if(customers.isEmpty) { 105 | sum 106 | } else { 107 | val addition = if(customers.head.enabled && 108 | customers.head.contacts.exists({ contact => contact.enabled })) { 109 | 1 110 | } else { 111 | 0 112 | } 113 | countEnabledCustomersWithNoEnabledContacts(customers.tail, addition + sum) 114 | } 115 | } 116 | 117 | def createCustomer(name : String, state : String, domain : String) : Customer = { 118 | (name, state, domain) match { 119 | case ("", _, _) => { 120 | println("Name cannot be blank") 121 | null 122 | } 123 | case (_, "", _) => { 124 | println("State cannot be blank") 125 | null 126 | } 127 | case (_, _, "") => { 128 | println("Domain cannot be blank") 129 | null 130 | } 131 | case _ => new Customer( 132 | 0, 133 | name, 134 | state, 135 | domain, 136 | true, 137 | new Contract(Calendar.getInstance, true), 138 | List() 139 | ) 140 | } 141 | } 142 | } 143 | 144 | class Customer(val customer_id : Integer, 145 | val name : String, 146 | val state : String, 147 | val domain : String, 148 | val enabled : Boolean, 149 | val contract : Contract, 150 | val contacts : List[Contact]) { 151 | } 152 | -------------------------------------------------------------------------------- /Chapter8/SimpleMatches/Customer.scala: -------------------------------------------------------------------------------- 1 | import java.util.Calendar 2 | 3 | object Customer { 4 | 5 | val allCustomers = List[Customer]() 6 | 7 | def EnabledCustomer(customer : Customer) : Boolean = customer.enabled == true 8 | def DisabledCustomer(customer : Customer) : Boolean = customer.enabled == false 9 | 10 | def getDisabledCustomerNames() : List[String] = { 11 | Customer.allCustomers.filter(DisabledCustomer).map({ customer => 12 | customer.name 13 | }) 14 | } 15 | 16 | def getEnabledCustomerStates() : List[String] = { 17 | Customer.allCustomers.filter(EnabledCustomer).map({ customer => 18 | customer.state 19 | }) 20 | } 21 | 22 | def getEnabledCustomerDomains() : List[String] = { 23 | Customer.allCustomers.filter(EnabledCustomer).map({ customer => 24 | customer.domain 25 | }) 26 | } 27 | 28 | def getEnabledCustomerSomeoneEmail(someone : String) : List[String] = { 29 | Customer.allCustomers.filter(EnabledCustomer).map({ customer => 30 | someone + "@" + customer.domain 31 | }) 32 | } 33 | 34 | def getCustomerById(inList : List[Customer], 35 | customer_id : Integer) : List[Customer] = { 36 | inList.filter(customer => customer.customer_id == customer_id) 37 | } 38 | 39 | def eachEnabledContact(cls : Contact => Unit) { 40 | Customer.allCustomers.filter({ customer => 41 | customer.enabled && customer.contract.enabled 42 | }).foreach({ customer => 43 | customer.contacts.foreach(cls) 44 | }) 45 | } 46 | 47 | def updateCustomerByIdList(initialIds : List[Customer], 48 | ids : List[Integer], 49 | cls : Customer => Customer) : List[Customer] = { 50 | if(ids.size <= 0) { 51 | initialIds 52 | } else if(initialIds.size <= 0) { 53 | List() 54 | } else { 55 | val precust = initialIds.find(cust => cust.customer_id == ids(0)) 56 | val cust = if(precust.isEmpty) { List() } else { List(cls(precust.get)) } 57 | cust ::: updateCustomerByIdList( 58 | initialIds.filter(cust => cust.customer_id == ids(0)), 59 | ids.drop(1), 60 | cls 61 | ) 62 | } 63 | } 64 | 65 | def updateContactForCustomerContact(customer_id : Integer, 66 | contact_id : Integer, 67 | cls : Contact => Contact) : List[Customer] = { 68 | updateCustomerByIdList(Customer.allCustomers, List(customer_id), { customer => 69 | new Customer( 70 | customer.customer_id, 71 | customer.name, 72 | customer.state, 73 | customer.domain, 74 | customer.enabled, 75 | customer.contract, 76 | customer.contacts.map { contact => 77 | if(contact.contact_id == contact_id) { 78 | cls(contact) 79 | } else { 80 | contact 81 | } 82 | } 83 | ) 84 | }) 85 | } 86 | 87 | def updateContractForCustomerList(ids : List[Integer], 88 | cls : Contract => Contract) : List[Customer] = { 89 | updateCustomerByIdList(Customer.allCustomers, ids, { customer => 90 | new Customer( 91 | customer.customer_id, 92 | customer.name, 93 | customer.state, 94 | customer.domain, 95 | customer.enabled, 96 | cls(customer.contract), 97 | customer.contacts 98 | ) 99 | }) 100 | } 101 | 102 | def countEnabledCustomersWithNoEnabledContacts(customers : List[Customer], 103 | sum : Integer) : Integer = { 104 | if(customers.isEmpty) { 105 | sum 106 | } else { 107 | val addition = if(customers.head.enabled && 108 | customers.head.contacts.exists({ contact => contact.enabled })) { 109 | 1 110 | } else { 111 | 0 112 | } 113 | countEnabledCustomersWithNoEnabledContacts(customers.tail, addition + sum) 114 | } 115 | } 116 | 117 | def createCustomer(name : String, state : String, domain : String) : Customer = { 118 | name match { 119 | case "" => { 120 | println("Name cannot be blank") 121 | null 122 | } 123 | case _ => state match { 124 | case "" => { 125 | println("State cannot be blank") 126 | null 127 | } 128 | case _ => domain match { 129 | case "" => { 130 | println("Domain cannot be blank") 131 | null 132 | } 133 | case _ => new Customer( 134 | 0, 135 | name, 136 | state, 137 | domain, 138 | true, 139 | new Contract(Calendar.getInstance, true), 140 | List() 141 | ) 142 | } 143 | } 144 | } 145 | } 146 | } 147 | 148 | class Customer(val customer_id : Integer, 149 | val name : String, 150 | val state : String, 151 | val domain : String, 152 | val enabled : Boolean, 153 | val contract : Contract, 154 | val contacts : List[Contact]) { 155 | } 156 | -------------------------------------------------------------------------------- /Chapter8/ExtractingLists/Customer.scala: -------------------------------------------------------------------------------- 1 | import java.util.Calendar 2 | 3 | object Customer { 4 | 5 | val allCustomers = List[Customer]() 6 | 7 | def EnabledCustomer(customer : Customer) : Boolean = customer.enabled == true 8 | def DisabledCustomer(customer : Customer) : Boolean = customer.enabled == false 9 | 10 | def getDisabledCustomerNames() : List[String] = { 11 | Customer.allCustomers.filter(DisabledCustomer).map({ customer => 12 | customer.name 13 | }) 14 | } 15 | 16 | def getEnabledCustomerStates() : List[String] = { 17 | Customer.allCustomers.filter(EnabledCustomer).map({ customer => 18 | customer.state 19 | }) 20 | } 21 | 22 | def getEnabledCustomerDomains() : List[String] = { 23 | Customer.allCustomers.filter(EnabledCustomer).map({ customer => 24 | customer.domain 25 | }) 26 | } 27 | 28 | def getEnabledCustomerSomeoneEmail(someone : String) : List[String] = { 29 | Customer.allCustomers.filter(EnabledCustomer).map({ customer => 30 | someone + "@" + customer.domain 31 | }) 32 | } 33 | 34 | def getCustomerById(inList : List[Customer], 35 | customer_id : Integer) : List[Customer] = { 36 | inList.filter(customer => customer.customer_id == customer_id) 37 | } 38 | 39 | def eachEnabledContact(cls : Contact => Unit) { 40 | Customer.allCustomers.filter({ customer => 41 | customer.enabled && customer.contract.enabled 42 | }).foreach({ customer => 43 | customer.contacts.foreach(cls) 44 | }) 45 | } 46 | 47 | def updateCustomerByIdList(initialIds : List[Customer], 48 | ids : List[Integer], 49 | cls : Customer => Customer) : List[Customer] = { 50 | (initialIds, ids) match { 51 | case (List(), _) => initialIds 52 | case (_, List()) => initialIds 53 | case (_, id :: tailIds) => { 54 | initialIds.find(cust => cust.customer_id == id) match { 55 | case None => updateCustomerByIdList(initialIds, tailIds, cls) 56 | case Some(cust) => updateCustomerByIdList( 57 | initialIds.filter(cust => cust.customer_id == id), 58 | tailIds, 59 | cls 60 | ) 61 | } 62 | } 63 | } 64 | } 65 | 66 | def updateContactForCustomerContact(customer_id : Integer, 67 | contact_id : Integer, 68 | cls : Contact => Contact) : List[Customer] = { 69 | updateCustomerByIdList(Customer.allCustomers, List(customer_id), { customer => 70 | new Customer( 71 | customer.customer_id, 72 | customer.name, 73 | customer.state, 74 | customer.domain, 75 | customer.enabled, 76 | customer.contract, 77 | customer.contacts.map { contact => 78 | if(contact.contact_id == contact_id) { 79 | cls(contact) 80 | } else { 81 | contact 82 | } 83 | } 84 | ) 85 | }) 86 | } 87 | 88 | def updateContractForCustomerList(ids : List[Integer], 89 | cls : Contract => Contract) : List[Customer] = { 90 | updateCustomerByIdList(Customer.allCustomers, ids, { customer => 91 | new Customer( 92 | customer.customer_id, 93 | customer.name, 94 | customer.state, 95 | customer.domain, 96 | customer.enabled, 97 | cls(customer.contract), 98 | customer.contacts 99 | ) 100 | }) 101 | } 102 | 103 | def countEnabledCustomersWithNoEnabledContacts(customers : List[Customer], 104 | sum : Integer) : Integer = { 105 | if(customers.isEmpty) { 106 | sum 107 | } else { 108 | val addition = if(customers.head.enabled && 109 | customers.head.contacts.exists({ contact => contact.enabled })) { 110 | 1 111 | } else { 112 | 0 113 | } 114 | countEnabledCustomersWithNoEnabledContacts(customers.tail, addition + sum) 115 | } 116 | } 117 | 118 | def createCustomer(name : String, state : String, domain : String) : Customer = { 119 | (name, state, domain) match { 120 | case ("", _, _) => { 121 | println("Name cannot be blank") 122 | null 123 | } 124 | case (_, "", _) => { 125 | println("State cannot be blank") 126 | null 127 | } 128 | case (_, _, "") => { 129 | println("Domain cannot be blank") 130 | null 131 | } 132 | case _ => new Customer( 133 | 0, 134 | name, 135 | state, 136 | domain, 137 | true, 138 | new Contract(Calendar.getInstance, true), 139 | List() 140 | ) 141 | } 142 | } 143 | } 144 | 145 | class Customer(val customer_id : Integer, 146 | val name : String, 147 | val state : String, 148 | val domain : String, 149 | val enabled : Boolean, 150 | val contract : Contract, 151 | val contacts : List[Contact]) { 152 | } 153 | -------------------------------------------------------------------------------- /Chapter8/ExtractingObjects/Customer.scala: -------------------------------------------------------------------------------- 1 | import java.util.Calendar 2 | 3 | object Customer { 4 | 5 | val allCustomers = List[Customer]() 6 | 7 | def EnabledCustomer(customer : Customer) : Boolean = customer.enabled == true 8 | def DisabledCustomer(customer : Customer) : Boolean = customer.enabled == false 9 | 10 | def getDisabledCustomerNames() : List[String] = { 11 | Customer.allCustomers.filter(DisabledCustomer).map({ customer => 12 | customer.name 13 | }) 14 | } 15 | 16 | def getEnabledCustomerStates() : List[String] = { 17 | Customer.allCustomers.filter(EnabledCustomer).map({ customer => 18 | customer.state 19 | }) 20 | } 21 | 22 | def getEnabledCustomerDomains() : List[String] = { 23 | Customer.allCustomers.filter(EnabledCustomer).map({ customer => 24 | customer.domain 25 | }) 26 | } 27 | 28 | def getEnabledCustomerSomeoneEmail(someone : String) : List[String] = { 29 | Customer.allCustomers.filter(EnabledCustomer).map({ customer => 30 | someone + "@" + customer.domain 31 | }) 32 | } 33 | 34 | def getCustomerById(inList : List[Customer], 35 | customer_id : Integer) : List[Customer] = { 36 | inList.filter(customer => customer.customer_id == customer_id) 37 | } 38 | 39 | def eachEnabledContact(cls : Contact => Unit) { 40 | Customer.allCustomers.filter({ customer => 41 | customer.enabled && customer.contract.enabled 42 | }).foreach({ customer => 43 | customer.contacts.foreach(cls) 44 | }) 45 | } 46 | 47 | def updateCustomerByIdList(initialIds : List[Customer], 48 | ids : List[Integer], 49 | cls : Customer => Customer) : List[Customer] = { 50 | (initialIds, ids) match { 51 | case (List(), _) => initialIds 52 | case (_, List()) => initialIds 53 | case (_, id :: tailIds) => { 54 | val precust = initialIds.find(cust => cust.customer_id == id) 55 | precust match { 56 | case None => updateCustomerByIdList(initialIds, tailIds, cls) 57 | case Some(cust) => updateCustomerByIdList( 58 | initialIds.filter(cust => cust.customer_id == id), 59 | tailIds, 60 | cls 61 | ) 62 | } 63 | } 64 | } 65 | } 66 | 67 | def updateContactForCustomerContact(customer_id : Integer, 68 | contact_id : Integer, 69 | cls : Contact => Contact) : List[Customer] = { 70 | updateCustomerByIdList(Customer.allCustomers, List(customer_id), { customer => 71 | new Customer( 72 | customer.customer_id, 73 | customer.name, 74 | customer.state, 75 | customer.domain, 76 | customer.enabled, 77 | customer.contract, 78 | customer.contacts.map { contact => 79 | if(contact.contact_id == contact_id) { 80 | cls(contact) 81 | } else { 82 | contact 83 | } 84 | } 85 | ) 86 | }) 87 | } 88 | 89 | def updateContractForCustomerList(ids : List[Integer], 90 | cls : Contract => Contract) : List[Customer] = { 91 | updateCustomerByIdList(Customer.allCustomers, ids, { customer => 92 | new Customer( 93 | customer.customer_id, 94 | customer.name, 95 | customer.state, 96 | customer.domain, 97 | customer.enabled, 98 | cls(customer.contract), 99 | customer.contacts 100 | ) 101 | }) 102 | } 103 | 104 | def countEnabledCustomersWithNoEnabledContacts(customers : List[Customer], 105 | sum : Integer) : Integer = { 106 | if(customers.isEmpty) { 107 | sum 108 | } else { 109 | val addition = if(customers.head.enabled && 110 | customers.head.contacts.exists({ contact => contact.enabled })) { 111 | 1 112 | } else { 113 | 0 114 | } 115 | countEnabledCustomersWithNoEnabledContacts(customers.tail, addition + sum) 116 | } 117 | } 118 | 119 | def createCustomer(name : String, state : String, domain : String) : Customer = { 120 | (name, state, domain) match { 121 | case ("", _, _) => { 122 | println("Name cannot be blank") 123 | null 124 | } 125 | case (_, "", _) => { 126 | println("State cannot be blank") 127 | null 128 | } 129 | case (_, _, "") => { 130 | println("Domain cannot be blank") 131 | null 132 | } 133 | case _ => new Customer( 134 | 0, 135 | name, 136 | state, 137 | domain, 138 | true, 139 | new Contract(Calendar.getInstance, true), 140 | List() 141 | ) 142 | } 143 | } 144 | } 145 | 146 | class Customer(val customer_id : Integer, 147 | val name : String, 148 | val state : String, 149 | val domain : String, 150 | val enabled : Boolean, 151 | val contract : Contract, 152 | val contacts : List[Contact]) { 153 | } 154 | -------------------------------------------------------------------------------- /Chapter8/Conclusion/Customer.scala: -------------------------------------------------------------------------------- 1 | import java.util.Calendar 2 | 3 | object Customer { 4 | 5 | val allCustomers = List[Customer]() 6 | 7 | def EnabledCustomer(customer : Customer) : Boolean = customer.enabled == true 8 | def DisabledCustomer(customer : Customer) : Boolean = customer.enabled == false 9 | 10 | def getDisabledCustomerNames() : List[String] = { 11 | Customer.allCustomers.filter(DisabledCustomer).map({ customer => 12 | customer.name 13 | }) 14 | } 15 | 16 | def getEnabledCustomerStates() : List[String] = { 17 | Customer.allCustomers.filter(EnabledCustomer).map({ customer => 18 | customer.state 19 | }) 20 | } 21 | 22 | def getEnabledCustomerDomains() : List[String] = { 23 | Customer.allCustomers.filter(EnabledCustomer).map({ customer => 24 | customer.domain 25 | }) 26 | } 27 | 28 | def getEnabledCustomerSomeoneEmail(someone : String) : List[String] = { 29 | Customer.allCustomers.filter(EnabledCustomer).map({ customer => 30 | someone + "@" + customer.domain 31 | }) 32 | } 33 | 34 | def getCustomerById(inList : List[Customer], 35 | customer_id : Integer) : List[Customer] = { 36 | inList.filter(customer => customer.customer_id == customer_id) 37 | } 38 | 39 | def eachEnabledContact(cls : Contact => Unit) { 40 | Customer.allCustomers.filter({ customer => 41 | customer.enabled && customer.contract.enabled 42 | }).foreach({ customer => 43 | customer.contacts.foreach(cls) 44 | }) 45 | } 46 | 47 | def updateCustomerByIdList(initialIds : List[Customer], 48 | ids : List[Integer], 49 | cls : Customer => Customer) : List[Customer] = { 50 | (initialIds, ids) match { 51 | case (List(), _) => initialIds 52 | case (_, List()) => initialIds 53 | case (_, id :: tailIds) => { 54 | val precust = initialIds.find(cust => cust.customer_id == id) 55 | precust match { 56 | case None => updateCustomerByIdList(initialIds, tailIds, cls) 57 | case Some(cust) => updateCustomerByIdList( 58 | initialIds.filter(cust => cust.customer_id == id), 59 | tailIds, 60 | cls 61 | ) 62 | } 63 | } 64 | } 65 | } 66 | 67 | def updateContactForCustomerContact(customer_id : Integer, 68 | contact_id : Integer, 69 | cls : Contact => Contact) : List[Customer] = { 70 | updateCustomerByIdList(Customer.allCustomers, List(customer_id), { customer => 71 | new Customer( 72 | customer.customer_id, 73 | customer.name, 74 | customer.state, 75 | customer.domain, 76 | customer.enabled, 77 | customer.contract, 78 | customer.contacts.map { contact => 79 | if(contact.contact_id == contact_id) { 80 | cls(contact) 81 | } else { 82 | contact 83 | } 84 | } 85 | ) 86 | }) 87 | } 88 | 89 | def updateContractForCustomerList(ids : List[Integer], 90 | cls : Contract => Contract) : List[Customer] = { 91 | updateCustomerByIdList(Customer.allCustomers, ids, { customer => 92 | new Customer( 93 | customer.customer_id, 94 | customer.name, 95 | customer.state, 96 | customer.domain, 97 | customer.enabled, 98 | cls(customer.contract), 99 | customer.contacts 100 | ) 101 | }) 102 | } 103 | 104 | def countEnabledCustomersWithNoEnabledContacts(customers : List[Customer], 105 | sum : Integer) : Integer = { 106 | customers match { 107 | case List() => sum 108 | case Customer(_,_,_,_,true,_,List()) :: custs => 109 | countEnabledCustomersWithNoEnabledContacts(custs, sum) 110 | case Customer(_,_,_,_,true,_,cont) :: custs if cont.exists({ contact => 111 | contact.enabled}) => countEnabledCustomersWithNoEnabledContacts(custs, sum + 1) 112 | case cust :: custs => countEnabledCustomersWithNoEnabledContacts(custs, sum) 113 | } 114 | } 115 | 116 | def createCustomer(name : String, state : String, domain : String) : Option[Customer] = { 117 | def error(message : String) : Option[Customer] = { 118 | println(message) 119 | None 120 | } 121 | (name, state, domain) match { 122 | case ("", _, _) => error("Name cannot be blank") 123 | case (_, "", _) => error("State cannot be blank") 124 | case (_, _, "") => error("Domain cannot be blank") 125 | case _ => new Some(new Customer( 126 | 0, 127 | name, 128 | state, 129 | domain, 130 | true, 131 | new Contract(Calendar.getInstance, true), 132 | List() 133 | ) 134 | ) 135 | } 136 | } 137 | } 138 | 139 | case class Customer(val customer_id : Integer, 140 | val name : String, 141 | val state : String, 142 | val domain : String, 143 | val enabled : Boolean, 144 | val contract : Contract, 145 | val contacts : List[Contact]) { 146 | } 147 | -------------------------------------------------------------------------------- /Chapter9/CodeAsData/Customer.scala: -------------------------------------------------------------------------------- 1 | import java.util.Calendar 2 | 3 | object Customer { 4 | 5 | var allCustomers = List[Customer]() 6 | 7 | def EnabledCustomer(customer : Customer) : Boolean = customer.enabled == true 8 | def DisabledCustomer(customer : Customer) : Boolean = customer.enabled == false 9 | 10 | def getDisabledCustomerNames() : List[String] = { 11 | Customer.allCustomers.filter(DisabledCustomer).map({ customer => 12 | customer.name 13 | }) 14 | } 15 | 16 | def getEnabledCustomerStates() : List[String] = { 17 | Customer.allCustomers.filter(EnabledCustomer).map({ customer => 18 | customer.state 19 | }) 20 | } 21 | 22 | def getEnabledCustomerDomains() : List[String] = { 23 | Customer.allCustomers.filter(EnabledCustomer).map({ customer => 24 | customer.domain 25 | }) 26 | } 27 | 28 | def getEnabledCustomerSomeoneEmail(someone : String) : List[String] = { 29 | Customer.allCustomers.filter(EnabledCustomer).map({ customer => 30 | someone + "@" + customer.domain 31 | }) 32 | } 33 | 34 | def getCustomerById(inList : List[Customer], 35 | customer_id : Integer) : List[Customer] = { 36 | inList.filter(customer => customer.customer_id == customer_id) 37 | } 38 | 39 | def eachEnabledContact(cls : Contact => Unit) { 40 | Customer.allCustomers.filter({ customer => 41 | customer.enabled && customer.contract.enabled 42 | }).foreach({ customer => 43 | customer.contacts.foreach(cls) 44 | }) 45 | } 46 | 47 | def updateCustomerByIdList(initialIds : List[Customer], 48 | ids : List[Integer], 49 | cls : Customer => Customer) : List[Customer] = { 50 | (initialIds, ids) match { 51 | case (List(), _) => initialIds 52 | case (_, List()) => initialIds 53 | case (_, id :: tailIds) => { 54 | val precust = initialIds.find(cust => cust.customer_id == id) 55 | precust match { 56 | case None => updateCustomerByIdList(initialIds, tailIds, cls) 57 | case Some(cust) => updateCustomerByIdList( 58 | initialIds.filter(cust => cust.customer_id == id), 59 | tailIds, 60 | cls 61 | ) 62 | } 63 | } 64 | } 65 | } 66 | 67 | def updateContactForCustomerContact(customer_id : Integer, 68 | contact_id : Integer, 69 | cls : Contact => Contact) : List[Customer] = { 70 | updateCustomerByIdList(Customer.allCustomers, List(customer_id), { customer => 71 | new Customer( 72 | customer.customer_id, 73 | customer.name, 74 | customer.state, 75 | customer.domain, 76 | customer.enabled, 77 | customer.contract, 78 | customer.contacts.map { contact => 79 | if(contact.contact_id == contact_id) { 80 | cls(contact) 81 | } else { 82 | contact 83 | } 84 | } 85 | ) 86 | }) 87 | } 88 | 89 | def updateContractForCustomerList(ids : List[Integer], 90 | cls : Contract => Contract) : List[Customer] = { 91 | updateCustomerByIdList(Customer.allCustomers, ids, { customer => 92 | new Customer( 93 | customer.customer_id, 94 | customer.name, 95 | customer.state, 96 | customer.domain, 97 | customer.enabled, 98 | cls(customer.contract), 99 | customer.contacts 100 | ) 101 | }) 102 | } 103 | 104 | def countEnabledCustomersWithNoEnabledContacts(customers : List[Customer], 105 | sum : Integer) : Integer = { 106 | customers match { 107 | case List() => sum 108 | case Customer(_,_,_,_,true,_,List()) :: custs => 109 | countEnabledCustomersWithNoEnabledContacts(custs, sum) 110 | case Customer(_,_,_,_,true,_,cont) :: custs if cont.exists({ contact => 111 | contact.enabled}) => countEnabledCustomersWithNoEnabledContacts(custs, sum + 1) 112 | case cust :: custs => countEnabledCustomersWithNoEnabledContacts(custs, sum) 113 | } 114 | } 115 | 116 | def createCustomer(name : String, state : String, domain : String) : Option[Customer] = { 117 | def error(message : String) : Option[Customer] = { 118 | println(message) 119 | None 120 | } 121 | (name, state, domain) match { 122 | case ("", _, _) => error("Name cannot be blank") 123 | case (_, "", _) => error("State cannot be blank") 124 | case (_, _, "") => error("Domain cannot be blank") 125 | case _ => new Some(new Customer( 126 | 0, 127 | name, 128 | state, 129 | domain, 130 | true, 131 | new Contract(Calendar.getInstance, true), 132 | List() 133 | ) 134 | ) 135 | } 136 | } 137 | 138 | def createCustomer() : Unit = { 139 | Customer.allCustomers = Customer.createCustomer( 140 | CommandLine.askForInput("Name"), 141 | CommandLine.askForInput("State"), 142 | CommandLine.askForInput("Domain") 143 | ).toList ::: Customer.allCustomers 144 | } 145 | 146 | def list() : Unit = { 147 | Customer.allCustomers.foreach(customer => println(customer)) 148 | } 149 | 150 | /*def addContact() = { 151 | val id = CommandLine.askForInput("Customer ID to add contact to").toInt 152 | Customer.allCustomers = Customer.updateCustomerByIdList(Customer.allCustomers, List(id), customer => 153 | println("Got customer " + customer.toString) 154 | new Customer( 155 | customer.customer_id, 156 | customer.name, 157 | customer.state, 158 | customer.domain, 159 | customer.enabled, 160 | customer.contract, 161 | Contact.createContact() :: customer.contacts 162 | ) 163 | ) 164 | }*/ 165 | } 166 | 167 | case class Customer(val customer_id : Integer, 168 | val name : String, 169 | val state : String, 170 | val domain : String, 171 | val enabled : Boolean, 172 | val contract : Contract, 173 | val contacts : List[Contact]) { 174 | } 175 | --------------------------------------------------------------------------------