Inheritance and composition

The modern JavaScript with the ES6 syntax and the rise of the popularity like ReactJS, functional programming becomes more and more common. When using React, one of the common practice is to use composition instead of inheritance.

Because I started learning programming when the OOP was the most prevailing paradigm, I was trained to solve the problem by using OOP concepts like polymorphoism, inheritance, encapsulation, etc.

I think JS is the most interesting programming language in the modern technology. It supports server-side and client-side development. With the ES6, it supports OOP keywords like class and also using FP (functional programming) syntax like fat arrow (=>).

In OOP, the most common usage is the inheritance and polymorphoism. The following is an example of inheritance in JS,

class Shape {
  constructor(w, h) {
    this.width = w;
    this.height = h;
  }
  area() {
    return this.width * this.height;
  }
}

class Rectangle extends Shape {
  constructor(w, h) {
    super(w, h);
  }
}

class Triangle extends Shape {
  constructor(w, h) {
    super(w, h);
  }
  area() {
    return super.area() / 2;
  }
}

function main() {
  const rectangle = new Rectangle(4, 5);
  const triangle = new Triangle(4, 5);
  console.log('Rectangle area: ', rectangle.area());
  console.log('Triangle area: ', triangle.area());
}

main();

The shape area calculation can be re-written to composition instead of inheritance as followings,

class Rectangle {
  constructor(w, h) {
    this.width = w;
    this.height = h;
  }
  area() {
    return this.width * this.height;
  }
}

class Triangle {
  constructor(w, h) {
    this.width = w;
    this.height = h;
  }
  area() {
    const rect = new Rectangle(this.width, this.height);
    return rect.area() / 2;
  }
}

Therefore, Rectangle and Triangle do not inherit from Shape. In fact, Triangle uses Rectangle to calculate the area. This is the object composition, and it is same as the way of composition in React. Furthermore, one of the greatest features of JS is closure. This allows React to pass a function with specific logic as a parameter to a generic component. Thus, the generic component can be designed without the prior knowledge of the business/application logic. This will produce a result similar to method override in OOP.

Moreover, the object composition can be re-written to function composition as FP.

const rectangleArea = (w, h) => w * h; // In math, f(x,y) = x * y
const halving = (area) => area / 2; // In math, g(x) = x / 2
const triangleArea = (w, h) => halving(rectangleArea(w, h)); // In math, h(x,y) = g(f(x,y)) = f(x,y) / 2

function main() {
  console.log('Rectangle area: ', rectangleArea(4, 5));
  console.log('Triangle area: ', triangleArea(4, 5));
}

main();

Statistics and functional programming languages

Recently, I feel fervent to learn functional programming, because i) (in my opinion), it will become a trend, and ii) the interpreter can be used as an advanced calculator.

Since I am teaching Statistics, I want to do some calculation of the normal distribution probability.

Before I begin, I need to mention, in order to calculate the normal distribution probability something like P(x < X), this can be done by using a spreadsheet software with NORMDIST() and NORM.INV() for the inverse of the former function.

Spreadsheet is good for calculation, but not good as a calculator. My primary calculator is SpeedCrunch, which allows entering expression. But the drawback of SpeedCrunch is the lack of statistical functions.

Therefore, the functional programming comes to my mind.

Firstly, let me introduce the usage of Python. Make sure SciPy is installed. Run the Python interpreter,

from scipy.stats import norm
norm.cdf(my_value)
norm.ppf(my_probability)

So, the two functions are norm.cdf() (cumulative distribution function) and norm.ppf() (percent point function).

Now, let me introduce the usage of R language.

pnorm(my_value)
qnorm(my_probability)

R language is used for statistics, thus, no further module or library is required.

Both Python and R languages are not pure functional programmings. I wanted to try Emacs Lisp, but the arithmetic syntax is not convenient. The syntax is using Polish notation. In order to perform arithmetic calculation,

(* 2 (+ 3 6))

Therefore, I tried the pure functional programming language, that is, Haskell.

In order to calculate the normal distribution probability, the Statistics module is required. After installation,

import Statistics.Distribution
import Statistics.Distribution.Normal
let d = normalDistr 0 1
cumulative d 0
quantile d 0.5

“normalDistr 0 1” is to create a normal distribution with mean 0 and standard deviation 1. Then “cumulative d 0” is the calculation of CDF (cumulative distribution function), which produces 0.5. And “quantile” is the inverse function of CDF.

So, enjoy the functional programming in mathematics and statistics.