Сообщения

Сообщения за сентябрь, 2020

Scala: compose, andThen

1) andThen - composes 2 functions from left to right, "execute first and send result into input of seconds function and etc"  Example: we have 4 functions: String => Int  Int => Int Int => Double Double => String we can compose all this function in chain and produce function  String  =>  String , look at val func val StrToInt :String => Int = param => param.toInt val mult2: Int => Int = param => param*2 val addSmall: Int => Double = param => param+0.1 val DblToString :Double => String = param => param.toString val initVal:String = "123" (StrToInt andThen mult2 andThen addSmall andThen DblToString)(initVal) DblToString(addSmall(mult2(StrToInt(initVal)))) val func : String => String = StrToInt andThen mult2 andThen addSmall andThen DblToString func(initVal) Output StrToInt: String => Int = function mult2: Int => Int = function addSmall: Int => Double = function DblToString: Double => String = fun