lens package:microlens

lens creates a Lens from a getter and a setter. The resulting lens isn't the most effective one (because of having to traverse the structure twice when modifying), but it shouldn't matter much. A (partial) lens for list indexing:
ix :: Int -> Lens' [a] a
ix i = lens (!! i)                                   -- getter
(\s b -> take i s ++ b : drop (i+1) s)   -- setter
Usage:
>>> [1..9] ^. ix 3
4

>>> [1..9] & ix 3 %~ negate
[1,2,3,-4,5,6,7,8,9]
When getting, the setter is completely unused; when setting, the getter is unused. Both are used only when the value is being modified. For instance, here we define a lens for the 1st element of a list, but instead of a legitimate getter we use undefined. Then we use the resulting lens for setting and it works, which proves that the getter wasn't used:
>>> [1,2,3] & lens undefined (\s b -> b : tail s) .~ 10
[10,2,3]
Lens s t a b is the lowest common denominator of a setter and a getter, something that has the power of both; it has a Functor constraint, and since both Const and Identity are functors, it can be used whenever a getter or a setter is needed.
  • a is the type of the value inside of structure
  • b is the type of the replaced value
  • s is the type of the whole structure
  • t is the type of the structure after replacing a in it with b
This is a type alias for monomorphic lenses which don't change the type of the container (or of the value inside).
LensLike is a type that is often used to make combinators as general as possible. For instance, take (<<%~), which only requires the passed lens to be able to work with the (,) a functor (lenses and traversals can do that). The fully expanded type is as follows:
(<<%~) :: ((a -> (a, b)) -> s -> (a, t)) -> (a -> b) -> s -> (a, t)
With LensLike, the intent to use the (,) a functor can be made a bit clearer:
(<<%~) :: LensLike ((,) a) s t a b -> (a -> b) -> s -> (a, t)
A type alias for monomorphic LensLikes.
A tiny lens library with no dependencies NOTE: If you're writing an app, you probably want microlens-platform – it has the most features. microlens is intended more for library writers who want a tiny lens library (after all, lenses are pretty useful for everything, not just for updating records!). This library is an extract from lens (with no dependencies). It's not a toy lenses library, unsuitable for “real world”, but merely a small one. It is compatible with lens, and should have same performance. It also has better documentation. There's a longer readme on Github. It has a migration guide for lens users, a description of other packages in the family, a discussion of other lens libraries you could use instead, and so on. Here are some usecases for this library:
  • You want to define lenses or traversals in your own library, but don't want to depend on lens. Having lenses available often make working with a library more pleasant.
  • You just want to be able to use lenses to transform data (or even just use over _1 to change the first element of a tuple).
  • You are new to lenses and want a small library to play with.
However, don't use this library if:
  • You need Isos, Prisms, indexed traversals, or actually anything else which isn't defined here (though some indexed functions are available elsewhere – containers and vector provide them for their types, and ilist provides indexed functions for lists).
  • You want a library with a clean, understandable implementation (in which case you're looking for lens-simple).
As already mentioned, if you're writing an application which uses lenses more extensively, look at microlens-platform – it combines features of most other microlens packages (microlens-mtl, microlens-th, microlens-ghc). If you want to export getters or folds and don't mind the contravariant dependency, please consider using microlens-contra. If you haven't ever used lenses before, read this tutorial. (It's for lens, but it applies to microlens just as well.) Note that microlens has no dependencies starting from GHC 7.10 (base-4.8). Prior to that, it depends on transformers-0.2 or above.