Ahmad Atallah
22 Apr 2020
•
2 min read
In this article I will solve a problem called Pascal's Triangle using one of the pattern matching techniques used in TypeScript. Pascal's triangle is an infinite triangle where the numbers at the edges of the triangle are all 1s, each number inside the triangle is the sum of the two numbers above it, and each number outside the edges is zero or not exist. I said infinite because it is increasingly growing on the limit you want it to be applied.
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
...
Pascal's Triangle
Defining a function that takes row: number
and column: number
as an arguments and
based on these argument values, it recursivly match to get our pascal value.
For example:
pascal(1, 0);
// expected result = 1
pascal(3, 4);
// expected result = null
pascal(3, 1);
// expected result = 3
What we want exactly to pattern match four main categories:
0
and row is whatever.therefor we will create EdgesPattern
which will define these categories:
interface EdgesPattern {
LeftEdge: () => number;
RightEdge: () => number;
BeyondEdge: () => number;
}
This interface will guarantee for us handling each case in pascal's triangle. It also allows separation of concerns; separating behaviour of edges categorization from the actual logic of the problem we want to solve, e.g., our pascal's triangle problem.
So if we want to define PascalTriangles
class that declare pascal's triangle logic it will be something like this:
class PascalEdges implements EdgesPattern {
constructor() {
// empty constructor
}
LeftEdge() {
return 1;
}
RightEdge() {
return 1;
}
BeyondEdge() {
return null;
}
}
We will use the previous class to define pascal's triangle logic.
This will be done by creating a wrapper function that takes an argument of the type PascalEdges
, our main concern.
this function will return a function of actual logic applied to solve pascal's triangle.
function pascal(p: PascalEdges): (row: number, column: number) => number {
const recursePascal = (row: number, column: number): number => {
if (column === 0) {
return p.LeftEdge();
} else if (column === row) {
return p.RightEdge();
} else if (column > row) {
return p.BeyondEdge();
}
return recursePasal(column, row - 1) + recursePasal(column - 1, row - 1);
};
return recursePascal;
}
At the end, to apply the above code blocks all together, we will use function currying to get the expected result.
For example to evaluate pascal(3, 2), we will create an instance of PascalEdges
, pass it to the pascal wrapper
function and curry the result function to be invoked with the actual logic arguments, which will be column
and row
in our case.
const pascal_3_2 = pascal(new PascalEdges())(3, 2);
// expected result = 3
Source code available on Github
Article Link: https://syncatallah.cc/writings/pascal-typescript-example
Ahmad Atallah
See other articles by Ahmad
Ground Floor, Verse Building, 18 Brunswick Place, London, N1 6DZ
108 E 16th Street, New York, NY 10003
Join over 111,000 others and get access to exclusive content, job opportunities and more!