代码拉取完成,页面将自动刷新
package goalgeff_test
import (
"goalgeff/algeff"
"testing"
"github.com/stretchr/testify/assert"
)
func TestTco(t *testing.T) {
type Nop struct {
algeff.EffReturns[int]
}
x := algeff.Handle(func(c algeff.Ctx) (r int) {
for range 100000 {
r += algeff.Perform(c, Nop{})
}
return
}, algeff.NewHandlers(algeff.Fun[int](func(Nop, algeff.Ctx) int {
return 1
}))).Run()
assert.Equal(t, 100000, x)
}
func TestDivide(t *testing.T) {
type DBZ struct {
algeff.EffReturns[int]
}
div := func(a, b int, c algeff.Ctx) int {
if b == 0 {
return algeff.Perform(c, DBZ{})
}
return a / b
}
algeff.Handle(func(c algeff.Ctx) struct{} {
assert.Equal(t, 7, div(10, 2, c)+div(10, 5, c))
assert.Equal(t, 100000, div(10, 10, c)+div(10, 0, c))
return struct{}{}
}, algeff.NewHandlers(algeff.Fun[struct{}](func(DBZ, algeff.Ctx) int {
return 99999
}))).Run()
h := algeff.NewHandlers(algeff.Ctl(func(DBZ, algeff.Ctx, algeff.Cont[int, int]) int {
return 99999
}))
algeff.Run(func(c algeff.Ctx) struct{} {
assert.Equal(t, 7, algeff.Handle(func(c algeff.Ctx) int {
return div(10, 2, c) + div(10, 5, c)
}, h)(c))
assert.Equal(t, 99999, algeff.Handle(func(c algeff.Ctx) int {
return div(10, 10, c) + div(10, 0, c)
}, h)(c))
return struct{}{}
})
}
func TestFibo(t *testing.T) {
type State struct {
a int
b int
}
type Read struct {
algeff.EffReturns[State]
}
type Write struct {
algeff.EffReturns[struct{}]
State
}
fibo := func(n int, c algeff.Ctx) int {
algeff.Perform(c, Write{State: State{a: 1, b: 0}})
for range n {
st := algeff.Perform(c, Read{})
algeff.Perform(c, Write{State: State{a: st.a + st.b, b: st.a}})
}
return algeff.Perform(c, Read{}).b
}
check := func(c algeff.Ctx) struct{} {
assert.Equal(t, 0, fibo(0, c))
assert.Equal(t, 5, fibo(5, c))
assert.Equal(t, 55, fibo(10, c))
assert.Equal(t, 6765, fibo(20, c))
return struct{}{}
}
algeff.Run(func(c algeff.Ctx) struct{} {
type RW func(State) State
algeff.Handle(check, algeff.NewHandlersReturns(
func(algeff.Ctx, struct{}) RW {
return func(s State) State { return s }
},
algeff.Ctl(func(_ Read, ctx algeff.Ctx, cont algeff.Cont[RW, State]) RW {
return func(s State) State { return cont(s)(s) }
}),
algeff.Ctl(func(e Write, ctx algeff.Ctx, cont algeff.Cont[RW, struct{}]) RW {
return func(State) State { return cont(struct{}{})(e.State) }
}),
))(c)(State{})
var st State
algeff.Handle(check, algeff.NewHandlers(
algeff.Fun[struct{}](func(Read, algeff.Ctx) State {
return st
}),
algeff.Fun[struct{}](func(e Write, _ algeff.Ctx) struct{} {
st = e.State
return struct{}{}
}),
))(c)
return struct{}{}
})
}
func TestIter(t *testing.T) {
type Yield struct {
algeff.EffReturns[struct{}]
val int
}
type Iter = algeff.Procedure[struct{}]
intRange := func(c algeff.Ctx) struct{} {
for i := 0; ; i++ {
algeff.Perform(c, Yield{val: i})
}
}
take := func(iter Iter, n int) Iter {
i := 0
return algeff.Handle(iter, algeff.NewHandlers(algeff.Dyn(func(e Yield, c algeff.Ctx, _ algeff.Cont[struct{}, struct{}]) (_ struct{}, _ struct{}, ok bool) {
if i >= n {
return
}
algeff.Perform(c, e)
i++
ok = true
return
})))
}
double := func(iter Iter) Iter {
return algeff.Handle(iter, algeff.NewHandlers(algeff.Fun[struct{}](func(e Yield, c algeff.Ctx) struct{} {
return algeff.Perform(c, Yield{val: e.val * 2})
})))
}
even := func(iter Iter) Iter {
return algeff.Handle(iter, algeff.NewHandlers(algeff.Fun[struct{}](func(e Yield, c algeff.Ctx) struct{} {
if e.val%2 != 0 {
return struct{}{}
}
return algeff.Perform(c, e)
})))
}
collect := func(iter Iter, c algeff.Ctx) []int {
var l []int
algeff.Handle(iter, algeff.NewHandlers(algeff.Fun[struct{}](func(e Yield, _ algeff.Ctx) struct{} {
l = append(l, e.val)
return struct{}{}
})))(c)
return l
}
algeff.Run(func(c algeff.Ctx) struct{} {
assert.Equal(t, []int{0, 1, 2, 3, 4, 5}, collect(take(intRange, 6), c))
assert.Equal(t, []int{0, 2, 4, 6, 8, 10}, collect(take(even(intRange), 6), c))
assert.Equal(t, []int{0, 2, 4}, collect(even(take(intRange, 6)), c))
assert.Equal(t, []int{0, 2, 4, 6, 8, 10}, collect(take(double(intRange), 6), c))
assert.Equal(t, []int{0, 4, 8}, collect(double(even(take(intRange, 6))), c))
return struct{}{}
})
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。