From 0de3e407e29437c7d7fe780282d827ffc89c0090 Mon Sep 17 00:00:00 2001 From: Xe Iaso Date: Sun, 24 Apr 2022 23:40:33 +0000 Subject: [PATCH] make better thunk test Signed-off-by: Xe Iaso --- thunk.go | 11 +++++++++-- thunk_test.go | 12 ++++++++---- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/thunk.go b/thunk.go index ca24455..b31fe29 100644 --- a/thunk.go +++ b/thunk.go @@ -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](), + } +} diff --git a/thunk_test.go b/thunk_test.go index 48eb3f9..bd7aaec 100644 --- a/thunk_test.go +++ b/thunk_test.go @@ -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) { + +}