Monoid接口有什么好的定义方法吗? [关闭]

问题描述 投票:0回答:2

我想定义像“Monoid”这样的结构。 (这是出现在Group-Theory中的一个词。)

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

例子(1):

type monoid_sum struct{
    val int
}

func op(x,y monoid_sum) monoid_sum {
    return monoid_sum{x.val + y.val}
}

func ide() monoid_sum{
    return monoid_sum{0}
}

例子(2):

import "math"

func max(a,b int) int{
    if a > b{
            return a
    }else{
            return b
    }
}

type monoid_max struct {
    val int
}

func op(x,y monoid_max) monoid_max {
    return monoid_max{max(x.val,y.val)}
}

func ide() monoid_max {
    return monoid_max{math.MinInt}
}

有什么好的方法可以定义幺半群接口吗?我想做一个这样的界面:

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 math interface
2个回答
0
投票

您必须遵循接口定义,因为它们是为结构定义的,以实现接口。当接口为您的

monoid
方法定义返回类型为
op
时,您必须在实现该接口时返回 monoid in。看看以下是否有帮助:

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


0
投票

谢谢大家的帮助和好意!

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

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
}
© www.soinside.com 2019 - 2024. All rights reserved.