1
0
mirror of https://github.com/IBM/fp-go.git synced 2025-08-10 22:31:32 +02:00

fix: typing for Y combinator

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
This commit is contained in:
Dr. Carsten Leue
2023-12-21 16:25:35 +01:00
parent 5ac47440a1
commit 12ef79184b
3 changed files with 14 additions and 13 deletions

View File

@@ -82,6 +82,11 @@ var (
O.Of[IOE.IOEither[error, []byte]],
Body.Set,
)
// WithBytes creates a [BuilderBuilder] for a request body using bytes
WithBytes = F.Flow2(
IOE.Of[error, []byte],
WithBody,
)
// WithContentType adds the [H.ContentType] header
WithContentType = WithHeader(H.ContentType)
// WithAuthorization adds the [H.Authorization] header

View File

@@ -15,16 +15,12 @@
package lambda
type (
// RecFct is the function called recursively
RecFct[T, R any] func(T) R
internalCombinator[T, R any] func(internalCombinator[T, R]) RecFct[T, R]
)
// Y is the Y-combinator based on https://dreamsongs.com/Files/WhyOfY.pdf
func Y[TRFRM ~func(RecFct[T, R]) RecFct[T, R], T, R any](f TRFRM) RecFct[T, R] {
g := func(h internalCombinator[T, R]) RecFct[T, R] {
func Y[TRFRM ~func(RecFct) RecFct, RecFct ~func(T) R, T, R any](f TRFRM) RecFct {
type internalCombinator[RecFct ~func(T) R, T, R any] func(internalCombinator[RecFct, T, R]) RecFct
g := func(h internalCombinator[RecFct, T, R]) RecFct {
return func(t T) R {
return f(h(h))(t)
}

View File

@@ -16,12 +16,13 @@
package lambda
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestFactorial(t *testing.T) {
fct := Y(func(r RecFct[int, int]) RecFct[int, int] {
fct := Y(func(r func(int) int) func(int) int {
return func(n int) int {
if n <= 0 {
return 1
@@ -29,6 +30,5 @@ func TestFactorial(t *testing.T) {
return n * r(n-1)
}
})
fmt.Println(fct(10))
assert.Equal(t, 3628800, fct(10))
}