experimenting with high order function application
This commit is contained in:
@ -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.
|
||||
|
||||
Reference in New Issue
Block a user