为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

Go
慕妹3146593 2022-09-19 17:47:56
我是围棋语言的新手。我想定义像“幺半群”这样的结构。(这是出现在群论中的一个词。下面是幺半群结构的一个例子。示例(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)}虽然代码不起作用(只是伪代码)
查看完整描述

2 回答

?
Smart猫小萌

TA贡献1911条经验 获得超7个赞

您必须遵循接口定义,因为它们是为结构定义的,以实现接口。当接口为您的方法定义返回类型时,您必须在实现该接口时返回幺半群。查看以下情况是否有帮助:monoidop


type 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}

}


查看完整回答
反对 回复 2022-09-19
?
Qyouu

TA贡献1786条经验 获得超11个赞

我成功地定义了幺半群结构。


package main


import (

    "fmt"

)


type monoid interface {

    get() interface{}         // type of interface{} depends on each monoid type.

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

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

}


type monoid1 struct {

    val int

    sum int

}


type monoid2 struct {

    name  string

    power int

}


func (m monoid2) get() interface{} {

    return m

}


func (m monoid2) op(x, y monoid) monoid {

    a := x.get().(monoid2)

    b := y.get().(monoid2)

    if len(a.name) > len(b.name) {

        return monoid2{a.name, a.power + b.power}

    } else {

        return monoid2{b.name, a.power + b.power}

    }

}


func (m monoid2) ide() monoid {

    return monoid2{"", 0}

}


func (m monoid1) get() interface{} {

    return m

}

func (m monoid1) op(x, y monoid) monoid {

    a := x.get().(monoid1)

    b := y.get().(monoid1)

    return monoid1{a.val + b.val, a.sum + b.sum}

}


func (m monoid1) ide() monoid {

    return monoid1{0, 0}

}


func main() {

    a := []monoid{monoid2{"Jame", 100}, monoid2{"Tom", 1010}, monoid2{"BOB SMITH", 1111}, monoid1{1, 1}, monoid1{2, 2}, monoid1{3, 3}}

    b := []monoid{monoid2{"Trump", 111}, monoid2{"MaryJames", 1234}, monoid2{"Cachy", 123245}, monoid1{1, 1}, monoid1{2, 2}, monoid1{3, 3}}

    for i := 0; i < 6; i++ {

        fmt.Println(a[i].op(b[i], a[i]))

    }

    return

}


查看完整回答
反对 回复 2022-09-19
  • 2 回答
  • 0 关注
  • 66 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信