Uploaded by
Published on

Build status

Type class for data structures which can be unconsed + some utilities built on top of it.

uncons comes in so many different flavors and is so common that it is obvious that we need a type class for it:

class Unconsable t where
  uncons :: forall a. t a -> Maybe { head :: a, tail :: t a }

Comparing sizes of foldable structures

Comparing sizes of foldable structures by computing their lengths using length :: forall a b f. Foldable f => Semiring b => f a -> b is not very effective. In fact, it is not required to evaluate both lengths to get the Ordering value: it is more optimal to traverse both structures simultaneously and return after the end of the shortest one. Foldable does not allow to do this, but with Unconsable it is possible.

Make sure that your types are instances of Unconsable class and refactor your code using the following rules:

Before After
length a < length b isShorterThan a b
length a > length b isLongerThan a b
length a == length b isOfSameLength a b
length a > n longerThan a n
length a < n shorterThan a n
length a == n hasLength a n

Module documentation is published on Pursuit.