猿问

有没有好的方法来定义幺半群接口?

我是围棋语言的新手。


我想定义像“幺半群”这样的结构。(这是出现在群论中的一个词。


下面是幺半群结构的一个例子。


示例(1) :




type monoid_1 struct{

    val int

}


func op(x,y monoid_1) monoid_1{

    return monoid_1{x.val + y.val}

}


func ide() monoid_1{

    return monoide_1{0}

}

示例(2) :



func max(a,b int) int{

    if a > b{

        return a

    }else{

        return b

    }

}


type monoid_2 struct{

    val int

}


func op(x,y monoid_2) monoid_2{

    return monoid_2{max(x.val,y.val)}

}


func ide() monoid_2{

    return monoide_2{-inf}

}

有什么好方法可以定义monoid_interface?


我想使界面像:


type monoid interface{

    op func(monoid) monoid // mapping_function(monoid,monoid) -> monoid(binary operations in monoid)

    ide () monoid          // identity_function() -> monoid (return Identity element)


}

虽然代码不起作用(只是伪代码)


慕妹3146593
浏览 121回答 2
2回答

Smart猫小萌

您必须遵循接口定义,因为它们是为结构定义的,以实现接口。当接口为您的方法定义返回类型时,您必须在实现该接口时返回幺半群。查看以下情况是否有帮助:monoidoptype monoid interface{    get() int    op(monoid, monoid) monoid   // mapping_function(monoid,monoid) -> monoid(binary operations in monoid)    ide() monoid                // identity_function() -> monoid (return Identity element)}func max(a,b int) int{    if a > b{        return a    }else{        return b    }}type monoid2 struct{    val int}func (m monoid2) get() int {    return m.val}func (monoid2) op(x,y monoid) monoid{    return monoid2{max(x.get(),y.get())}}func (monoid2) ide() monoid{    return monoid2{-math.MaxInt8}}

Qyouu

我成功地定义了幺半群结构。package mainimport (&nbsp; &nbsp; "fmt")type monoid interface {&nbsp; &nbsp; get() interface{}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// type of interface{} depends on each monoid type.&nbsp; &nbsp; op(monoid, monoid) monoid // mapping_function(monoid,monoid) -> monoid(binary operations in monoid)&nbsp; &nbsp; ide() monoid&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // identity_function() -> monoid (return Identity element)}type monoid1 struct {&nbsp; &nbsp; val int&nbsp; &nbsp; sum int}type monoid2 struct {&nbsp; &nbsp; name&nbsp; string&nbsp; &nbsp; power int}func (m monoid2) get() interface{} {&nbsp; &nbsp; return m}func (m monoid2) op(x, y monoid) monoid {&nbsp; &nbsp; a := x.get().(monoid2)&nbsp; &nbsp; b := y.get().(monoid2)&nbsp; &nbsp; if len(a.name) > len(b.name) {&nbsp; &nbsp; &nbsp; &nbsp; return monoid2{a.name, a.power + b.power}&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; return monoid2{b.name, a.power + b.power}&nbsp; &nbsp; }}func (m monoid2) ide() monoid {&nbsp; &nbsp; return monoid2{"", 0}}func (m monoid1) get() interface{} {&nbsp; &nbsp; return m}func (m monoid1) op(x, y monoid) monoid {&nbsp; &nbsp; a := x.get().(monoid1)&nbsp; &nbsp; b := y.get().(monoid1)&nbsp; &nbsp; return monoid1{a.val + b.val, a.sum + b.sum}}func (m monoid1) ide() monoid {&nbsp; &nbsp; return monoid1{0, 0}}func main() {&nbsp; &nbsp; a := []monoid{monoid2{"Jame", 100}, monoid2{"Tom", 1010}, monoid2{"BOB SMITH", 1111}, monoid1{1, 1}, monoid1{2, 2}, monoid1{3, 3}}&nbsp; &nbsp; b := []monoid{monoid2{"Trump", 111}, monoid2{"MaryJames", 1234}, monoid2{"Cachy", 123245}, monoid1{1, 1}, monoid1{2, 2}, monoid1{3, 3}}&nbsp; &nbsp; for i := 0; i < 6; i++ {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(a[i].op(b[i], a[i]))&nbsp; &nbsp; }&nbsp; &nbsp; return}
随时随地看视频慕课网APP

相关分类

Go
我要回答