https://github.com/brandonhamilton/ilist
ℹ️ Index-related functions for lists
https://github.com/brandonhamilton/ilist
hacktoberfest haskell-library indexed list
Last synced: 4 months ago
JSON representation
ℹ️ Index-related functions for lists
- Host: GitHub
- URL: https://github.com/brandonhamilton/ilist
- Owner: brandonhamilton
- License: mpl-2.0
- Created: 2016-05-14T09:27:56.000Z (about 10 years ago)
- Default Branch: main
- Last Pushed: 2021-11-15T22:59:01.000Z (over 4 years ago)
- Last Synced: 2025-12-13T01:44:50.428Z (6 months ago)
- Topics: hacktoberfest, haskell-library, indexed, list
- Language: Haskell
- Homepage: https://hackage.haskell.org/package/ilist
- Size: 68.4 KB
- Stars: 12
- Watchers: 3
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# ilist
[](https://github.com/brandonhamilton/ilist/actions)
[](https://hackage.haskell.org/package/ilist)
[](http://stackage.org/lts/package/ilist)
[](http://stackage.org/nightly/package/ilist)
[](LICENSE)
## What is this
This is a library with lots of list functions that are related to indices. It has often-reinvented `deleteAt`, `setAt`, etc, as well as indexed variants of functions from `Data.List` (e.g. `imap`, `ifilter`, `izipWith`). It has no dependencies, builds in about a second, and works on GHC from 7.4 to 8.0; the functions are [optimised](https://github.com/brandonhamilton/ilist/blob/main/lib/Data/List/Index.hs) and benchmarked (for instance, the `zip [0..]` idiom is usually twice as slow, and sometimes 20× as slow).
So, this library is intended to be the canonical place for index-related functions. You are encouraged to depend on this library instead of reinventing the functions, using `zip [0..]`, or using [lens](hackage.haskell.org/package/lens) when all you need is a simple `imap` or `ifoldr` (not to mention that lens variants are usually 2–10 times slower for lists).
## Why should you care
You shouldn't, actually. This is a small library, it won't change anyone's life, and if you care about speed you probably shouldn't be using lists anyway (unless you keep your fingers crossed and hope that fusion will kick in). So, consider it more of a public service announcement – “hey, just in case you ever need them, index-related functions live here”.
## Usage
Unfortunately, `Data.List.Indexed` was taken by [IndexedList](http://hackage.haskell.org/package/IndexedList), which implements such exciting things as “counted lists” and “conic lists”. Nope, I'm not bitter at all. Okay, maybe a bit, even tho it's completely unfair to [IndexedList](http://hackage.haskell.org/package/IndexedList). Anyway:
~~~ haskell
import Data.List.Index
~~~
And you can use functions from `Data.List` by prepending `i` to them. There's also `indexed :: [a] -> [(Int,a)]` and a family of functions for modifying the element at an index (`deleteAt`, `setAt`, `modifyAt`, `updateAt`, `insertAt`).
Watch out – `ifoldl` has the index as the *second* parameter of the function:
~~~ haskell
ifoldl :: (b -> Int -> a -> b) -> b -> [a] -> b
~~~
That's the same convention that [containers](http://hackage.haskell.org/package/containers) and [vector](http://hackage.haskell.org/package/vector) use. Other functions pass the index as the first argument, as expected.