【问题标题】:Is there any good way to define Monoid interface? [closed]有什么好的方法来定义 Monoid 接口吗? [关闭]
【发布时间】:2021-10-15 10:44:16
【问题描述】:

我是 Go 语言的新手。

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

这是 Monoid-Structure 的一个示例。

示例(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)

}

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

【问题讨论】:

  • 您展示的两个结构是相同的。如果您修复语法错误,这将起作用。 Go 中的接口是一组方法。这个幺半群需要什么方法?
  • 我想要这样的接口: type monoid interface{ op func(monoid) monoid // mapping_function(monoid,monoid) -> monoid(monoid 中的二元运算) ide () monoid // identity_function( ) -> monoid (return Identity element) } 虽然代码不起作用(这只是伪代码)
  • 在这种情况下定义带有接收者的方法(op,ide),并将return类型和arg类型更改为monoid。
  • type monoid interface { Op(monoid) monoid; ide() monoid }
  • golang.org/doc/faq#go_or_golang 不过,你能添加一个链接到什么是幺半群吗?

标签: go math interface


【解决方案1】:

您必须遵循接口定义,因为它们是为 struct 定义的以实现接口。当接口为您的op 方法定义返回类型为monoid 时,您必须在实现该接口时返回monoid。看看以下是否有帮助:

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

【讨论】:

    【解决方案2】:

    感谢大家的帮助和善意!

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

    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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-05
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多