Last active
February 22, 2018 00:39
-
-
Save hamxiaoz/49cf75648c8892635ae180a8d8158c7c to your computer and use it in GitHub Desktop.
Point free style in TypeScript
This file contains hidden or 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
interface Transaction { | |
amount: number; | |
} | |
class Test { | |
private const BIG_AMOUNT = 10; | |
public getPositiveTransactions(transactions: Transaction[]) { | |
// point free | |
return transactions.filter(this.isPositive); | |
// not point free | |
// return transactions.filter((transaction: Transaction) => transaction.amount > 0)); | |
} | |
public getBigTransactions(transactions: Transaction[]) { | |
// point free | |
return transactions.filter(this.moreThan(BIG_AMOUNT)); | |
// not point free | |
// return transactions.filter((transaction: any) => transaction.amount > BIG_AMOUNT); | |
} | |
private isPositive(transaction: Transaction) { | |
return transactions.amount > 0; | |
} | |
private moreThan(amount: number) { | |
return (transaction: Transaction) => { | |
return transactions.amount > amount; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment