In the skeleton of the following class
class Iterable<A> {
filter( filter: ( value: A ) => boolean ): Iterable<A> { ... }
map<B>( f: ( value: A, index?: number ) => B ): Iterable<B> { ...}
collect = <B>( filter: ( value: A ) => boolean ) => ( mapper: ( value: A ) => B ): Iterable<B> => { ... }
}
there is a curried property collect which is implemented this way
collect = <B>( filter: ( value: A ) => boolean ) => ( mapper: ( value: A ) => B ): Iterable<B> => {
return this.filter( filter ).map( mapper )
}
This property is basically a curried function that takes two functions as argument and returns an Iterable<B>. With this, being an Iterable<A>, it can be used in the following manner:
const it: Iterable<string> = this.collect<string>( f => true )( ( v: A ) => v.toString() )
Now, I would like to transform this property into a class method but I am failing to find the right syntax
declaring
collect<B>( filter: ( value: A ) => boolean ): ( mapper: ( value: A ) => B ) => Iterable<B>will preserve its usage (see
itabove), however I do not know how to implement it as the method expects to return aFunction, not anIterabledeclaring
collect<B>( filter: ( value: A ) => boolean ) => ( mapper: ( value: A ) => B ) : Iterable<B>is invalid...
Solution: (see @JLRishe and @NitzanTomer answers) I went for an explicit type declaration:
collect<B>(filter: (value: A) => boolean): (mapper: (value: A) => B) => Iterable<B> {
return (mapper: (value: A) => B) => this.filter(filter).map(mapper);
}
A, what doesthis.filterreturns? etc..Ais a generic, see my edits above with the skeleton of the class