slist: Sized list

[ data-structures, library, list, mpl ] [ Propose Tags ] [ Report a vulnerability ]

This package implements Slist data structure that stores the size of the list along with the list itself.


[Skip to Readme]

Downloads

Note: This package has metadata revisions in the cabal description newer than included in the tarball. To unpack the package including the revisions, use 'cabal get'.

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 0.0.0, 0.1.0.0, 0.1.1.0, 0.2.0.0, 0.2.0.1, 0.2.1.0
Change log CHANGELOG.md
Dependencies base (>=4.10.1.0 && <5), containers (>=0.5 && <1) [details]
Tested with ghc ==8.2.2, ghc ==8.4.4, ghc ==8.6.5, ghc ==8.8.4, ghc ==8.10.7, ghc ==9.0.2, ghc ==9.2.4, ghc ==9.4.2
License MPL-2.0
Copyright 2019-2020 Veronika Romashkina 2020-2022 Kowainik
Author Veronika Romashkina
Maintainer Kowainik <xrom.xkov@gmail.com>
Revised Revision 1 made by AndreasAbel at 2023-10-14T23:18:00Z
Category Data Structures, List
Home page https://github.com/kowainik/slist
Bug tracker https://github.com/kowainik/slist/issues
Source repo head: git clone https://github.com/kowainik/slist.git
Uploaded by vrom911 at 2022-11-03T11:22:30Z
Distributions Arch:0.2.1.0, LTSHaskell:0.2.1.0, NixOS:0.2.1.0, Stackage:0.2.1.0
Reverse Dependencies 1 direct, 2 indirect [details]
Downloads 4535 total (126 in the last 30 days)
Rating 2.25 (votes: 2) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for slist-0.2.1.0

[back to package description]

slist

GitHub CI Hackage MPL-2.0 license

⚠️ Caution: this is a very opinionated library. There is no intention to replace the standard list data type. We are aware of every design decision we made for this package, and we are taking responsibility for that design. If you find it inappropriate, please, consider to use another library instead, that would fulfil your requirements.

This package introduces sized list data type — Slist. The data type has the following shape:

data Slist a = Slist
    { sList :: [a]
    , sSize :: Size
    }

As you can see that along with the familiar list, it contains Size field that represents the size of the structure. Slists can be finite or infinite, and this is expressed with Size.

data Size
    = Size Int
    | Infinity

⚠️ Caution: Int is used for the size by design. We had to make some trade-offs to provide the better (as we think) interface for users. For more details on the choice, see the Haddock documentation for the Size data type.

This representation of the list gives some additional advantages. Getting the length of the list is the "free" operation (runs in O(1)). This property helps to improve the performance for a bunch of functions like take, drop, at, etc. But also it doesn't actually add any overhead on the existing functions.

Also, this allows to write a number of safe functions like safeReverse, safeHead, safeLast, safeIsSuffixOf, etc.

Comparison

Check out the comparison table between lists and slists performance.

Function list (finite) list (infinite) Slist (finite) Slist (infinite)
length O(n) <hangs> O(1) O(1)
safeLast O(n) <hangs> O(n) O(1)
init O(n) <works infinitely> O(n) O(1)
take O(min i n) O(i) 0 < i < n: O(i); otherwise: O(1) O(i)
at O(min i n) (run-time exception) O(i) (run-time exception) 0 < i < n: O(i); otherwise: O(1) O(i)
safeStripPrefix O(m) O(m) (can hang) O(m) O(m)

Potential usage cases

  • When you ask the size of the list too frequently.
  • When you need to convert to data structures that require to know the list size in advance for allocating an array of the elements. Example: Vector data structure.
  • When you need to serialise lists.
  • When you need to control the behaviour depending on the finiteness of the list.
  • When you need a more efficient or safe implementation of some functions.