# Transpose Function

A TransposeFunction takes a collection of columns and returns a collection of rows. The first row consists of the first element from each column. Successive rows are constructed similarly.

In general, the formalism of transposition takes a function of two arguments, f(x,y) and replaces it with a function which takes the same arguments in reversed order. In formal mathematical terms one interprets a function of many arguments via currying as a function of one argument (the first) returning a (likewise interpreted) function of the rest; so our function of two arguments is

```   f = ( x -> ( y -> f(x,y) ) )

```
and its transpose is the function

```   ( y -> ( x -> f(x,y) ) )

```
Of course, in programming languages, we use various things in ways that formal mathematics can describe as functions; pretty much anything that takes an input (array index, hash key, function parameter list) and produces an output can be construed in this way.

Where it comes to collections of collections, we effectively (by iterating over the collections) treat the collections as lists (even if we call them sequences or arrays); so we have a list of lists. Mathematics happily construes a list as a function from (an initial subset of) the natural numbers; so our list of lists is a function returning functions and we have what we need to apply transposition.

Any time you have a list of lists (or kindred DataStructure) and find yourself having to pass down both the list of lists and a second-level key (i.e. f and y in the above) so that the recipient can supply first level key(s) as appropriate, passing down transpose(f)(y) instead will give a simple function (or list, as appropriate) that the recipient can use simply.

I HaveThisPattern, but I shouldn't have had to reinvent it. Anyone know of a good source for patterns of this sort? This is the kind of thing that eventually becomes obvious when you look at certain kinds of problems long enough, but it'd be nice to have a shopping list to choose from ahead of time, to borrow some other smart person's analysis of common forms and solutions. -- DougMerritt

See the TransfoldPattern which has an embedded TransposeFunction. It is described in the book FunctionalPatternSystemForObjectOrientedDesign and I am currently working on an implementation, described in ObjectFunctionalImplementation. -- JohnFletcher

EditText of this page (last edited March 14, 2007) or FindPage with title or text search