diff --git a/src/API.ts b/src/API.ts
index ed161b3..856e697 100644
--- a/src/API.ts
+++ b/src/API.ts
@@ -977,6 +977,7 @@ export class UserAPI {
return this.counters[name].value;
};
$ = this.counter;
+ count = this.counter;
// =============================================================
// Iterator functions (for loops, with evaluation count, etc...)
@@ -1737,12 +1738,21 @@ export class UserAPI {
* @param step - The step value of the array
* @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[] = [];
if ((end > start && step > 0) || (end < start && step < 0)) {
for (let value = start; value <= end; value += step) {
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 {
console.error("Invalid range or step provided.");
}
diff --git a/src/documentation/patterns/patterns.ts b/src/documentation/patterns/patterns.ts
index 70b7c12..139b4e6 100644
--- a/src/documentation/patterns/patterns.ts
+++ b/src/documentation/patterns/patterns.ts
@@ -120,6 +120,19 @@ beat(1)::sound(['kick', 'fsnare'].dur(3, 1))
true,
)}
+## Iterating over lists
+
+- counter(name,limit?,step?): 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.
+- $(name,limit?,step?): 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
- pitch(): convert a list of integers to pitch classes
diff --git a/src/extensions/ArrayExtensions.ts b/src/extensions/ArrayExtensions.ts
index 13cb751..98a1b58 100644
--- a/src/extensions/ArrayExtensions.ts
+++ b/src/extensions/ArrayExtensions.ts
@@ -33,6 +33,8 @@ declare global {
gen(min: number, max: number, times: number): number[];
sometimes(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)];
};
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 (
scale: string = "major",
base_note: number = 0,