list-extras-0.4.1.3: Common not-so-common functions for lists

PortabilityHaskell98
Stabilitystable
Maintainerwren@community.haskell.org
Safe HaskellSafe-Inferred

Data.List.Extras.Pair

Contents

Description

This module provides safe zipping functions which will fail (return Nothing) on uneven length lists.

Synopsis

Safe functions for zipping lists

pairWithBy :: (a -> b -> c) -> (c -> d -> d) -> d -> [a] -> [b] -> Maybe dSource

A generic version of pair. The first argument is a tuple homomorphism (i.e. a function for how to combine values from the two lists), the second two arguments form a list homomorphism (i.e. so you can foldr the [c] list directly without actually constructing it).

In order to evaluate to WHNF pairWithBy is strict in both list arguments, as it must be, to determine that the lists are of the same length. This means it can survive one infinite list (yielding Nothing) but that it can't survive two. The implementation is very efficient and uses a tight tail-recursive loop, however with extremely long lists it will be churning through heap and that tightness can make it hard to interrupt (lists of 1 million elements return in 1~2 seconds, but lists of 10 million can lock your system up).

pairWith :: (a -> b -> c) -> [a] -> [b] -> Maybe [c]Source

A safe version of zipWith.

pairBy :: ((a, b) -> d -> d) -> d -> [a] -> [b] -> Maybe dSource

A safe version of zip that uses a user-defined list homomorphism.

pair :: [a] -> [b] -> Maybe [(a, b)]Source

A safe version of zip.

Special safe zipping functions

biject :: [a -> b] -> [a] -> Maybe [b]Source

A bijection from a list of functions and a list of arguments to a list of results of applying the functions bijectively.

biject' :: [a -> b] -> [a] -> Maybe [b]Source

A version of biject that applies functions strictly. N.B. the list is still lazily evaluated, this just makes the functions strict in their argument.

New (unsafe) zipping functions

zipWithBy :: (a -> b -> c) -> (c -> d -> d) -> d -> [a] -> [b] -> dSource

An unsafe variant of pairWithBy to fill out the interface.

zipBy :: ((a, b) -> d -> d) -> d -> [a] -> [b] -> dSource

A version of zip that uses a user-defined list homomorphism.