License | BSD-3-Clause |
---|---|
Maintainer | Preetham Gujjula <libraries@mail.preetham.io> |
Stability | experimental |
Safe Haskell | Safe-Inferred |
Language | GHC2021 |
Synopsis
- applyMerge :: Ord c => (a -> b -> c) -> [a] -> [b] -> [c]
- applyMergeBy :: (c -> c -> Ordering) -> (a -> b -> c) -> [a] -> [b] -> [c]
- applyMergeOn :: Ord d => (c -> d) -> (a -> b -> c) -> [a] -> [b] -> [c]
Documentation
applyMerge :: Ord c => (a -> b -> c) -> [a] -> [b] -> [c] Source #
If given a binary function f
that is non-decreasing in both arguments,
and two (potentially infinite) ordered lists xs
and ys
, then
is a sorted list of all applyMerge
f xs ysf x y
, for each x
in
xs
and y
in ys
.
Producing \(n\) elements of
takes \(O(n \log n)\)
time and \(O(\sqrt{n})\) auxiliary space, assuming that applyMerge
f xs ysf
and compare
take \(O(1)\) time.
For example, to generate the 3-smooth numbers (Wikipedia):
smooth3 :: [Integer] smooth3 = applyMerge (*) (iterate (*2) 1) (iterate (*3) 1)
For more examples, see README#examples.
applyMergeBy :: (c -> c -> Ordering) -> (a -> b -> c) -> [a] -> [b] -> [c] Source #
Like applyMerge
, but uses a custom comparison function.
applyMergeOn :: Ord d => (c -> d) -> (a -> b -> c) -> [a] -> [b] -> [c] Source #
Like applyMerge
, but applies a custom projection function before
performing comparisons.
For example, to compute the Gaussian integers, ordered by norm:
zs :: [Integer] zs = 0 : concatMap (\i -> [i, -i]) [1..] gaussianIntegers :: [GaussianInteger] -- `GaussianInteger` from arithmoi gaussianIntegers = applyMergeOn norm (:+) zs zs