Re: [go-nuts] Imitating tuple in golang

2024-08-11 Thread Ian Lance Taylor
On Sun, Aug 11, 2024 at 5:35 AM 'Brian Candler' via golang-nuts wrote: > > Are there many instances where interpretation (1) is useful? Yes. > I would also observe: if Go implemented case (2) for "==", this doesn't > prevent the user from implementing case (1) using a separate function, as > t

Re: [go-nuts] Imitating tuple in golang

2024-08-11 Thread Jochen Voss
Hi Brian, One instance where (1) would be useful is, when trying to implement a PostScript interpreter. The Postscript "eq" operator has to test whether two PostScript arrays share the same storage, rather than whether they contain the same values [*]. If you can do (1), you can use Go slices

Re: [go-nuts] Imitating tuple in golang

2024-08-11 Thread Jochen Voss
Hi Ian, Just out of interest: if I choose that I mean equality 1 (same length and same underlying storage), how would I perform the test for equality? Does this require unsafe.Pointer, or is there a more proper way? All the best, Jochen On Sunday 11 August 2024 at 00:08:26 UTC+1 Ian Lance Tay

Re: [go-nuts] Imitating tuple in golang

2024-08-11 Thread 'Brian Candler' via golang-nuts
Are there many instances where interpretation (1) is useful? I would also observe: if Go implemented case (2) for "==", this doesn't prevent the user from implementing case (1) using a separate function, as today. Supplementary question: does the Go standard library even expose a function for

Re: [go-nuts] Imitating tuple in golang

2024-08-10 Thread Ian Lance Taylor
On Fri, Aug 9, 2024 at 9:03 PM 'Brian Candler' via golang-nuts wrote: > > I would agree, except there is no == operator defined on slices, and I'm not > really sure why that is. The semantics I would expect are straightforward: > i.e. length is the same, and the first 'len' slice elements compar

Re: [go-nuts] Imitating tuple in golang

2024-08-10 Thread glenn stevenson
@Gergely Brautigam its .com not .con as in your post , pls edit On Friday, August 9, 2024 at 6:57:33 AM UTC+1 Gergely Brautigam wrote: Hello. I actually implemented a Tupke logic here https://github.con/Skarlso/tuple. I find this a much better way to deal with tuples instead of the indexing

Re: [go-nuts] Imitating tuple in golang

2024-08-10 Thread p...@morth.org
I think when discussing tuples one should consider the [...]any type as an alternative to []any. Arrays supports comparison, aren't boxed and are generally more tuple-like. They can be used as map keys. Of course, they're still not really type-safe. Structs solve that and also support access by

Re: [go-nuts] Imitating tuple in golang

2024-08-10 Thread 'lijh8' via golang-nuts
I add nested tuple (slice) support:     a := []any{"abc", 123, 3.14, 100, []any{"abc", 123, 3.14, 100}}     b := []any{"abc", 123, 3.14, 100, []any{"abc", 123, 3.14, 100}}     c, ok := tuple2.Cmp(a, b)     fmt.Println(c, ok) ``` package tuple2 import (     "cmp"     "reflect" ) func Cmp

Re: [go-nuts] Imitating tuple in golang

2024-08-09 Thread 'Brian Candler' via golang-nuts
On Friday 9 August 2024 at 06:19:22 UTC+6 Jolyon Direnko-Smith wrote: Second, you don't need to implement a custom == operator to compare custom struct types; the implementation of == in Golang already covers the comparison of structured value types in a logical, intuitive and type-safe manner.

Re: [go-nuts] Imitating tuple in golang

2024-08-09 Thread 'lijh8' via golang-nuts
Hi community, I have got this now. It handles only built-in types: string, int, float64. The tuples (slices) should be the same size. Type of elements in same position should be the same. I still need help on this: The part involves cmp.Compare repeates three times for string, int, float64. I

Re: [go-nuts] Imitating tuple in golang

2024-08-08 Thread Gergely Brautigam
My apologies the link is incorrect. It should be https://github.com/Skarlso/tuple Hopefully it’s somewhat of a help in pursuing this topic. On Friday 9 August 2024 at 07:57:33 UTC+2 Gergely Brautigam wrote: > Hello. > > I actually implemented a Tupke logic here https://github.con/Skarlso/tuple

Re: [go-nuts] Imitating tuple in golang

2024-08-08 Thread Gergely Brautigam
My apologies for the link. Google groups on mobile isn’t really working. https://girhub.com/Skarlso/tuple On Friday 9 August 2024 at 07:57:33 UTC+2 Gergely Brautigam wrote: > Hello. > > I actually implemented a Tupke logic here https://github.con/Skarlso/tuple > . > > I find this a much better

Re: [go-nuts] Imitating tuple in golang

2024-08-08 Thread Gergely Brautigam
Hello. I actually implemented a Tupke logic here https://github.con/Skarlso/tuple. I find this a much better way to deal with tuples instead of the indexing logic. And it’s using generics as well. Feel free to borrow stuff. 😊 On Friday 9 August 2024 at 02:19:22 UTC+2 Jolyon Direnko-Smith wrote

Re: [go-nuts] Imitating tuple in golang

2024-08-08 Thread Jolyon Direnko-Smith
My $0.02 on this First, to address the use cases in your question, Golang does not have custom operators so the value of tuples in assisting with their implementation is moot. (and, IMHO, should resist incorporating them; someone should not have to consult the source for a type to understand w

Re: [go-nuts] Imitating tuple in golang

2024-08-08 Thread Jan Mercl
On Thu, Aug 8, 2024 at 9:35 PM 'lijh8' via golang-nuts wrote: > I try to use slice of any to imitate the tuple type, > and use this function to compare two slices: a, b. > > How can I improve it? Take a look at https://pkg.go.dev/slices#Compare if it can fit your use case. -- You received this

[go-nuts] Imitating tuple in golang

2024-08-08 Thread 'lijh8' via golang-nuts
Hi community, I try to use slice of any to imitate the tuple type, and use this function to compare two slices: a, b. How can I improve it? Thanks ``` package tuple2 func Cmp(a, b []any) (int, bool) { for i := 0; i != min(len(a), len(b)); i++ { if a[i] == nil && b[i]