Kurren123 / non-empty-list-alias

Functions for NonEmptyList you already have and Zipper implementation that goes with it.

Home Page:https://package.elm-lang.org/packages/turboMaCk/non-empty-list-alias/latest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

List.NonEmpty alias with Zipper

Build Status

Functions for NonEmpty list you already have and Zipper implementation that goes with it.

Implementation of non-empty list defined as alias to pair (a, List a). This means that producing or consuming type compatible with this library doesn't require having it as a dependecy. Despite this, this package aims to provide the most feature complete implementation of non-empty list for Elm and includes variety of practical functions from JSON decoder helpers to hi-level combinators like duplicate and extend.

Motivation

elm/core doesn't come with non empty list type. This means one usually has to rely library implementations of NonEmpty type:

These implementations usually define custom data type like NonEmpty a = Cons a (List a) and expose the constructor to make pattern matching possible. This makes it hard for library authors to provide support for NonEmpty, because they would need to pick one of these libraries and use it as a dependency of their own implementation and essentially impose this decision onto their users.

This implementation uses different approach. NonEmpty is an alias for the pair type alias NonEmpty a = (a, List a). Relying on anonymous data-type like tuple means:

  1. Libraries can produce NonEmpty data without depending on specific (including this) implementation.
  2. Implementation provided by this package can be easily replaced by different implementation using the same type definition.
  3. Users may choose to work with tuple directly without need to transform from and to NonEmpty type.

Zipper

One of the downsides of common approache is also noticable with available list zipper implementations.

All of above are usually constructed using List a -> Maybe (Zipper a) without posibility to construct zipper as NonEmpty a -> Zipper a. The motivation behind including Zipper in this package is to encourage its usage together with NonEmpty list. My favorite implementation of list zipper which doesn't rely on NonEmpty is zwilias/elm-holey-zipper.

Drawbacks

Compared to other implementations this implementation has less descriptive constructor in value space. This means that pattern matching happens on the pair instead of explicit constructor like Cons or NonEmpty.

conventional library:

matchNonEmpty : NonEmptyList a -> foo
matchNonEmpty (Cons h t) =
    .....

With this library:

matchNonEmpty : NonEmptyList a -> foo
matchNonEmpty (h, t) =
    .....

For Haskell Fanbois

List.NoneEmpty.NonEmpty is:

  • Functor
  • Foldable
  • Applicative
  • Monad
  • Comonad

List.NonEmpty.Zipper is:

  • Functor
  • Foldable
  • Applicative
  • Comonad

About

Functions for NonEmptyList you already have and Zipper implementation that goes with it.

https://package.elm-lang.org/packages/turboMaCk/non-empty-list-alias/latest/

License:BSD 3-Clause "New" or "Revised" License


Languages

Language:Elm 99.7%Language:Makefile 0.3%