yc

package module
v0.0.0-...-98cd430 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 13, 2022 License: Unlicense Imports: 1 Imported by: 0

README

YC

YC is the Y combinator in Go with generics

This package is written based on the content of "The Y combinator in Go with generics".

Implemented the Y combinator and some adaptions (e.g. memoize, tracing) for it.

Synopsis

package main

var factorialTag = func(recurse yc.Func[int, int]) yc.Func[int, int] {
	return func(n int) int {
		if n == 0 {
			return 1
		}
		return n * recurse(n-1)
	}
}

func main() {
    fac := yc.Y(yc.Adapt(factorialTag, yc.Memo[int, int](), yc.Trace[int, int]()))
    got := fac(10)
    fmt.Println(got) // 3628800
}

Run tests

$ go test -timeout 30s ./... github.com/Code-Hex/yc

Run benchmark

$ go test -benchmem -bench . github.com/Code-Hex/yc

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CombinatorFunc

type CombinatorFunc[T, U any] func(CombinatorFunc[T, U]) Func[T, U]

type Func

type Func[T, U any] func(T) U

func Y

func Y[T, U any](f TagFunc[T, U]) Func[T, U]

type TagFunc

type TagFunc[T, U any] func(Func[T, U]) Func[T, U]

func Adapt

func Adapt[T, U any](f TagFunc[T, U], adapters ...TagFunc[T, U]) TagFunc[T, U]

func Memo

func Memo[T comparable, U any]() TagFunc[T, U]

func Trace

func Trace[T, U any]() TagFunc[T, U]

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL