Skip to content

Commit

Permalink
fix: typing for Y combinator
Browse files Browse the repository at this point in the history
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
  • Loading branch information
CarstenLeue committed Dec 21, 2023
1 parent 5ac4744 commit 12ef791
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
5 changes: 5 additions & 0 deletions ioeither/http/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 4 additions & 8 deletions lambda/y.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,12 @@

package lambda

type (
// RecFct is the function called recursively
RecFct[T, R any] func(T) R
// Y is the Y-combinator based on https://dreamsongs.com/Files/WhyOfY.pdf
func Y[TRFRM ~func(RecFct) RecFct, RecFct ~func(T) R, T, R any](f TRFRM) RecFct {

internalCombinator[T, R any] func(internalCombinator[T, R]) RecFct[T, R]
)
type internalCombinator[RecFct ~func(T) R, T, R any] func(internalCombinator[RecFct, T, R]) RecFct

// 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] {
g := func(h internalCombinator[RecFct, T, R]) RecFct {
return func(t T) R {
return f(h(h))(t)
}
Expand Down
8 changes: 4 additions & 4 deletions lambda/y_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@
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
}
return n * r(n-1)
}
})

fmt.Println(fct(10))
assert.Equal(t, 3628800, fct(10))
}

0 comments on commit 12ef791

Please sign in to comment.