Created
March 27, 2022 19:18
-
-
Save fogfish/d8196f7d38088acfa444117944961595 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
SeqEq is a heterogeneous product of Seq and Eq laws. | |
It composes two types together that "knows" how to compare sequences. | |
*/ | |
type SeqEq[S, T any] struct { | |
Seq[S, T] | |
Eq[T] | |
} | |
// implements equality rule for sequence using Seq & Eq type classes. | |
func (seq SeqEq[S, T]) Equal(a, b S) bool { | |
seqA := a | |
seqB := b | |
for !seq.Seq.IsVoid(seqA) && !seq.Seq.IsVoid(seqB) { | |
headA := seq.Seq.Head(seqA) | |
headB := seq.Seq.Head(seqB) | |
if headA == nil || headB == nil || !seq.Eq.Equal(*headA, *headB) { | |
return false | |
} | |
seqA = seq.Seq.Tail(seqA) | |
seqB = seq.Seq.Tail(seqB) | |
} | |
return seq.Seq.IsVoid(seqA) && seq.Seq.IsVoid(seqB) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment