adaptive-tuple-0.1.0: Self-optimizing tuple types




This module provides support for adaptive tuples. An 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 whereSource

Adaptive tuples: unboxed tuples of varying size. s is a type-level indicator of the number of elements in the container.


getIndex :: c s el -> Int -> elSource

setIndex :: Int -> el -> c s el -> c s elSource

mapIndex :: (el -> el) -> Int -> c s el -> c s elSource

toATuple :: [el] -> c s elSource

fromATuple :: c s el -> [el]Source

tupLength :: c s el -> IntSource



reifyTuple :: forall el r. Int -> [el] -> (forall c s. (AdaptiveTuple c s, Nat s) => c s el -> r) -> rSource

run a computation using a lazy AdaptiveTuple

reifyStrictTuple :: forall el r. Int -> [el] -> (forall c s. (AdaptiveTuple c s, Nat s) => c s el -> r) -> rSource

run a computation using a strict AdaptiveTuple

invert :: AdaptiveTuple c s => [c s a] -> c s [a]Source

Lazily convert a list of AdaptiveTuples into an AdaptiveTuple of lists.

mapIndexed :: AdaptiveTuple c s => (Int -> a -> b) -> c s a -> c s bSource

Map a 0-indexed function over an AdaptiveTuple