diff --git a/converge.go b/converge.go new file mode 100644 index 0000000..e11acef --- /dev/null +++ b/converge.go @@ -0,0 +1,15 @@ +package fp + +/* + * Converge is a function that Accepts a converging function and variadic parameters of function and returns a new function. + * When invoked, this new function is applied to some arguments, + * and each branching function is applied to those same arguments. + * The results of each branching function are passed as arguments to the converging function to produce the return value. + */ + +// Apply converge of 2 branch functions +func Converge2[R1, R2, R, T any](fc func(R1, R2) R, f1 func(T) R1, f2 func(T) R2) func(T) R { + return func(t T) R { + return fc(f1(t), f2(t)) + } +} diff --git a/converge_test.go b/converge_test.go new file mode 100644 index 0000000..f48373a --- /dev/null +++ b/converge_test.go @@ -0,0 +1,18 @@ +package fp + +import ( + "fmt" + "testing" +) + +func TestConverge_Example(t *testing.T) { + concatNumbers := func(n1, n2 int) string { + return fmt.Sprintf("%d%d", n1, n2) + } + res := Converge2(concatNumbers, add1, double)(10) + expected := "1120" + + if res != expected { + t.Errorf("Should converge results of two functions. Expected: %s, Received:%s", expected, res) + } +}