Skip to content

Instantly share code, notes, and snippets.

@export-mike
Created June 25, 2021 06:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save export-mike/4d7099c36b8f2095784dea7e142b9e3c to your computer and use it in GitHub Desktop.
Save export-mike/4d7099c36b8f2095784dea7e142b9e3c to your computer and use it in GitHub Desktop.
rolo
package main
import (
// "bytes"
"fmt"
)
func main() {
type Mapping struct {
symbol string
exchange string
}
currentMappings := []Mapping{
{
symbol: "NOVN",
exchange: "EBS",
},
{
symbol: "NOVN",
exchange: "EBS",
},
{
symbol: "NOVN",
exchange: "VIRTX",
},
{
symbol: "NOVNz",
exchange: "VIRTX",
},
}
fmt.Println("current ", currentMappings)
stringKeyMapping := make(map[string]string)
for _, mapping := range currentMappings {
stringKeyMapping[mapping.symbol] = mapping.exchange
}
structKeyMapping := make(map[Mapping]Mapping)
for _, mapping := range currentMappings {
structKeyMapping[mapping] = mapping
}
incomingMappings := []Mapping{
{
symbol: "NOVN",
exchange: "EBS",
},
{
symbol: "NOVN",
exchange: "EBS",
},
{
symbol: "NOVN",
exchange: "VIRTX",
},
{
symbol: "NOVNz",
exchange: "VIRTX",
},
{
symbol: "NVS",
exchange: "NYSE",
},
}
fmt.Println("incoming", incomingMappings)
var deduplicated []Mapping
for _, mapping := range incomingMappings {
val, ok := stringKeyMapping[mapping.symbol]
if ok && val == mapping.exchange {
continue
}
fmt.Println(fmt.Sprintf("val (%v), ok(%v) := stringKeyMapping[%v]; ok && val == %v", val, ok, mapping.symbol, mapping.exchange))
deduplicated = append(deduplicated, mapping)
}
fmt.Println("string key method deduplicated", deduplicated)
fmt.Println("struct key map", structKeyMapping)
var structKeyDeduplicated []Mapping
for _, mapping := range incomingMappings {
if _, ok := structKeyMapping[mapping]; ok {
continue
}
structKeyDeduplicated = append(structKeyDeduplicated, mapping)
}
fmt.Println("struct key method deduplicated", structKeyDeduplicated)
}
@export-mike
Copy link
Author

mike@Mikes-MacBook-Pro examples % go run maps.go
current  [{NOVN EBS} {NOVN EBS} {NOVN VIRTX} {NOVNz VIRTX}]
incoming [{NOVN EBS} {NOVN EBS} {NOVN VIRTX} {NOVNz VIRTX} {NVS NYSE}]
val (VIRTX), ok(true) := stringKeyMapping[NOVN]; ok && val == EBS
val (VIRTX), ok(true) := stringKeyMapping[NOVN]; ok && val == EBS
val (), ok(false) := stringKeyMapping[NVS]; ok && val == NYSE
string key method deduplicated [{NOVN EBS} {NOVN EBS} {NVS NYSE}]
struct key map map[{NOVN EBS}:{NOVN EBS} {NOVN VIRTX}:{NOVN VIRTX} {NOVNz VIRTX}:{NOVNz VIRTX}]
struct key method deduplicated [{NVS NYSE}]

@export-mike
Copy link
Author

Notice VIRTX is a value twice?

@Zhenwood
Copy link

Worth noting that gists are public (I think)
Maybe worth putting in confluence somewhere instead.

(Not that this is particularly sensitive information... but gists in general maybe not great for work)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment