document some mathematical pattern methods

This commit is contained in:
alex 2022-05-06 15:42:48 +02:00
parent 73b0a3a0ef
commit d5a1832f31

View File

@ -67,7 +67,7 @@ export class Pattern {
* @param {Function} func the function to apply
* @returns Pattern
*/
withQueryTime(func) {
withQueryTime(func) {
return new Pattern((state) => this.query(state.withSpan((span) => span.withTime(func))));
}
@ -88,7 +88,7 @@ export class Pattern {
* @param {Function} func the function to apply
* @returns Pattern
*/
withHapTime(func) {
withHapTime(func) {
return this.withHapSpan((span) => span.withTime(func));
}
@ -97,7 +97,7 @@ export class Pattern {
* @param {Function} func
* @returns Pattern
*/
_withHaps(func) {
_withHaps(func) {
return new Pattern((state) => func(this.query(state)));
}
@ -106,7 +106,7 @@ export class Pattern {
* @param {Function} func
* @returns Pattern
*/
_withHap(func) {
_withHap(func) {
return this._withHaps((haps) => haps.map(func));
}
@ -193,7 +193,7 @@ export class Pattern {
/**
* see {@link Pattern#withValue|withValue}
*/
fmap(func) {
fmap(func) {
return this.withValue(func);
}
@ -231,7 +231,7 @@ export class Pattern {
* as its `part` timespan.
* @returns Pattern
*/
onsetsOnly() {
onsetsOnly() {
// Returns a new pattern that will only return haps where the start
// of the 'whole' timespan matches the start of the 'part'
// timespan, i.e. the haps that include their 'onset'.
@ -467,35 +467,83 @@ export class Pattern {
return result;
}
/**
* Assumes a numerical pattern. Returns a new pattern with all values rounded
* to the nearest integer.
* @returns Pattern
*/
round() {
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() {
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() {
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() {
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() {
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) {
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) {
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) {
return this._fromBipolar().range(min, max);
}
@ -979,7 +1027,7 @@ function _composeOp(a, b, func) {
}
// Make composers
(function() {
(function () {
const num = (pat) => pat._asNumber();
const numOrString = (pat) => pat._asNumber(false, true);