March 2019
Intermediate to advanced
208 pages
5h 11m
English
The simplest form of collection is the tuple, which consists of a series of values in parentheses. The classic example of a tuple is for use as a pair of (x, y) coordinates:
| | type coord = (float, float); |
| | |
| | let distance = (p0: coord, p1: coord) : float => { |
| | let (x0, y0) = p0; |
| | let (x1, y1) = p1; |
| | sqrt((x0 -. x1) ** 2.0 +. (y0 -. y1) ** 2.0) |
| | }; |
| | |
| | let startPoint = (3.5, 4.6); |
| | let endPoint = (0.5, 9.6); |
| | let result = distance(startPoint, endPoint); |
| | Js.log(result); /* output: 5.830951894845301 */ |
Pay special attention to the let bindings in the distance function. They use destructuring to extract ...
Read now
Unlock full access