What is “point free” style (in Functional Programming)?

前端 未结 5 1148
情深已故
情深已故 2020-11-27 12:33

A phrase that I\'ve noticed recently is the concept of \"point free\" style...

First, there was this question, and also this one.

Then, I discovered here the

5条回答
  •  半阙折子戏
    2020-11-27 13:03

    Here is one example in TypeScript without any other library:

    interface Transaction {
      amount: number;
    }
    
    class Test {
      public getPositiveNumbers(transactions: Transaction[]) {
        return transactions.filter(this.isPositive);
    
        //return transactions.filter((transaction: {amount: number} => transaction.amount > 0));
      }
    
      public getBigNumbers(transactions: Transaction[]) {
        // point-free
        return transactions.filter(this.moreThan(10));
    
        // not point-free
        // return transactions.filter((transaction: any) => transaction.amount > 10);
      }
    
      private isPositive(transaction: Transaction) {
        return transactions.amount > 0;
      }
    
      private moreThan(amount: number) {
        return (transaction: Transaction) => {
          return transactions.amount > amount;
        }
      }
    }
    

    You can see point-free style is more "fluent" and easier to read.

提交回复
热议问题