>>> take 1 (transpose ['a' : undefined, 'b' : undefined]) ["ab"]
>>> transpose [[1,2,3],[4,5,6]] [[1,4],[2,5],[3,6]]If some of the rows are shorter than the following rows, their elements are skipped:
>>> transpose [[10,11],[20],[],[30,31,32]] [[10,20,30],[11,31],[32]]For this reason the outer list must be finite; otherwise transpose hangs:
>>> transpose (repeat []) * Hangs forever *
[[00,01,02,...], [[00], [10,11,12,...], --> [10,01], [20,21,22,...], [20,11,02], ...] ...]With concat . shear you can perform a Cantor diagonalization, that is an enumeration of all elements of the sub-lists where each element is reachable within a finite number of steps. It is also useful for polynomial multiplication (convolution).
[[00,01,02,...], [[00], [10,11,12,...], --> [01,10], [20,21,22,...], [02,11,20], ...] ...]It's like shear but the order of elements in the sub list is reversed. Its implementation seems to be more efficient than that of shear. If the order does not matter, better choose shearTranspose.
\xs -> shearTranspose xs == map reverse (shear (xs::[String]))
>>> transpose [[1,2,3],[4,5,6]] [[1,4],[2,5],[3,6]]If some of the rows are shorter than the following rows, their elements are skipped:
>>> transpose [[10,11],[20],[],[30,31,32]] [[10,20,30],[11,31],[32]]For this reason the outer list must be finite; otherwise transpose hangs:
>>> transpose (repeat []) * Hangs forever *transpose is lazy:
>>> take 1 (transpose ['a' : undefined, 'b' : undefined]) ["ab"]
>>> transpose [[1,2,3],[4,5,6]] [[1,4],[2,5],[3,6]]If some of the rows are shorter than the following rows, their elements are skipped:
>>> transpose [[10,11],[20],[],[30,31,32]] [[10,20,30],[11,31],[32]]
>>> transpose [[1,2,3],[4,5,6]] [[1,4],[2,5],[3,6]]If some of the rows are shorter than the following rows, their elements are skipped:
>>> transpose [[10,11],[20],[],[30,31,32]] [[10,20,30],[11,31],[32]]For this reason the outer list must be finite; otherwise transpose hangs:
>>> transpose (repeat []) * Hangs forever *
delay [xs, ys, zs, ... ] = [[], xs, ys, zs, ...]
delay [[x,...], [y,...], ...] = [[], [x,...], [y,...], ...]Typically used when defining Listable instances:
instance Listable <Type> where tiers = ... \/ delay (cons<N> <Constructor>) \/ ...
reset [[], [], ..., xs, ys, zs, ...] = [xs, ys, zs, ...]
reset [[], xs, ys, zs, ...] = [xs, ys, zs, ...]
reset [[], [], ..., [x], [y], [z], ...] = [[x], [y], [z], ...]Typically used when defining Listable instances:
instance Listable <Type> where tiers = ... \/ reset (cons<N> <Constructor>) \/ ...Be careful: do not apply reset to recursive data structure constructors. In general this will make the list of size 0 infinite, breaking the tiers invariant (each tier must be finite).
normalizeT [xs0,xs1,...,xsN,[]] = [xs0,xs1,...,xsN] normalizeT [xs0,xs1,...,xsN,[],[]] = [xs0,xs1,...,xsN]The arbitrary limit of 12 tiers is necessary as this function would loop if there is an infinite trail of empty tiers.
>>> sequenceA [Just 1, Just 2, Just 3] Just [1,2,3]
>>> sequenceA [Right 1, Right 2, Right 3] Right [1,2,3]The next two example show Nothing and Just will short circuit the resulting structure if present in the input. For more context, check the Traversable instances for Either and Maybe.
>>> sequenceA [Just 1, Just 2, Just 3, Nothing] Nothing
>>> sequenceA [Right 1, Right 2, Right 3, Left 4] Left 4