What is a Flow?
A Flow is a Func that is created in a procedural way by weaving other Funcs.
According to R.apex, we normally have two ways to create a Func.
- Func Composition An example of this is below:
Func notEqBy3 = (Func)R.complement.run(R.equals.apply(3));
In this way, we compose a large Func out of small Funcs. This more functional way of creating Funcs is limited by the number of existing Funcs that we can use, and the complexity of composing them. Basically, it works perfectly for small and simple functions, but not so good with large and complicated functions.
- Func Inheritance Another way of creating a Func is by subclassing
Func
.
public class CustomFunc extends Func {
public CustomFunc() {
super(1);
}
public override Object exec(Object arg) {
// Custom logic here
return ...;
}
}
We write our same old logic here in the custom Func, and wraps our complex code in a custom Func. The issue, however, is that creating custom Funcs all the time is quite heavy. We kind of miss the adorable composing abilities from the first approach.
- Flow Funcs So here comes Flow.apex for the rescue.
Func f = new Flow()
.inputAs('a', 'b').returnInteger()
.var('sum = a + b')
.doReturn('sum');
Flows, subclassing from Func
, come with another style of functional composition, the procedural style of functional chaining.
The intention of Flow.apex is not to promote procedural programming camouflaged in functional programming. Instead, it is just a bridge between the pure functional way and the pure procedural way, trying to glue the Funcs to create the ultimate complex monster by utilizing a little power of both. So the takeaway is to try to keep your Flow logic as simple as possible, and reuse as many Funcs as possible.