first-and-last-0.1.0.1: First and Last generalized to return up to n values

Copyright(C) 2015 Mark Andrus Roberts
LicenseBSD-style (see the file LICENSE)
MaintainerMark Andrus Roberts <markandrusroberts@gmail.com>
Stabilityprovisional
Safe HaskellNone
LanguageHaskell2010

Data.Monoid.Last

Contents

Description

Last' n is a generalization of the Last exported by Data.Monoid: whereas Data.Monoid.Last returns up to one value, Last' n returns up to n values.

Data.Monoid.Last    a ≡
            Last' 1 a ≡
            Last    a

This library also provides an API-compatible type synonym Last and function getLast allowing you to use it as a drop-in replacement.

Synopsis

Last

type Last a = Last' 1 a Source

A type isomorphic to Data.Monoid.Last

getLast :: Last a -> Maybe a Source

Get the last value of type a, if any.

>>> getLast (foldMap pure [])
Nothing
>>> getLast (foldMap pure [1,2,3,4])
Just 4

Last'

data Last' n a Source

A generalized version of Data.Monoid.Last

getLast' :: Last' n a -> [a] Source

Get the last n values or fewer of type a.

>>> getLast' (foldMap pure [1,2,3,4] :: Last' 0 Int)
[]
>>> getLast' (foldMap pure [1,2,3,4] :: Last' 1 Int)
[4]
>>> getLast' (foldMap pure [1,2,3,4] :: Last' 2 Int)
[3,4]