hgeometry-combinatorial
Copyright(C) Frank Staals
Licensesee the LICENSE file
MaintainerFrank Staals
Safe HaskellNone
LanguageGHC2021

HGeometry.List.Util

Description

 
Synopsis

Documentation

leaveOutOne :: [a] -> [(a, [a])] Source #

Given an input list, computes all lists in which just one element is missing.

>>> mapM_ print $ leaveOutOne [1..5]
(1,[2,3,4,5])
(2,[1,3,4,5])
(3,[1,2,4,5])
(4,[1,2,3,5])
(5,[1,2,3,4])
>>> leaveOutOne []
[]
>>> leaveOutOne [1]
[(1,[])]

Improved functions for minima and maxima

minimumMaybe :: Ord a => [a] -> Maybe a Source #

Safe variant of Prelude.minimum.

>>> minimumMaybe [] :: Maybe ()
Nothing
>>> minimumMaybe [1,2,3]
Just 1

maximumMaybe :: Ord a => [a] -> Maybe a Source #

Safe variant of Prelude.maximum.

>>> maximumMaybe [] :: Maybe ()
Nothing
>>> maximumMaybe [1,2,3]
Just 3

minimumMaybeBy :: (a -> a -> Ordering) -> [a] -> Maybe a Source #

Total variant of Data.List.minimumBy.

>>> minimumMaybeBy (comparing abs) [] :: Maybe Int
Nothing
>>> minimumMaybeBy (comparing abs) [1,-2,3]
Just 1

minimaOn :: Ord b => (a -> b) -> [a] -> [a] Source #

Computes all minima by comparing some property.

>>> minimaOn (max 2) [1,2,3,4,5,-1]
[-1,2,1]

minimaBy :: (a -> a -> Ordering) -> [a] -> [a] Source #

Computes all minima.

>>> minimaBy (comparing abs) [1,2,3,2,1,-1]
[-1,1,1]

extractMinimaBy :: (a -> a -> Ordering) -> [a] -> [a] :+ [a] Source #

Extracts all minima from the list. The result consists of the list of minima, and all remaining points. Both lists are returned in the order in which they occur in the input.

>>> extractMinimaBy compare [1,2,3,0,1,2,3,0,1,2,0,2]
[0,0,0] :+ [2,3,1,2,3,1,2,1,2]

Partitioning and Grouping

partition3 :: Foldable f => (a -> Ordering) -> f a -> ([a], [a], [a]) Source #

Given a function f, partitions the list into three lists (lts,eqs,gts) such that:

  • f x == LT for all x in lts
  • f x == EQ for all x in eqs
  • f x == gt for all x in gts
>>> partition3 (compare 4) [0,1,2,2,3,4,5,5,6,6,7,7,7,7,7,8]
([5,5,6,6,7,7,7,7,7,8],[4],[0,1,2,2,3])

groupBy' :: (a -> a -> Ordering) -> [a] -> [NonEmpty a] Source #

A version of groupBy that uses the given Ordering to group consecutive Equal items

>>> groupBy' compare [0,1,2,2,3,4,5,5,6,6,7,7,7,7,7,8]
[0 :| [],1 :| [],2 :| [2],3 :| [],4 :| [],5 :| [5],6 :| [6],7 :| [7,7,7,7],8 :| []]