最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

go - How to map an iter.Seq from one item type to a different one? - Stack Overflow

programmeradmin1浏览0评论

I'm able to capture the input generic sequence type in order to return it like so:

type Seq[V any] interface {
    ~func(yield func(V) bool)
}

func Filter[V any, SV Seq[V]](s SV, filterFn func(V) bool) SV {
    return func(yield func(V) bool) {
        for item := range s {
            if filterFn(item) {
                if !yield(item) {
                    return
                }
            }
        }
    }
}

However, I can't find a way to do this when when the underlying item type changes as per the below example:

func Map[V, U any, SV Seq[V]](s SV, mapFn func(V) U) func(yield func(U) bool) {
    // Returning the underlying type as it is a cheap implicit conversion 
    
    return func(yield func(U) bool) {
        for item := range s {
            if !yield(mapFn(item)) {
                return
            }
        }
    }
}

Is it possible to capture the input sequence type in order to return it over a new item type? Or is this implementation my next best option?

I'm able to capture the input generic sequence type in order to return it like so:

type Seq[V any] interface {
    ~func(yield func(V) bool)
}

func Filter[V any, SV Seq[V]](s SV, filterFn func(V) bool) SV {
    return func(yield func(V) bool) {
        for item := range s {
            if filterFn(item) {
                if !yield(item) {
                    return
                }
            }
        }
    }
}

However, I can't find a way to do this when when the underlying item type changes as per the below example:

func Map[V, U any, SV Seq[V]](s SV, mapFn func(V) U) func(yield func(U) bool) {
    // Returning the underlying type as it is a cheap implicit conversion 
    
    return func(yield func(U) bool) {
        for item := range s {
            if !yield(mapFn(item)) {
                return
            }
        }
    }
}

Is it possible to capture the input sequence type in order to return it over a new item type? Or is this implementation my next best option?

Share Improve this question edited Feb 4 at 8:50 blackgreen 45.2k28 gold badges160 silver badges155 bronze badges asked Feb 4 at 7:10 richardpjrichardpj 4314 silver badges11 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 4

You don't need to declare a generic interface for iterators. iter.Seq[V] is already generic.

Therefore, you simply rewrite your functions as follows:

func Filter[V any](s iter.Seq[V], filterFn func(V) bool) iter.Seq[V] {
    return func(yield func(V) bool) {
        for item := range s {
            if filterFn(item) {
                if !yield(item) {
                    return
                }
            }
        }
    }
}

And

func Map[V, U any](s iter.Seq[V], mapFn func(V) U) iter.Seq[U] {
    return func(yield func(U) bool) {
        for item := range s {
            if !yield(mapFn(item)) {
                return
            }
        }
    }
}

Playground: https://go.dev/play/p/blX4HgqOwWl

发布评论

评论列表(0)

  1. 暂无评论