Home > Software design >  How to apply sequentially a set of functions where the input of the current is the output of the pre
How to apply sequentially a set of functions where the input of the current is the output of the pre

Time:03-31

I'm searching for the most readable way of executing something like that:

    def f1(x: Int) = x   1
    def f2(x: Int) = x   2
    def f3(x: Int) = x   3
    
    val x = 1
    val result = f3(f2(f1(x)))

I know I could make some middle variables to improve the readability, but would like to know if there is a better way.

I am using this in Spark where each function add a new column to the initial Dataframe.

CodePudding user response:

The "scala way" is using compose (I changed the function types from your example, so that it is clearer what is going on):

    def f1(x: Int) = x 1
    def f2(x: Int) = x.toString
    def f3(s: String) = "foobar: "   s

    val combined = f3 _ compose f2 _ compose f1 _
    val result = combined(0) // "foobar: 1"

CodePudding user response:

Something like this?

val functions: List[DataFrame => DataFrame] = List(
  f1,
  ...
  fn
)

functions.foldLeft(initialDF) {
  case (acc, f) =>
    f(acc)
}

CodePudding user response:

The solution @Dima described is an excellent way of writing Scala code.

Though if you want to keep the fluent-API flow, you can also use scala.util.chaining.pipe to make it read left-to-right, like so:

import scala.util.chaining._

def f1(x: Int) = x 1
def f2(x: Int) = x.toString
def f3(s: String) = "foobar: "   s

val result = f1(0).pipe(f2).pipe(f3)
  • Related