0%

Coursera-Programming Language Week4

In this week's class, we start to touch some core concept of Functional Programming – First class function, closure, anonymous function and currying. Right now such concepts are quite popular among programing language. Not only some new rising language like Rust, Go, Kotlin and Dart, but also languages like Java, Python and C++ start to support the Functional Programing.

Back to this course, the first concept is First class function, this concept is that we can allow function act as a return value, parameter or a variable. From my understanding, the first class function is the foundation of the whole Functional Programming. In Javascript, we can do:

let add = function(a,b) {
return a + b
}

Also, when we talk about the first class function, we also come up with the concept of currying. Currying from my understanding is to make a function that requires multi-parameters into as function that takes one of the parameters and return another function takes one another parameter. Such a method is helpful when we do some job that reuires us to calculate the partial result first and then put another part into the expression.

let add = (a) => { 
return (b) => {
return a+b
}
}

Above code, also shows another concept that is anonymous function and it is a thing when we only need to use a function once and we do not want to dirty the whole namespace.

Another interesting concept is the closure, it is kind of not so cursical in the standard ml while in language like Javascript that not follows the lexical scope. In such case, we need closure to protect the local namespce from being modified.