mattn / go-generics-example

Example code for Go generics

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Ternaryop and Normal Order Evaluation

yuchanns opened this issue · comments

Normal Order Evaluation is an evaluation strategy in which an expression is evaluated by repeatedly evaluating its leftmost outermost reducible expression. This means that a function's arguments are not evaluated before the function is applied.

In a word, left or right value should be evaluated when it is applied

So this code should be some kind of the following form:

package main

import (
	"fmt"
)

func ternaryOp[T any](s bool, t func() T, f func() T) T {
	if s {
		return t()
	}
	return f()
}

func main() {
	fmt.Println(ternaryOp(4 < 5, func() string {
		fmt.Println("left evaluated")
		return "less"
	}, func() string {
		fmt.Println("right evaluated")
		return "greater"
	}))
}
commented

Closed by #4