these different functions are composed together like so
f1 -> f2 -> f3 -> f4 -> f5
I need to add two new functions fx and fy around f3 like so:
f1 -> f2 -> fx -> f3 -> fy -> f4 -> f5
so I need to pre-process the argument for f3 and then post-process the output from f3
here function fx makes a change and fy reverts the change back, but fy needs additional details from fx to be able to revert, to know what values to revert to for example
the problem is that function fx needs to produce two outputs
first: output that is needed by f3
second: output that is needed by fy
Question: How to share dependencies between functions that are not connected together direct, is there a spacial way/technique to solve this?
FYI, I'm using Java8
I will use some type
Afor all function return values; you will need to sort out the real types yourself.Here's the intention as you described it:
fxwill yield two parameters, let's call themaandb.f3will process the valueaand yielda'.fywill consumea'andband producec.Let's see how can we achieve that (I will write pseudocode for type signatures):
Define
fyas a function that takes two parameters and returns one:Define
fyp(short for "fy preprocessed") as a function that takes some preprocessing functionpand performs the logic of yourfy, but with first parameter preprocessed withp. We will write this in the formP => FY, wherePis the type signature of the preprocessing function, andFYis the type signature offy:So the implementation of this function should take the preprocessing function
pof typeA => Aas input, and perform the logic offy(this is the right hand side,(A, A) => A... note how it corresponds to the signature offy), but before performing the logic, it will map the first parameter offywithp. Here's some Scala implementation for reference (I'm not so good with Java):where
performFyLogicis the actual processing logic of yourfyfunction.Define the final composition as:
Here's full Scala implementation for reference (again, don't know Java 8 that well):