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.First

Contents

Description

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

Data.Monoid.First    a ≡
            First' 1 a ≡
            First    a

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

Synopsis

First

type First a = First' 1 a Source

A type isomorphic to Data.Monoid.First

getFirst :: First a -> Maybe a Source

Get the first value of type a, if any.

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

First'

data First' n a Source

A generalized version of Data.Monoid.First

getFirst' :: First' n a -> [a] Source

Get the first n values or fewer of type a.

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