make better thunk test
Signed-off-by: Xe Iaso <me@christine.website>
This commit is contained in:
parent
716360786a
commit
0de3e407e2
11
thunk.go
11
thunk.go
|
@ -15,8 +15,8 @@ package gonads
|
||||||
// In this example, `addTwo` is a thunk that contains a partially applied addCurr
|
// In this example, `addTwo` is a thunk that contains a partially applied addCurr
|
||||||
// invocation.
|
// invocation.
|
||||||
type Thunk[T any] struct {
|
type Thunk[T any] struct {
|
||||||
doer func() T // action being thunked
|
doer func() T // action being thunked
|
||||||
o Option[T] // cache for complete thunk data
|
o *Option[T] // cache for complete thunk data
|
||||||
}
|
}
|
||||||
|
|
||||||
// Force evaluates a Thunk's action if it needs to, otherwise it returns the
|
// 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())
|
t.o.Set(t.doer())
|
||||||
return t.o.Yank()
|
return t.o.Yank()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewThunk[T any](doer func() T) *Thunk[T] {
|
||||||
|
return &Thunk[T]{
|
||||||
|
doer: doer,
|
||||||
|
o: NewOption[T](),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -16,9 +16,7 @@ func TestRecurFib(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestThunkFib(t *testing.T) {
|
func TestThunkFib(t *testing.T) {
|
||||||
cache := make([]Thunk[int], 41)
|
cache := make([]*Thunk[int], 41)
|
||||||
cache[0].o.Set(0)
|
|
||||||
cache[1].o.Set(1)
|
|
||||||
|
|
||||||
var fib func(int) int
|
var fib func(int) int
|
||||||
fib = func(n int) int {
|
fib = func(n int) int {
|
||||||
|
@ -30,8 +28,14 @@ func TestThunkFib(t *testing.T) {
|
||||||
|
|
||||||
for i := range cache {
|
for i := range cache {
|
||||||
i := i
|
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())
|
t.Log(cache[40].Force())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestThunkDelay(t *testing.T) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue