MapWith: mapWith: like fmap, but with additional parameters (isFirst, isLast, etc).

This is a package candidate release! Here you can preview how this package release will appear once published to the main package index (which can be accomplished via the 'maintain' link below). Please note that once a package has been published to the main package index it cannot be undone! Please consult the package uploading documentation for more information.

[maintain] [Publish]

fmap over Traversables (including lists), but pass additional parameters to the map function, such as isFirst, isLast, prevElt, nextElt, index from start or end, custom params. For examples see

[Skip to Readme]


Change log
Dependencies base (>=4.9.1 && <4.15) [details]
License BSD-3-Clause
Copyright (c) David James, 2020.
Author David James
Category Combinators
Home page
Source repo head: git clone
Uploaded by davtjames at 2020-08-25T12:29:29Z


[Index] [Quick Jump]


Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Readme for MapWith-

[back to package description]

MapWith Stackage version Hackage version build status

mapWith: like fmap, but can "inject" additional parameters such as whether first (or last) element, etc.


I often want to map over a list, but do something slightly different with the first or last element.

For a long time I used markbounds, but also wanted something that:

So, after only 2 years, I built a small library to do all of these.


Passing a "standard combination" of isFirst and isLast parameters:

let g x f l = [star f, x, star l]; star b = if b then '*' else ' '
in withFirstLast g "fred"
["*f ", " r ", " e ", " d*"]

Passing a custom combination of different types of parameter (the index from the start, whether it's the last element, and elements from another list applied from the right):

let g x n l e = concat [[x], show n, if l then "*" else "-", e]
in mapWith (g ^-> eltIx & isLast <-^ eltFrom ["x","yy","z","zzzz","y"]) "fred"

More examples are here.


Note that this is my first library and my first use of cabal, so I've probably done some dumb things.

Some things I wonder:

Future Work

Areas for potential improvement in later releases: