document some mathematical pattern methods
This commit is contained in:
parent
73b0a3a0ef
commit
d5a1832f31
1 changed files with 71 additions and 23 deletions
|
|
@ -467,35 +467,83 @@ export class Pattern {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assumes a numerical pattern. Returns a new pattern with all values rounded
|
||||||
|
* to the nearest integer.
|
||||||
|
* @returns Pattern
|
||||||
|
*/
|
||||||
round() {
|
round() {
|
||||||
return this._asNumber().fmap((v) => Math.round(v));
|
return this._asNumber().fmap((v) => Math.round(v));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assumes a numerical pattern. Returns a new pattern with all values set to
|
||||||
|
* their mathematical floor. E.g. `3.7` replaced with to `3`, and `-4.2`
|
||||||
|
* replaced with `-5`.
|
||||||
|
* @returns Pattern
|
||||||
|
*/
|
||||||
floor() {
|
floor() {
|
||||||
return this._asNumber().fmap((v) => Math.floor(v));
|
return this._asNumber().fmap((v) => Math.floor(v));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assumes a numerical pattern. Returns a new pattern with all values set to
|
||||||
|
* their mathematical ceiling. E.g. `3.2` replaced with `4`, and `-4.2`
|
||||||
|
* replaced with `-4`.
|
||||||
|
* @returns Pattern
|
||||||
|
*/
|
||||||
ceil() {
|
ceil() {
|
||||||
return this._asNumber().fmap((v) => Math.ceil(v));
|
return this._asNumber().fmap((v) => Math.ceil(v));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assumes a numerical pattern, containing unipolar values in the range 0 ..
|
||||||
|
* 1. Returns a new pattern with values scaled to the bipolar range -1 .. 1
|
||||||
|
* @returns Pattern
|
||||||
|
*/
|
||||||
_toBipolar() {
|
_toBipolar() {
|
||||||
return this.fmap((x) => x * 2 - 1);
|
return this.fmap((x) => x * 2 - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assumes a numerical pattern, containing bipolar values in the range -1 ..
|
||||||
|
* 1. Returns a new pattern with values scaled to the unipolar range 0 .. 1
|
||||||
|
* @returns Pattern
|
||||||
|
*/
|
||||||
_fromBipolar() {
|
_fromBipolar() {
|
||||||
return this.fmap((x) => (x + 1) / 2);
|
return this.fmap((x) => (x + 1) / 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Assumes source pattern of numbers in range 0..1
|
/**
|
||||||
|
* Assumes a numerical pattern, containing unipolar values in the range 0 ..
|
||||||
|
* 1. Returns a new pattern with values scaled to the given min/max range.
|
||||||
|
* @param {Number} min
|
||||||
|
* @param {Number} max
|
||||||
|
* @returns Pattern
|
||||||
|
*/
|
||||||
range(min, max) {
|
range(min, max) {
|
||||||
return this.mul(max - min).add(min);
|
return this.mul(max - min).add(min);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assumes a numerical pattern, containing unipolar values in the range 0 ..
|
||||||
|
* 1. Returns a new pattern with values scaled to the given min/max range,
|
||||||
|
* following an exponential curve.
|
||||||
|
* @param {Number} min
|
||||||
|
* @param {Number} max
|
||||||
|
* @returns Pattern
|
||||||
|
*/
|
||||||
rangex(min, max) {
|
rangex(min, max) {
|
||||||
return this.range(Math.log(min), Math.log(max)).fmap(Math.exp);
|
return this.range(Math.log(min), Math.log(max)).fmap(Math.exp);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Assumes source pattern of numbers in range -1..1
|
/**
|
||||||
|
* Assumes a numerical pattern, containing bipolar values in the range -1 ..
|
||||||
|
* 1. Returns a new pattern with values scaled to the given min/max range.
|
||||||
|
* @param {Number} min
|
||||||
|
* @param {Number} max
|
||||||
|
* @returns Pattern
|
||||||
|
*/
|
||||||
range2(min, max) {
|
range2(min, max) {
|
||||||
return this._fromBipolar().range(min, max);
|
return this._fromBipolar().range(min, max);
|
||||||
}
|
}
|
||||||
|
|
@ -979,7 +1027,7 @@ function _composeOp(a, b, func) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make composers
|
// Make composers
|
||||||
(function() {
|
(function () {
|
||||||
const num = (pat) => pat._asNumber();
|
const num = (pat) => pat._asNumber();
|
||||||
const numOrString = (pat) => pat._asNumber(false, true);
|
const numOrString = (pat) => pat._asNumber(false, true);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue