Debian Codegraph @

test/fixedbugs/issue72063.go

raw
1 // run
2
3 // Copyright 2025 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
6
7 package main
8
9 import "fmt"
10
11 // Y is the Y-combinator based on https://dreamsongs.com/Files/WhyOfY.pdf
12 func Y[Endo ~func(RecFct) RecFct, RecFct ~func(T) R, T, R any](f Endo) RecFct {
13
14 type internal[RecFct ~func(T) R, T, R any] func(internal[RecFct, T, R]) RecFct
15
16 g := func(h internal[RecFct, T, R]) RecFct {
17 return func(t T) R {
18 return f(h(h))(t)
19 }
20 }
21 return g(g)
22 }
23
24 func main() {
25
26 fct := Y(func(r func(int) int) func(int) int {
27 return func(n int) int {
28 if n <= 0 {
29 return 1
30 }
31 return n * r(n-1)
32 }
33 })
34
35 want := 3628800
36 if got := fct(10); got != want {
37 msg := fmt.Sprintf("unexpected result, got: %d, want: %d", got, want)
38 panic(msg)
39 }
40 }