77 lines
2.5 KiB
JavaScript
77 lines
2.5 KiB
JavaScript
|
/**
|
||
|
* The `Ring` class is for types that support addition, multiplication, and subtraction operations.
|
||
|
*
|
||
|
* Instances must satisfy the following law in addition to the `Semiring` laws:
|
||
|
*
|
||
|
* - Additive inverse: `a - a <-> (zero - a) + a <-> zero`
|
||
|
*
|
||
|
* Adapted from https://github.com/purescript/purescript-prelude/blob/master/src/Data/Ring.purs
|
||
|
*
|
||
|
* @since 2.0.0
|
||
|
*/
|
||
|
import { getRing } from './function';
|
||
|
// -------------------------------------------------------------------------------------
|
||
|
// combinators
|
||
|
// -------------------------------------------------------------------------------------
|
||
|
/**
|
||
|
* Given a tuple of `Ring`s returns a `Ring` for the tuple
|
||
|
*
|
||
|
* @example
|
||
|
* import { tuple } from 'fp-ts/Ring'
|
||
|
* import * as N from 'fp-ts/number'
|
||
|
*
|
||
|
* const R = tuple(N.Field, N.Field, N.Field)
|
||
|
* assert.deepStrictEqual(R.add([1, 2, 3], [4, 5, 6]), [5, 7, 9])
|
||
|
* assert.deepStrictEqual(R.mul([1, 2, 3], [4, 5, 6]), [4, 10, 18])
|
||
|
* assert.deepStrictEqual(R.one, [1, 1, 1])
|
||
|
* assert.deepStrictEqual(R.sub([1, 2, 3], [4, 5, 6]), [-3, -3, -3])
|
||
|
* assert.deepStrictEqual(R.zero, [0, 0, 0])
|
||
|
*
|
||
|
* @since 2.10.0
|
||
|
*/
|
||
|
export var tuple = function () {
|
||
|
var rings = [];
|
||
|
for (var _i = 0; _i < arguments.length; _i++) {
|
||
|
rings[_i] = arguments[_i];
|
||
|
}
|
||
|
return ({
|
||
|
add: function (x, y) { return rings.map(function (R, i) { return R.add(x[i], y[i]); }); },
|
||
|
zero: rings.map(function (R) { return R.zero; }),
|
||
|
mul: function (x, y) { return rings.map(function (R, i) { return R.mul(x[i], y[i]); }); },
|
||
|
one: rings.map(function (R) { return R.one; }),
|
||
|
sub: function (x, y) { return rings.map(function (R, i) { return R.sub(x[i], y[i]); }); }
|
||
|
});
|
||
|
};
|
||
|
// -------------------------------------------------------------------------------------
|
||
|
// utils
|
||
|
// -------------------------------------------------------------------------------------
|
||
|
/**
|
||
|
* `negate x` can be used as a shorthand for `zero - x`
|
||
|
*
|
||
|
* @since 2.0.0
|
||
|
*/
|
||
|
export var negate = function (R) {
|
||
|
return function (a) {
|
||
|
return R.sub(R.zero, a);
|
||
|
};
|
||
|
};
|
||
|
// -------------------------------------------------------------------------------------
|
||
|
// deprecated
|
||
|
// -------------------------------------------------------------------------------------
|
||
|
/**
|
||
|
* Use [`tuple`](#tuple) instead.
|
||
|
*
|
||
|
* @category zone of death
|
||
|
* @since 2.0.0
|
||
|
* @deprecated
|
||
|
*/
|
||
|
export var getTupleRing = tuple;
|
||
|
/**
|
||
|
* Use [`getRing`](./function.ts.html#getring) instead.
|
||
|
*
|
||
|
* @category zone of death
|
||
|
* @since 2.0.0
|
||
|
* @deprecated
|
||
|
*/
|
||
|
export var getFunctionRing = getRing;
|