length package:base-compat

Number of elements in NonEmpty list.
Returns the size/length of a finite structure as an Int. The default implementation just counts elements starting with the leftmost. Instances for structures that can compute the element count faster than via element-by-element counting, should provide a specialised implementation.

Examples

Basic usage:
>>> length []
0
>>> length ['a', 'b', 'c']
3

>>> length [1..]
* Hangs forever *
Use compareLength xs n as a safer and faster alternative to compare (length xs) n. Similarly, it's better to write compareLength xs 10 == LT instead of length xs < 10. While length would force and traverse the entire spine of xs (which could even diverge if xs is infinite), compareLength traverses at most n elements to determine its result.
>>> compareLength [] 0
EQ

>>> compareLength [] 1
LT

>>> compareLength ['a'] 1
EQ

>>> compareLength ['a', 'b'] 1
GT

>>> compareLength [0..] 100
GT

>>> compareLength undefined (-1)
GT

>>> compareLength ('a' : undefined) 0
GT
Use compareLength xs n as a safer and faster alternative to compare (length xs) n. Similarly, it's better to write compareLength xs 10 == LT instead of length xs < 10. While length would force and traverse the entire spine of xs (which could even diverge if xs is infinite), compareLength traverses at most n elements to determine its result.
>>> compareLength ('a' :| []) 1
EQ

>>> compareLength ('a' :| ['b']) 3
LT

>>> compareLength (0 :| [1..]) 100
GT

>>> compareLength undefined 0
GT

>>> compareLength ('a' :| 'b' : undefined) 1
GT