This module provides support for adaptive tuples.
AdaptiveTuple is a tuple type with the size chosen at run-time and
minimal overhead. All elements must be of the same type. Calculations
are generated by combining adaptive tuples, which are then given an
initial input with the
reifyTuple function or its strict variant.
Example: suppose you have a list of numbers that is either a single list or multiple interleaved lists. You wish to determine the maximum value of the single list or maximums of all interleaved lists.
-- |The second argument is a dummy argument to fix the type of c s () -- so this function can be used directly with reifyTuple deinterleave :: AdaptiveTuple c s => [Int] -> c s () -> [c s Int] deinterleave  _ =  deinterleave xs n = let (h, rest) = splitAt (tupLength n) xs in toATuple h : deinterleave n rest maxVals :: AdaptiveTuple c s => [c s Int] -> c s Int maxVals = foldl' (\a b -> max <$> a <*> b) (pure 0) runner :: Int -> [Int] -> [Int] runner n xs = reifyStrictTuple n (repeat ()) (fromATuple . maxVals . deinterleave xs)
using AdaptiveTuple is similar to the
ZipList applicative instance, except
without the overhead.
- class (Nat s, Applicative (c s)) => AdaptiveTuple c s where
- data AdaptiveTupleException
- reifyTuple :: forall el r. Int -> [el] -> (forall c s. (AdaptiveTuple c s, Nat s) => c s el -> r) -> r
- reifyStrictTuple :: forall el r. Int -> [el] -> (forall c s. (AdaptiveTuple c s, Nat s) => c s el -> r) -> r
- invert :: AdaptiveTuple c s => [c s a] -> c s [a]
- mapIndexed :: AdaptiveTuple c s => (Int -> a -> b) -> c s a -> c s b
Adaptive tuples: unboxed tuples of varying size.
s is a type-level indicator of the number of elements in the container.
run a computation using a lazy AdaptiveTuple
run a computation using a strict AdaptiveTuple
Lazily convert a list of AdaptiveTuples into an AdaptiveTuple of lists.