从响应中删除重复项

huangapple go评论92阅读模式
英文:

Remove duplicate from response

问题

我有以下任务:我正在创建一个循环,向服务器发送请求并接收一个值,例如一个整数切片。然后我需要将这个整数切片传递给另一个任务。问题是有时我会从服务器接收到重复的值,例如:

resp1 := []int{1,2,3,4} // 做一些操作
resp2 := []int{3,4,5,6} // 做一些操作

但我希望只将响应中的新值传递给下一个任务:

resp1 := []int{1,2,3,4} // 传递 1,2,3,4
resp2 := []int{3,4,5,6} // 传递 5,6

有没有办法避免/清除值中的重复项?

英文:

I have the following task: I'm making loop that doing requests to server and recieving a value, for example it is a slice of ints. Then I need to pass this slice of ints to another task. The thing is that sometime I recieve duplicate value from server, for example:

resp1 := []int{1,2,3,4} > do some things
resp2 := []int{3,4,5,6} > do some things

But I want to pass only NEW values from response to next task

resp1 := []int{1,2,3,4} > pass 1,2,3,4
resp2 := []int{3,4,5,6} > pass 5,6

Is there any solution to avoid / "clean up" duplicates from value?

答案1

得分: 2

一般的想法是维护一个全局的“切片映射”(如mkopriva评论中建议的),它会记录你传递给其他任务的任何值。

还要构建一个新的切片,它会:

  • 对于全局映射中的每个值'x'(来自respx)进行测试:
    • 如果存在,你的新切片将不包括该值
    • 如果不存在,你的新切片将包括该值(并且一个空的结构值将被添加到全局映射中,以'x'作为键)
  • 将新切片(只包含新值,之前从未传递过的值)传递给新任务

关键是:你当前的任务是有状态的,并且需要在每次调用之间保持状态。

你将需要一些形式的全局变量和/或单例模式,即在程序执行期间持续存在的东西。
mkopriva还提到了“visited模式”作为实现示例。

英文:

The general idea would be to maintain a global <del>slice</del> map (as suggested by mkopriva in the comments) which would memorize any value you are passing to your other task.

Build also a new slice which would:

  • test for each value 'x' (of respx) in the global map:
    • if present, your new slice would not include that value
    • if absent, your new slice would include that value (and an empty struct value would be added to your global map, with 'x' as key)
  • pass the new slice (with only new values, never passed before) to the new task

The point is: your current task is stateful and need to maintain a state between each call.

You will need some kind of global variable and/or singleton, something which will span the life of the execution of your program.
mkopriva also mentions the "visited pattern" as implementation examples.

答案2

得分: 1

你可以使用一个映射(map)来存储已经出现过的值,并且通过这个映射来生成每次获取响应时的唯一值。一个获取唯一响应的函数可以像这样:

func getUniqueResponse(curResp []int, seen map[int]bool) []int {
    var resp []int
    for _, num := range curResp {
        if !seen[num] {
            seen[num] = true
            resp = append(resp, num)
        }
    }
    return resp
}

你可以声明一个 map[int]bool,并将该映射与切片一起传递,以获得一个唯一的切片。

seen := make(map[int]bool)
resp := []int{1, 2, 3, 4}
uniqueResp := getUniqueResponse(resp, seen)
// 使用唯一响应进行其他操作
resp = []int{3, 4, 5, 6}
uniqueResp = getUniqueResponse(resp, seen)
// 使用唯一响应进行其他操作

请注意,以上代码是使用Go语言编写的。

英文:

You can store the values you have already seen in a map and using that you can generate unique values each time you get a response. A function to get unique response can be like this :

func getUniqueResponse(curResp []int, seen map[int]bool) []int {
    var resp []int
    for _, num := range curResp {
	    if !seen[num] {
		    seen[num] = true
		    resp = append(resp, num)
	    }
    }
    return resp
}

You can declare a map[int]bool and pass that map along with slice and receive a unique slice.

seen := make(map[int]bool)
resp := []int{1, 2, 3, 4}
uniqueResp := getUniqueResponse(resp, seen)
// Do other work with unique Response
resp = []int{3, 4, 5, 6}
uniqueResp = getUniqueResponse(resp, seen)
// Do other work with unique Response

huangapple
  • 本文由 发表于 2022年8月30日 14:33:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/73538076.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定