type Circle = {x: number; y: number; r: number};
type Rectangle = {x: number; y: number; w: number; h: number};
type Shape = <T>(xs: {
circle: (args: Circle) => T;
rectangle: (args: Rectangle) => T;
}) => T;
function Circle(x: Circle): Shape {
return ({circle}) => circle(x);
}
function Rectangle(x: Rectangle): Shape {
return ({rectangle}) => rectangle(x);
}
const exampleCircle = Circle({x: 2, y: 1.4, r: 4.5});
const exampleRectangle = Rectangle({x: 1.3, y: 3.1, w: 10.3, h: 7.7});
const area = (shape: Shape): number =>
shape({
circle: ({r}: Circle) => Math.PI * r * r,
rectangle: ({w, h}: Rectangle) => w * h,
});
console.log(area(exampleCircle));
console.log(area(exampleRectangle));
/edit: Fixed formatting.
/edit: Clarify something.
I've been using (neo)vim for years, and don't want to use anything else. It isn't just about productivity (I would probably be as productive in an IDE). It's because its _mine_. It's a highly customized environment, catered to my needs.
[1] https://www.youtube.com/watch?v=QMVIJhC9Veg&