Last active
April 25, 2018 17:19
-
-
Save fasetto/aac946ab784c9fb477b0bd56a6ba0013 to your computer and use it in GitHub Desktop.
Predicate Sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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