亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

有沒有好的方法來定義幺半群接口?

有沒有好的方法來定義幺半群接口?

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 關注
  • 111 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號