NoRedInk/elm-compare/1.0.0

NOTE: this package is not compatible with Elm 0.19.1

Compare

Tools for composing comparison functions.

A comparison function is the type of function you can pass to List.sortWith. You pass it two elements of a type and it returns an Order (defined in the Basics module and imported by deault).

Comparator type

type alias Comparator a =
a -> a -> Order

A function that returns the relative ordering of two elements of a type.

Using compare functions

min : Comparator a -> a -> a -> a

Like Basics.min but using a custom comparator.

Compare.min (by String.length) "short" "longer"
--> "short"
max : Comparator a -> a -> a -> a

Like Basics.max but using a custom comparator.

Compare.max (by String.length) "short" "longer"
--> "longer"
minimum : Comparator a -> List a -> Maybe a

Like List.minimum but using a custom comparator.

minimum (by String.length) [ "longer", "short", "longest" ]
--> Just "short"
maximum : Comparator a -> List a -> Maybe a

Like List.maximum but using a custom comparator.

maximum (by String.length) [ "longer", "short", "longest" ]
--> Just "longest"

Composing compare functions

by : (a -> comparable) -> Comparator a

Take a function meant for List.sortBy and turn it into a Comparator.

List.sortWith (by String.length) [ "longer", "short", "longest" ]
--> [ "short", "longer", "longest" ]
concat : List (Comparator a) -> Comparator a

Compare by multiple criteria in order. The first criterium that distinguishes two elements is used.

kara : { firstName : String, lastName : String }
kara = { firstName = "Kara", lastName = "Thrace" }


william : { firstName : String, lastName : String }
william = { firstName = "William", lastName = "Adama" }


lee : { firstName : String, lastName : String }
lee = { firstName = "Lee", lastName = "Adama" }


List.sortWith (concat [ by .lastName, by .firstName ]) [ kara, william, lee  ]
--> [ lee, william, kara ]
reverse : Comparator a -> Comparator a

Reverse an ordering function.

List.sortWith (by String.length |> reverse) [ "longer", "short", "longest" ]
--> [ "longest", "longer", "short" ]