hrldcpr / elm-cons

A non-empty list data structure for Elm.

Home Page:http://package.elm-lang.org/packages/hrldcpr/elm-cons/latest/Cons

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cons

This library provides a type for non-empty lists, called Cons.

Being able to encode non-emptiness in the type system can lead to simpler, clearer code.

For example, to find the largest element in a List, you have to account for the empty list, which complicates things:

maximum : List comparable -> Maybe comparable
maximum l =
  case l of
    [] -> Nothing
    first::rest -> Just <| List.foldl max first rest

Using Cons, on the other hand, the type system knows the list will never be empty, leading to much simpler code:

maximum : Cons comparable -> comparable
maximum = foldl1 max

See the full documentation for more.

About

A non-empty list data structure for Elm.

http://package.elm-lang.org/packages/hrldcpr/elm-cons/latest/Cons

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


Languages

Language:Elm 100.0%