The 'diff' package you showed identifies the changed object index by means 
of a "Path" attribute in the Changelog entry. If the top level object is a 
slice, then Atoi(change.Path[0]) is the index into the slice.

On Friday, 14 July 2023 at 10:47:47 UTC+1 Mark wrote:

> Hi Brian,
> Your code certainly identifies the different items.
> However, that's not a diff tool in the sense I mean.
> Unix diff and tools like it don't just say x[i] != y[i], they find the 
> longest common subsequences and in essence produce a series of edit 
> commands that would turn slice x into slice y.
> There are quite a few go diff tools that will do this, including my own 
> <https://pkg.go.dev/github.com/mark-summerfield/go-diff> based on 
> Python's difflib sequence matcher.
> What I want to do is find one that does this for slices of structs where 
> only one struct field is considered for comparison purposes.
>
> On Friday, July 14, 2023 at 10:00:34 AM UTC+1 Brian Candler wrote:
>
>> I forgot you wanted generics:
>> https://go.dev/play/p/PhGVjsWWTdB
>>
>> On Friday, 14 July 2023 at 09:47:21 UTC+1 Brian Candler wrote:
>>
>>> You seem to be saying "if the S field is different then I want to 
>>> consider these two structs different, and get pointers to the two structs. 
>>> If the S field is the same then I want to skip the pair entirely". Is that 
>>> right?
>>>
>>> The required semantics are not entirely clear, but it sounds like a 
>>> handful of lines of code to implement - there's no point importing and 
>>> learning a third party library.
>>>
>>> On the assumption that all the elements to be compared are in 
>>> corresponding positions in a and b:
>>> https://go.dev/play/p/Y71sLUpftzR
>>>
>>> On Friday, 14 July 2023 at 09:11:35 UTC+1 Mark wrote:
>>>
>>>> In fact the diff pkg mentioned above does work but is of no use to me 
>>>> since for each change it gives back only the field(s) used, not the 
>>>> original structs (or pointers to them), so I can't see any way back to the 
>>>> original structs (or their slice indexes).
>>>>
>>>> On Friday, July 14, 2023 at 8:58:41 AM UTC+1 Mark wrote:
>>>>
>>>>> What I really want to do is to be able to diff slices of structs on 
>>>>> the basis of one single field.
>>>>> For example, given:
>>>>> ```
>>>>> type Item struct {
>>>>>   I int
>>>>>   S string
>>>>> }
>>>>> ```
>>>>> and given `a` and `b` are both of type`[]Item`, I want to diff these 
>>>>> slices based purely on the `S` field, ignoring the `I` field.
>>>>>
>>>>> This diff pkg <https://pkg.go.dev/github.com/r3labs/diff/v3> claims 
>>>>> to be able to do this (something I'm testing, so I don't know either way 
>>>>> yet), but in any case, it is incredibly slow.
>>>>>
>>>>> On Friday, July 14, 2023 at 8:31:39 AM UTC+1 Peter Galbavy wrote:
>>>>>
>>>>>> As a slight digression - I thought I was going mad, but 'slices' and 
>>>>>> 'maps' are new :-) Only in 1.21 though...
>>>>>>
>>>>>> Well, there is a lot of boiler plate that maps.Keys() will get rid of.
>>>>>>
>>>>>> On Thursday, 13 July 2023 at 10:06:01 UTC+1 Brian Candler wrote:
>>>>>>
>>>>>>> Structs are already comparable, but all fields must be the same:
>>>>>>> https://go.dev/play/p/XwhSz4DEDwL
>>>>>>>
>>>>>>> I think your solution with function 'eq' is fine.  You can see the 
>>>>>>> same thing in the standard library in slices.CompactFunc and 
>>>>>>> slices.EqualFunc
>>>>>>> https://pkg.go.dev/slices#CompactFunc
>>>>>>> https://pkg.go.dev/slices#EqualFunc
>>>>>>>
>>>>>>> For the case of "ordered" rather than "comparable", have a look at 
>>>>>>> slices.BinarySearchFunc and related functions.
>>>>>>>
>>>>>>> On Thursday, 13 July 2023 at 09:29:38 UTC+1 Mark wrote:
>>>>>>>
>>>>>>>> I have a package which has a function `Do[T comparable](a, b []T) 
>>>>>>>> Result`.
>>>>>>>> I have a struct:
>>>>>>>> ```go
>>>>>>>> type N struct {
>>>>>>>>   x int
>>>>>>>>   y int
>>>>>>>>   t string
>>>>>>>> }
>>>>>>>> ```
>>>>>>>> Is it possible to make `N` comparable; in particular by a field of 
>>>>>>>> my choice, e.g., `t`?
>>>>>>>>
>>>>>>>> Or will I have to make, say, `DoFunc(a, b []N, eq func(i, j N) 
>>>>>>>> bool) Result` with, say,
>>>>>>>> `func eq(i, j N) { return i.t == j.t }`?
>>>>>>>>
>>>>>>>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/5cc6237b-1dcc-49f8-b426-69edf2405484n%40googlegroups.com.

Reply via email to