Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions converge.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
18 changes: 18 additions & 0 deletions converge_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}