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:
@@ -82,6 +82,11 @@ var (
|
|||||||
O.Of[IOE.IOEither[error, []byte]],
|
O.Of[IOE.IOEither[error, []byte]],
|
||||||
Body.Set,
|
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 adds the [H.ContentType] header
|
||||||
WithContentType = WithHeader(H.ContentType)
|
WithContentType = WithHeader(H.ContentType)
|
||||||
// WithAuthorization adds the [H.Authorization] header
|
// WithAuthorization adds the [H.Authorization] header
|
||||||
|
14
lambda/y.go
14
lambda/y.go
@@ -15,16 +15,12 @@
|
|||||||
|
|
||||||
package lambda
|
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
|
// 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] {
|
func Y[TRFRM ~func(RecFct) RecFct, RecFct ~func(T) R, T, R any](f TRFRM) RecFct {
|
||||||
g := func(h internalCombinator[T, R]) RecFct[T, R] {
|
|
||||||
|
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 func(t T) R {
|
||||||
return f(h(h))(t)
|
return f(h(h))(t)
|
||||||
}
|
}
|
||||||
|
@@ -16,12 +16,13 @@
|
|||||||
package lambda
|
package lambda
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFactorial(t *testing.T) {
|
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 {
|
return func(n int) int {
|
||||||
if n <= 0 {
|
if n <= 0 {
|
||||||
return 1
|
return 1
|
||||||
@@ -29,6 +30,5 @@ func TestFactorial(t *testing.T) {
|
|||||||
return n * r(n-1)
|
return n * r(n-1)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
assert.Equal(t, 3628800, fct(10))
|
||||||
fmt.Println(fct(10))
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user