From f92b7d7480def33c10786204474d060229ca7fd6 Mon Sep 17 00:00:00 2001 From: Raphael Forment Date: Sun, 8 Oct 2023 19:34:09 +0200 Subject: [PATCH] experimenting with high order function application --- src/API.ts | 72 ++++++++++++++++++++++++++++++++++++++++++ src/ArrayExtensions.ts | 14 ++++++++ 2 files changed, 86 insertions(+) diff --git a/src/API.ts b/src/API.ts index c0134f2..66567bf 100644 --- a/src/API.ts +++ b/src/API.ts @@ -1750,4 +1750,76 @@ export class UserAPI { */ return array[(this.app.clock.time_position.bar + 1) % array.length]; }; + + // ============================================================= + // High Order Functions + // ============================================================= + + public shuffle = (array: T[]): T[] => { + /** + * Returns a shuffled version of an array. + * @param array - The array to shuffle + * @returns A shuffled version of the array + */ + return array.sort(() => this.randomGen() - 0.5); + }; + + public reverse = (array: T[]): T[] => { + /** + * Returns a reversed version of an array. + * @param array - The array to reverse + * @returns A reversed version of the array + */ + return array.reverse(); + }; + + public rotate = (n: number): Function => { + /** + * Returns a partially applied function that rotates an array by n. + * + */ + + return (array: T[]): T[] => { + return array.slice(n, array.length).concat(array.slice(0, n)); + }; + }; + + public repeat = (n: number): Function => { + /** + * Returns a partially applied function that repeats each element of an array n times. + * + */ + return (array: T[]): T[] => { + return array.flatMap((x) => Array(n).fill(x)); + }; + }; + + public repeatOdd = (n: number): Function => { + /** + * Returns a partially applied function that repeats each even element of an array n times. + * + */ + return (array: T[]): T[] => { + return array.flatMap((x, i) => (i % 2 === 0 ? Array(n).fill(x) : x)); + }; + }; + + public repeatEven = (n: number): Function => { + /** + * Returns a partially applied function that repeats each even element of an array n times. + * + */ + return (array: T[]): T[] => { + return array.flatMap((x, i) => (i % 2 !== 0 ? Array(n).fill(x) : x)); + }; + }; + + public palindrome = (array: T[]): T[] => { + /** + * Returns a palindrome of an array. + * @param array - The array to palindrome + * @returns A palindrome of the array + */ + return array.concat(array.slice(0, array.length - 1).reverse()); + }; } diff --git a/src/ArrayExtensions.ts b/src/ArrayExtensions.ts index c48344c..69a0323 100644 --- a/src/ArrayExtensions.ts +++ b/src/ArrayExtensions.ts @@ -28,6 +28,8 @@ declare global { square(): number[]; sqrt(): number[]; gen(min: number, max: number, times: number): number[]; + sometimes(func: Function): number[]; + apply(func: Function): number[]; } } @@ -39,6 +41,18 @@ export const makeArrayExtensions = (api: UserAPI) => { return this.map((x: number) => x * x); }; + Array.prototype.sometimes = function (func: Function): number[] { + if (api.randomGen() < 0.5) { + return func(this); + } else { + return this; + } + }; + + Array.prototype.apply = function (func: Function): number[] { + return func(this); + }; + Array.prototype.sqrt = function (): number[] { /** * @returns New array with square roots of values. Throws if any element is negative.