0
votes

I am trying to remove some duplicate code using Higher Order functions.

How this is different from calling Singleton Object method?

Sample Code:

filterDFForPaymentType(filterDF, df, "Manual")
filterDFForPaymentType(filterDF, df, "Electronic")

def filterDFForPaymentType(fn: (DataFrame, String) => DataFrame, df: DataFrame, paymentType: String) {
      fn(df, paymentType)
}

def filterDF(df: DataFrame, paymentType: String): DataFrame = {
      val filteredDF = df.where(col("paymenttypecol") === paymentType)
      filteredDF
}

How this is different from calling the same function using a Singleton object method.

filterDFForPaymentType(UtilClass.filterDF(df, "Manual"))
filterDFForPaymentType(UtilClass.filterDF(df, "Electronic"))

I'm sure we can do much more using Higher order functions, but i don't really getting the point.

Is this something correct way to use Higher-Order functions?

Can someone give me the link for proper usage of Higher-Order functions with examples?

1
In this case, there is no benefit against directly using the functions - cchantep

1 Answers

2
votes

The filterDFForPaymentType function you have here doesn't do anything other than return the result of your other function, in this case the code can be shortened to

filterDF(df, "Manual")
filterDF(df, "Electronic")
def filterDF(df: DataFrame, paymentType: String): DataFrame = {
      val filteredDF = df.where(col("paymenttypecol") === paymentType)
      filteredDF
}

The higher order functions are useful where they implement some logic themselves (i.e. in collections the deconstruction & construction of the collection) whilst using the passed in function as part of it. In the case where the passed in function would optionally be used there could be a difference in performance.