Added counter to lists

This commit is contained in:
2023-12-19 22:41:30 +02:00
parent e557e5565b
commit facb30be3a
3 changed files with 48 additions and 0 deletions

View File

@ -977,6 +977,7 @@ export class UserAPI {
return this.counters[name].value; return this.counters[name].value;
}; };
$ = this.counter; $ = this.counter;
count = this.counter;
// ============================================================= // =============================================================
// Iterator functions (for loops, with evaluation count, etc...) // Iterator functions (for loops, with evaluation count, etc...)
@ -1737,12 +1738,21 @@ export class UserAPI {
* @param step - The step value of the array * @param step - The step value of the array
* @returns An array of values between start and end, with a given step * @returns An array of values between start and end, with a given step
*/ */
function countPlaces(num: number) {
var text = num.toString();
var index = text.indexOf(".");
return index == -1 ? 0 : (text.length - index - 1);
}
const result: number[] = []; const result: number[] = [];
if ((end > start && step > 0) || (end < start && step < 0)) { if ((end > start && step > 0) || (end < start && step < 0)) {
for (let value = start; value <= end; value += step) { for (let value = start; value <= end; value += step) {
result.push(value); result.push(value);
} }
} else if((end > start && step < 0) || (end < start && step > 0)) {
for (let value = start; value >= end; value -= step) {
result.push(parseFloat(value.toFixed(countPlaces(step))));
}
} else { } else {
console.error("Invalid range or step provided."); console.error("Invalid range or step provided.");
} }

View File

@ -120,6 +120,19 @@ beat(1)::sound(['kick', 'fsnare'].dur(3, 1))
true, true,
)} )}
## Iterating over lists
- <ic>counter(name,limit?,step?)</ic>: return the next value on the list based on counter value. The limit is optional and defaults to the length of the list. The step is optional and defaults to 1. Setting / changing limit will reset the counter.
- <ic>$(name,limit?,step?)</ic>: shorter alias for the counter.
${makeExample(
"Using counter to iterate over a list",
`
beat(0.5) :: sound("bd").gain(line(0,1,0.01).$("ramp")).out()
`,
true,
)}
## Manipulating notes and scales ## Manipulating notes and scales
- <ic>pitch()</ic>: convert a list of integers to pitch classes - <ic>pitch()</ic>: convert a list of integers to pitch classes

View File

@ -33,6 +33,8 @@ declare global {
gen(min: number, max: number, times: number): number[]; gen(min: number, max: number, times: number): number[];
sometimes(func: Function): number[]; sometimes(func: Function): number[];
apply(func: Function): number[]; apply(func: Function): number[];
counter(name: string | number, limit?: number, step?: number): number[]
$(name: string | number, limit?: number, step?: number): number[];
} }
} }
@ -398,8 +400,31 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this[Math.floor(api.randomGen() * this.length)]; return this[Math.floor(api.randomGen() * this.length)];
}; };
Array.prototype.rand = Array.prototype.random; Array.prototype.rand = Array.prototype.random;
Array.prototype.counter = function(
name: string | number,
limit?: number,
step?: number) {
/**
* @param n - Returns next item in array until the end, then returns the last value.
*
* @returns the shifted array
*/
const idx = api.counter(name,limit,step);
if(limit) {
return this[idx % this.length];
} else if(idx < this.length) {
return this[idx];
} else {
return this[this.length - 1];
}
};
Array.prototype.$ = Array.prototype.counter;
}; };
Array.prototype.scale = function ( Array.prototype.scale = function (
scale: string = "major", scale: string = "major",
base_note: number = 0, base_note: number = 0,