last package:relude

O(n). Extracts the last element of a NonEmpty list. Actual type of this function is the following:
last :: NonEmpty a -> a
but it was given a more complex type to provide friendlier compile time errors.
>>> last ('a' :| "bcde")
'e'

>>> last [0..5 :: Int]
...
... 'last' works with 'NonEmpty', not ordinary lists.
Possible fix:
Replace: [Int]
With:    NonEmpty Int
...
However, you can use 'last' with the ordinary lists.
Apply 'viaNonEmpty' function from relude:
viaNonEmpty last (yourList)
Note, that this will return 'Maybe Int'
therefore it is a safe function unlike 'last' from the standard Prelude
...

>>> last (Just 'a')
...
... 'last' works with 'NonEmpty Char' lists
But given: Maybe Char
...
Extract the last element of a list, which must be finite and non-empty. WARNING: This function is partial. Consider using unsnoc instead.

Examples

>>> last [1, 2, 3]
3
>>> last [1..]
* Hangs forever *
>>> last []
*** Exception: Prelude.last: empty list
Maybe monoid returning the rightmost non-Nothing value. Last a is isomorphic to Dual (First a), and thus to Dual (Alt Maybe a) Data.Semigroup.Last. The former returns the last non-Nothing, so x <> Data.Monoid.Last Nothing = x. The latter simply returns the last value, thus x <> Data.Semigroup.Last Nothing = Data.Semigroup.Last Nothing.

Examples

>>> Last (Just "hello") <> Last Nothing <> Last (Just "world")
Last {getLast = Just "world"}
>>> Last Nothing <> mempty
Last {getLast = Nothing}
The last element of a non-empty data structure.
>>> last1 (1 :| [2, 3, 4])
4