Scala: how to implement a common thread through the functional parameters of an object, where are the different signatures?

I am a Java programmer who performs baby steps in Scala programming.

I defined a function similar (which might be idiomatically incorrect, I would not know ...):

def someGenericAlgorithm(param1: String, param1: String, param3: String) = { val triedResult1 = someFunction(param1) triedResult1 match { case Success(result1) => val triedResult2 = someOtherFunction(param2) triedResult2 match { case Success(result2) => val triedPolymorphicResult = someToBeReplacedAccordingToSpecificLogicFunction(result1, result2, param3) triedPolymorphicResult match { case Success(polymorphicResult) => doSomethingGeneric(polymorphicResult) .... case Failure(exception) => .... } case Failure(exception) => .... } case Failure(exception) => .... } } 

Thus, the function receives 3 parameters, processes the first two in the sequence, takes the processing results and passes it along with someToBeReplacedAccordingToSpecificLogicFunction along the side of the third parameter.

Now I want to write a similar function in which the first two function calls are present, the error logic is the same, and the only difference is that someToBeReplacedAccordingToSpecificLogicFunction now aReplacementLogicFunction .

If two functions had the same signature, I think that would not be a problem, but they do not, aReplacementLogicFunction has different parameters, or rather two more parameters. Then you can say that I can simply pass Option parameters, where these two Option are None in the first case, and so I would compare my two functions with the same signature, like this:

 def someToBeReplacedAccordingToSpecificLogicFunction(result1: Int, result2: Int, param3: String, unused1:Option[String] = None, unused2:Option[String] = None) def aReplacementLogicFunction(result1: Int, result2: Int, param3: String, used1:Option[String], used2:Option[String]) 

But there is a third thread where non-existent arguments of the first function are of a different type, for example MyClass, for example:

 def anotherReplacementLogicFunction(result1: Int, result2: Int, param3: String, used1: MyClass, used2: MyClass) 

The question is, how could I pass these functions along with some refactored universal function of the algorithm, which would receive the function as a parameter. Is there a way to define something like:

 def refactoredOutCoreLogic(param1: String, param1: String, param3: String, polimorphicFunction: SignatureUnknown) 

To answer your own question in Java :

The way I solved this in Java is to define a class hierarchy, akin to:

 abstract class ResultKeeperRunnable implements Runnable { protected int param1, param2; void setParam1(...) {...} void setParam2(...) {...} } class SomeToBeReplacedAccordingToSpecificLogicFunctionClass extends ResultKeeperRunnable { String param3; // ctor receiving all params omitted for brevity void run() { // do specific Logic } } class AReplacementLogicFunctionClass extends ResultKeeperRunnable { String param3, param4, param5; // ctor receiving all params omitted for brevity void run() { // do specific Logic } } class AnotherReplacementLogicFunctionClass extends ResultKeeperRunnable { String param3; MyClass param4, param5; // ctor receiving all params omitted for brevity void run() { // do specific Logic } } 

Finally, a complete main logical method would be something like this:

 void refactoredOutCoreLogic(String param1, String param2, String param3, ResultKeeperRunnable polimorphicLogic) { // error handling logic removed for brevity int result1 = someMethod(param1); int result2 = someOtherMethod(param2); polymorphicLogic.setParam1(result1); polymorphicLogic.setParam2(result2); polymorphicLogic.run() ... } 

Will the Scala solution be the no more syntactically distinct clone of the Java solution presented above? There seems to be a more elegant solution.

+6
source share
1 answer

You have a function that takes result1 , result2 and many other parameters.

You need a function that accepts only result1 and result2 .

To do this, use a private application . Write down your plug-in functions, made in the same way as the second brackets at the end, containing common parameters:

 def aReplacementLogicFunction(param3: String, used1:Option[String], used2:Option[String])(result1: Int, result2: Int) 

Then pass the partially applied function to your generic function as follows:

 someGenericAlgorithm(param1, param2, aReplacementLogicFunction(param3, used1, used2)) 

As for the general algorithm, it absolutely does not know additional parameters. All of them are applied before a function call.

+2
source

Source: https://habr.com/ru/post/982365/


All Articles