From 1e0a0d12685125b1d3a8f064c09656b4bab8f965 Mon Sep 17 00:00:00 2001 From: Raphael Forment Date: Sun, 27 Aug 2023 19:29:38 +0200 Subject: [PATCH] making plans --- src/API.ts | 11 +++++++- src/ArrayExtensions.ts | 64 ++++++++++++++++++++++++++++++++++++++---- 2 files changed, 69 insertions(+), 6 deletions(-) diff --git a/src/API.ts b/src/API.ts index 9f4b09d..4033437 100644 --- a/src/API.ts +++ b/src/API.ts @@ -958,7 +958,16 @@ export class UserAPI { */ return this._euclidean_cycle(pulses, length, rotate)[iterator % length]; }; - ec = this.euclid; + ec: Function = this.euclid; + + public rhythm = ( + div: number, + pulses: number, + length: number, + rotate: number = 0 + ): boolean => { + return this.mod(div) && this._euclidean_cycle(pulses, length, rotate).div(div); + } _euclidean_cycle( pulses: number, diff --git a/src/ArrayExtensions.ts b/src/ArrayExtensions.ts index c8c6e72..b715fa9 100644 --- a/src/ArrayExtensions.ts +++ b/src/ArrayExtensions.ts @@ -3,6 +3,10 @@ export {}; declare global { interface Array { + add(amount: number): number[]; + sub(amount: number): number[]; + mult(amount: number): number[]; + division(amount: number): number[]; palindrome(): T[]; random(index: number): T; rand(index: number): T; @@ -20,6 +24,8 @@ declare global { rotate(steps: number): this; unique(): this; in(value: T): boolean; + square(): number[]; + sqrt(): number[]; } } @@ -27,6 +33,54 @@ export const makeArrayExtensions = (api: UserAPI) => { Array.prototype.in = function (this: T[], value: T): boolean { return this.includes(value); }; + + Array.prototype.square = function (): number[] { + /** + * @returns New array with squared values. + */ + return this.map((x: number) => x * x); + }; + + Array.prototype.sqrt = function (): number[] { + /** + * @returns New array with square roots of values. Throws if any element is negative. + */ + if (this.some(x => x < 0)) throw new Error('Cannot take square root of negative number'); + return this.map((x: number) => Math.sqrt(x)); + }; + + Array.prototype.add = function (amount: number): number[] { + /** + * @param amount - The value to add to each element in the array. + * @returns New array with added values. + */ + return this.map((x: number) => x + amount); + }; + + Array.prototype.sub = function (amount: number): number[] { + /** + * @param amount - The value to subtract from each element in the array. + * @returns New array with subtracted values. + */ + return this.map((x: number) => x - amount); + }; + + Array.prototype.mult = function (amount: number): number[] { + /** + * @param amount - The value to multiply with each element in the array. + * @returns New array with multiplied values. + */ + return this.map((x: number) => x * amount); + }; + + Array.prototype.division = function (amount: number): number[] { + /** + * @param amount - The value to divide each element in the array by. + * @returns New array with divided values. Throws if division by zero. + */ + if (amount === 0) throw new Error('Division by zero'); + return this.map((x: number) => x / amount); + }; Array.prototype.pick = function () { /** @@ -37,13 +91,13 @@ export const makeArrayExtensions = (api: UserAPI) => { return this[Math.floor(api.randomGen() * this.length)]; }; - Array.prototype.beat = function () { + Array.prototype.beat = function (beat: number = 1) { /** * Returns the element corresponding to the current beat * * @returns The element corresponding to the current beat */ - return this[api.ebeat() % this.length]; + return this[(api.ebeat() / beat) % this.length]; }; Array.prototype.bar = function () { @@ -160,7 +214,7 @@ export const makeArrayExtensions = (api: UserAPI) => { return this; }; - Array.prototype.repeatAll = function (this: T[], amount: number) { + Array.prototype.repeatAll = function (this: T[], amount: number = 1) { /** * Repeats all elements in the array n times. * @@ -181,7 +235,7 @@ export const makeArrayExtensions = (api: UserAPI) => { return this; }; - Array.prototype.repeatPair = function (this: T[], amount: number) { + Array.prototype.repeatPair = function (this: T[], amount: number = 1) { /** * Repeats all elements in the array n times, except for the * elements at odd indexes. @@ -211,7 +265,7 @@ export const makeArrayExtensions = (api: UserAPI) => { return this; }; - Array.prototype.repeatOdd = function (this: T[], amount: number) { + Array.prototype.repeatOdd = function (this: T[], amount: number = 1) { /** * Repeats all elements in the array n times, except for the * elements at even indexes.