Preface
Goal: Alternative query using Koazee, go-linq or go-funk.
6: Using Third Party Module
There is however other way to make our life simpler.
I won’t give much talk, just the code should be enough.
Reference
I don’t which one is the best. All works for me.
The choice is your.
Koazee
package main
import (
"fmt"
"example.com/mysongs"
"github.com/wesovilabs/koazee"
)
var tags = []string{
"rock", "jazz", "rock", "pop", "pop"}
func main() {
var stream = koazee.StreamOf(mysongs.GetSongs())
stream = stream.Map(
func(song mysongs.Song) []string {
return song.Tags
}).Filter(
func(tags []string) bool {
return tags != nil
}).Do()
var flatten = stream.Reduce(
func(acc, tags []string) []string {
return append(acc, tags...)
})
stream = koazee.StreamOf(flatten.Val()).
RemoveDuplicates()
fmt.Printf("stream : %v\n", stream.Out().Val())
}
With the result similar as below slices
.
$ go run 14-koazee.go
stream : [60s jazz rock 70s pop]
Go Linq
package main
import "fmt"
import . "example.com/mysongs"
import . "github.com/ahmetb/go-linq/v3"
func main() {
var tags []string
From(GetSongs()).
WhereT(func(song Song) bool {
return song.Tags != nil
}).SelectT(func(song Song) []string {
return song.Tags
}).SelectMany(func(tag interface{}) Query {
return From(tag)
}).Distinct().ToSlice(&tags)
fmt.Println(tags)
}
With the result similar as below slices
.
$ go run 15-linq.go
[60s jazz rock 70s pop]
Go Funk
package main
import "fmt"
import "example.com/mysongs"
import "github.com/thoas/go-funk"
func main() {
var tags []string
tags = funk.
Chain(mysongs.GetSongs()).
Filter(func(song mysongs.Song) bool {
return song.Tags != nil
}).Map(func(song mysongs.Song) []string {
return song.Tags
}).FlattenDeep().
Uniq().
Value().([]string)
fmt.Println(tags)
}
With the result similar as below slices
.
>}}
$ go run 16-funk.go
[60s jazz rock 70s pop]
I think that is all.
What is Next 🤔?
Consider continue reading [ Rust - Playing with Records - Part One ].