dongqian6234 2016-05-17 15:05
浏览 51
已采纳

Golang容器/列表创建FindAll函数

I was wondering if this is the way to create and pass 'generic'(yeah I know, a sensitive word in GoLang) lists to a FindAll function.

Here's my attempt:

package main

import (
    "container/list"
    "fmt"
    "strings"
)

func FindAll(lst *list.List, p func(interface{}) bool) *list.List {
    ans := list.New()

    for i := lst.Front(); i != nil; i = i.Next() {
        if p(i.Value) {
            ans.PushBack(i.Value)
        }
    }
    return ans
}

func ConvertToInt(p func(int) bool) func(interface{}) bool {
    return func(v interface{}) bool {
        if value, ok := v.(int); ok {
            if p(value) {
                return true
            } else {
                return false
            }
        } else {
            return false
        }
    }
}

func IsEven(n int) bool {
    if n%2 == 0 {
        return true
    }
    return false
}

func ConvertoString(p func(s string) bool) func(interface{}) bool {
    return func(v interface{}) bool {
        if value, ok := v.(string); ok {
            if p(value) {
                return true
            } else {
                return false
            }
        } else {
            return false
        }
    }
}

func IsHello(str string) bool {
    if strings.ToLower(str) == "hello" {
        return true
    } else {
        return false
    }
}

func main() {
    fmt.Println("Find All Programs!

")

    lsti := list.New()

    for i := 0; i < 11; i++ {
        lsti.PushBack(i)
    }

    ansIsEven := FindAll(lsti, ConvertToInt(IsEven))

    for i := ansIsEven.Front(); i != nil; i = i.Next() {
        if value, ok := i.Value.(int); ok {
            fmt.Printf("Found even: %d
", value)
        } else {
            fmt.Println("Huh! What's that?")
        }
    }

}

I've been playing with this for a while and thought I'd better get the advice of the Go experts before I convince myself its correct.

  • 写回答

1条回答 默认 最新

  • dpikoto468637 2016-05-18 00:05
    关注

    The code as-is is pretty fine, but you should ask your self 2 questions:

    1. Why shouldn't you use a typed slice? (interface{} performance is slow compared to the explicit type, although it will greatly improve in Go 1.7)

    2. Would it be better to implement your specific type as a linked list?

    Something like this can be much more efficient:

    type IntList []int
    
    func (l IntList) Filter(fn func(v int) bool) IntList {
        var o IntList
        for _, v := range l {
            if fn(v) {
                o = append(o, v)
            }
        }
        return o
    }
    

    There's almost always a better alternative to container/list, however it all depends on your use case.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧