Skip to content

Instantly share code, notes, and snippets.

@fasetto
Last active April 25, 2018 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fasetto/aac946ab784c9fb477b0bd56a6ba0013 to your computer and use it in GitHub Desktop.
Save fasetto/aac946ab784c9fb477b0bd56a6ba0013 to your computer and use it in GitHub Desktop.
Predicate Sample
type Predicate<T> = (item: T) => boolean;
function filter<T> (source : T[], callback : Predicate<T>) {
const temp = [];
for (let item of source) {
if (callback(item)) {
temp.push(item);
}
}
return temp;
}
class List<T> extends Array<T>
{
constructor(...items: T[])
{
super();
for (let item of items){
this.push(item);
}
}
public where = (predicate: Predicate<T>): List<T> =>
{
const temp: List<T> = new List<T>();
for (let item of this) {
if (predicate(item)) {
temp.push(item);
}
}
return temp;
}
}
// Usage
const workers = new List<number>(1, 2, 3, 4, 5, 6, 7, 8, 9, 0);
let items = filter(workers, x => x > 6); // 7, 8, 9
items = workers.where(x => x > 6); // 7, 8, 9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment