make better thunk test

Signed-off-by: Xe Iaso <me@christine.website>
This commit is contained in:
Cadey Ratio 2022-04-24 23:40:33 +00:00
parent 716360786a
commit 0de3e407e2
2 changed files with 17 additions and 6 deletions

View File

@ -15,8 +15,8 @@ package gonads
// In this example, `addTwo` is a thunk that contains a partially applied addCurr
// invocation.
type Thunk[T any] struct {
doer func() T // action being thunked
o Option[T] // cache for complete thunk data
doer func() T // action being thunked
o *Option[T] // cache for complete thunk data
}
// Force evaluates a Thunk's action if it needs to, otherwise it returns the
@ -29,3 +29,10 @@ func (t Thunk[T]) Force() T {
t.o.Set(t.doer())
return t.o.Yank()
}
func NewThunk[T any](doer func() T) *Thunk[T] {
return &Thunk[T]{
doer: doer,
o: NewOption[T](),
}
}

View File

@ -16,9 +16,7 @@ func TestRecurFib(t *testing.T) {
}
func TestThunkFib(t *testing.T) {
cache := make([]Thunk[int], 41)
cache[0].o.Set(0)
cache[1].o.Set(1)
cache := make([]*Thunk[int], 41)
var fib func(int) int
fib = func(n int) int {
@ -30,8 +28,14 @@ func TestThunkFib(t *testing.T) {
for i := range cache {
i := i
cache[i].doer = func() int { return fib(i) }
cache[i] = NewThunk(func() int { return fib(i) })
}
cache[0].o.Set(0)
cache[1].o.Set(1)
t.Log(cache[40].Force())
}
func TestThunkDelay(t *testing.T) {
}