First-class functions are functions treated as objects themselves, meaning we can pass a function as a parameter to another function, return a function from a function, or store a function in a variable.
– Becoming Functional
一级函数 高阶函数?
First-Class Function (暂时不知道怎么样翻译较为妥当) 或者 High Order Function 是被视作为对象的函数,也就是说本质上还是一个对象,因此可以作为另一个函数的参数或者在一个函数中返回一个函数,或者将一个函数保存在一个对象中。
The DRY (Don’t Repeat Yourself) principle has been around for many years; the concept is that we should not be duplicating lines of code. This makes the code harder to maintain. But why is that the case? Think about what happens if you duplicate a function multiple times. Now imagine that you just found a bug in one of those functions; you’ll have to go through however many other functions to see if that same bug exists.
Closures are much like lambdas, except they reference variables outside the scope of the function. The body references a variable that doesn’t exist in either the body or the parameter list.
Closure 和 lambda function 很像,但区别在于它们引用了函数范围以外的变量,这个变量既不存在参数列表中,也不存在函数体中。
defcreateCustomer(name : String, state : String, domain : String) : Option[Customer] = { deferror(message : String) : Option[Customer] = { println(message) None } (name, state, domain) match { case ("", _, _) => error("Name cannot be blank") case (_, "", _) => error("State cannot be blank") case (_, _, "") => error("Domain cannot be blank") case _ => newSome(newCustomer( 0, name, state, domain, true, newContract(Calendar.getInstance, true), List() ) ) } }
用 case 关键字修饰类
1 2 3 4 5 6 7 8
caseclassCustomer(val customer_id : Integer, val name : String, val state : String, val domain : String, val enabled : Boolean, val contract : Contract, val contacts : List[Contact]) { }
1 2 3 4 5 6 7 8 9 10 11 12
defcountEnabledCustomersWithNoEnabledContacts(customers : List[Customer], sum : Integer) : Integer = { customers match { caseList() => sum case cust :: custs => { if (cust.enabled && cust.contacts.exists({ contact => contact.enabled})) countEnabledCustomersWithNoEnabledContacts(custs, sum + 1) else countEnabledCustomersWithNoEnabledContacts(custs, sum) } } }
1 2 3 4 5 6 7 8 9 10 11
defcountEnabledCustomersWithNoEnabledContacts(customers : List[Customer], sum : Integer) : Integer = { customers match { caseList() => sum caseCustomer(_,_,_,_,true,_,cont) :: custs if cont.exists({ contact => contact.enabled}) => countEnabledCustomersWithNoEnabledContacts(custs, sum + 1) case cust :: custs => countEnabledCustomersWithNoEnabledContacts(custs, sum) } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14
defcountEnabledCustomersWithNoEnabledContacts(customers : List[Customer], sum : Integer) : Integer = { customers match { caseList() => sum caseCustomer(_,_,_,_,true,_,List()) :: custs => countEnabledCustomersWithNoEnabledContacts(custs, sum) caseCustomer(_,_,_,_,true,_,cont) :: custs if cont.exists({ contact => contact.enabled}) => countEnabledCustomersWithNoEnabledContacts(custs, sum + 1) case cust :: custs => countEnabledCustomersWithNoEnabledContacts(custs, sum) } }