mono-traversable

Type classes for mapping, folding, and traversing monomorphic containers

https://github.com/snoyberg/mono-traversable

Version on this page:0.6.3@rev:2
LTS Haskell 22.43:1.0.20.0
Stackage Nightly 2024-12-05:1.0.20.0
Latest on Hackage:1.0.20.0

See all snapshots mono-traversable appears in

MIT licensed by Michael Snoyman, John Wiegley, Greg Weber
Maintained by [email protected]
This version can be pinned in stack with:mono-traversable-0.6.3@sha256:b6d1d0b8f48138ce8d38b01862c556fa30e72198061f689bd020f8f1fe71d96e,2804

Module documentation for 0.6.3

  • Data
    • Data.ByteVector
    • Data.Containers
    • Data.MinLen
    • Data.MonoTraversable
    • Data.NonNull
    • Data.Sequences

mono-traversable

Type classes for mapping, folding, and traversing monomorphic containers. Contains even more experimental code for abstracting containers and sequences.

A polymorphic container is one such as list which has a type variable [a] A monomorphic container is one such as Text which has a type Text that does not expose the underlying characters.

Adding instances

If you have a data type which is a member of one of the relevant typeclasses (Functor, Foldable, Traversable), its quite easy to add an instance for MonoFunctor, MonoFoldable or MonoTraversable.

You just have to declare the proper type instance:

    {-# LANGUAGE TypeFamilies         #-}
    
    (...)
    
    -- type instance Element T.Text = Char  -- already defined
    -- type instance Element [a] = a        -- here for example
    type instance Element (CustomType a) = a

And then, the needed instances:

    instance MonoFunctor (CustomType a)
    instance MonoFoldable (CustomType a)
    instance MonoTraversable (CustomType a)

in your code, and your ready to use CustomType a with the functions defined in this package.

Note: if your type is as monomorphic container without the proper typeclasses, then you will have to provide an implementation. However, this should be fairly simple, as it can be seen in the code

mono-traversable versuse lens Traversal

lens is a huge package with a lot of functionality. One piece of functionality it exposes is Fold and Traversal which can also be used to deal with monomorphic containers.

You could prefer mono-traversable to using this part of lens because

  • There is really no new API to learn. If you know Foldable, you can use MonoFoldable just as easily
  • mono-traversable’s typeclass based approach means many methods are included in the class but can easily be given specialised optimized implementations
  • You don’t need to explicitly pass around the Traversal

The last point is also a point of inflexibility and points to a use case where you could prefer using a lens Traversal. mono-traversable treats ByteString as a sequence of bytes. If you want to treat it as both bytes and characters, mono-traversable would require a newtype wrapper around ByteString, whereas a lens traversal would just use a different traversal function.

Build Status