72 lines
1.8 KiB
JavaScript
72 lines
1.8 KiB
JavaScript
import { Type } from "../type.js";
|
|
import { typedFnType } from "../structures/types.js"
|
|
import { Double } from "../primitives/types.js";
|
|
|
|
// Create nominal types
|
|
export const PointCartesian2D = { symbol: Symbol('PointCartesian2D'), params: [] };
|
|
export const PointPolar2D = { symbol: Symbol('PointPolar2D') , params: [] };
|
|
|
|
export const cart2polar = ({x, y}) => {
|
|
const r = Math.sqrt(x*x + y*y);
|
|
const θ = Math.atan(y/x);
|
|
return {r, θ};
|
|
};
|
|
|
|
export const polar2cart = ({r, θ}) => {
|
|
const x = r * Math.cos(θ);
|
|
const y = r * Math.sin(θ);
|
|
return {x, y};
|
|
}
|
|
|
|
export const translate = dx => dy => ({x, y}) => {
|
|
return {left: x+dx, right: y+dy};
|
|
}
|
|
|
|
export const rotate = dθ => ({r, θ}) => {
|
|
return {r, θ: θ+dθ};
|
|
}
|
|
|
|
export const scale = dr => ({r, θ}) => {
|
|
return {r: r+dr, θ};
|
|
}
|
|
|
|
const examplePoint = {x: 1, y: 2};
|
|
|
|
export const ModulePoint = {l:[
|
|
{i: -1 , t: Double},
|
|
{i: 2 , t: Double},
|
|
{i: examplePoint , t: PointCartesian2D},
|
|
|
|
{i: PointCartesian2D , t: Type},
|
|
{i: PointPolar2D , t: Type},
|
|
|
|
...typedFnType(cart2polar, fnType => fnType(PointCartesian2D)(PointPolar2D)),
|
|
|
|
...typedFnType(polar2cart, fnType => fnType(PointPolar2D)(PointCartesian2D)),
|
|
|
|
// Double -> Double -> PointCartesian2D -> PointCartesian2D
|
|
...typedFnType(translate, fnType =>
|
|
fnType
|
|
(Double)
|
|
(fnType
|
|
(Double)
|
|
(fnType
|
|
(PointCartesian2D)
|
|
(PointCartesian2D)))),
|
|
|
|
// Double -> PointPolar2D -> PointPolar2D
|
|
...typedFnType(rotate, fnType =>
|
|
fnType
|
|
(Double)
|
|
(fnType
|
|
(PointPolar2D)
|
|
(PointPolar2D))),
|
|
|
|
// Double -> PointPolar2D -> PointPolar2D
|
|
...typedFnType(scale, fnType =>
|
|
fnType
|
|
(Double)
|
|
(fnType
|
|
(PointPolar2D)
|
|
(PointPolar2D))),
|
|
]};
|