Skip to main content
added 443 characters in body
Source Link
nponeccop
  • 1.1k
  • 5
  • 10

Here is another approach to implement toList:

indexList p l = range (p, l - 1) ++ range (0, p - 1)

toList' (FLQ l p a) = map (a !) $ indexList p l

I'm not sure that it's much better though, but the idea can be used everywhere:

eq (FLQ l1 p1 a1) (FLQ l2 p2 a2) = l1 == l2 && comp where
    r1 = indexList p1 l1
    r2 = indexList p2 l2
    comp = Prelude.all (\(i1, i2) -> (a1 ! i1) == (a2 ! i2)) $ zip r1 r2

Here is another approach to implement toList:

indexList p l = range (p, l - 1) ++ range (0, p - 1)

toList' (FLQ l p a) = map (a !) $ indexList p l

I'm not sure that it's much better though, but the idea can be used everywhere:

eq (FLQ l1 p1 a1) (FLQ l2 p2 a2) = l1 == l2 && comp where
    r1 = indexList p1 l1
    r2 = indexList p2 l2
    comp = Prelude.all (\(i1, i2) -> (a1 ! i1) == (a2 ! i2)) $ zip r1 r2
added 171 characters in body
Source Link
nponeccop
  • 1.1k
  • 5
  • 10

Interestingly, Prelude uses "innerList" approach - it converts an array to a list and back:

instance Ix i => Traversable (Array i) where
        traverse f arr = listArray (bounds arr) `fmap` traverse f (elems arr)

So I think the best we can do with Traversable is to use "innerList" approach too:

instance Traversable FixedLengthQueue where
    traverse f = fmap (fromJust . fromList) . traverse f . toList

Here is an Eq instance:

Here is an Eq instance:

Interestingly, Prelude uses "innerList" approach - it converts an array to a list and back:

instance Ix i => Traversable (Array i) where
        traverse f arr = listArray (bounds arr) `fmap` traverse f (elems arr)

So I think the best we can do with Traversable is to use "innerList" approach too:

instance Traversable FixedLengthQueue where
    traverse f = fmap (fromJust . fromList) . traverse f . toList

Here is an Eq instance:

added 171 characters in body
Source Link
nponeccop
  • 1.1k
  • 5
  • 10

Here is an Eq instance:

instance (Eq a) => Eq (FixedLengthQueue a) where
    (FLQ l1 _ _) == (FLQ l2 _ _) | l1 /= l2 = False
    q1 == q2 = toList q1 == toList q2

Here is an Eq instance:

instance (Eq a) => Eq (FixedLengthQueue a) where
    (FLQ l1 _ _) == (FLQ l2 _ _) | l1 /= l2 = False
    q1 == q2 = toList q1 == toList q2
added 311 characters in body
Source Link
nponeccop
  • 1.1k
  • 5
  • 10
Loading
added 305 characters in body
Source Link
nponeccop
  • 1.1k
  • 5
  • 10
Loading
added 120 characters in body
Source Link
nponeccop
  • 1.1k
  • 5
  • 10
Loading
added 6 characters in body
Source Link
nponeccop
  • 1.1k
  • 5
  • 10
Loading
added 659 characters in body
Source Link
nponeccop
  • 1.1k
  • 5
  • 10
Loading
added 132 characters in body
Source Link
nponeccop
  • 1.1k
  • 5
  • 10
Loading
added 132 characters in body
Source Link
nponeccop
  • 1.1k
  • 5
  • 10
Loading
Source Link
nponeccop
  • 1.1k
  • 5
  • 10
Loading