Skip to content

Instantly share code, notes, and snippets.

@michaljemala
Created September 6, 2019 20:22
Show Gist options
  • Select an option

  • Save michaljemala/6059f1937dde8e308e4fd364baa2ee98 to your computer and use it in GitHub Desktop.

Select an option

Save michaljemala/6059f1937dde8e308e4fd364baa2ee98 to your computer and use it in GitHub Desktop.

Revisions

  1. michaljemala created this gist Sep 6, 2019.
    36 changes: 36 additions & 0 deletions unordered_slice.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    package main

    import (
    "fmt"

    "hova.dzia.poliev.ka/slice"
    )

    func main() {
    x := slice.Unordered{1, 2, 3}
    y := slice.Unordered{3, 2, 1}
    fmt.Println(slice.Equal(x, y))
    fmt.Println(slice.Match(x, y))

    }
    -- go.mod --
    module hova.dzia.poliev.ka
    -- slice/slice.go --
    package slice

    import (
    "github.com/google/go-cmp/cmp"
    "github.com/stretchr/testify/assert"
    )

    type Unordered []interface{}

    func Equal(x, y interface{}) bool {
    return cmp.Equal(x, y)
    }

    func Match(x, y interface{}) bool {
    return cmp.Equal(x, y, cmp.Comparer(func(x, y Unordered) bool {
    return assert.ElementsMatch(nil, x, y)
    }))
    }