Decorate-sort-undecorate idiom

From Rosetta Code
Revision as of 04:14, 28 July 2023 by Laurence (talk | contribs) (Created draft task)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Decorate-sort-undecorate idiom is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Introduction

Suppose you have to sort a list of strings based on a property of each, called "the key", i.e. their lenghts. The most popular solution would require the usage of a sorting algorithm with a custom comparator.

Now suppose that key computation is an expensive operation, for example, it might involve intensive reading of files, databases, or exchanging information over a network. In such a case, using a sorting algorithm with a custom comparator is not optimal, because the key is computed multiple times for each element in the array, once each time the element needs to be compared to another (and also requires the calculation of the key for that other).

The decorate-sort-undecorate idiom

One solution of the problem is called the "decorate-sort-undecorate" idiom, pattern or technique. It was originated and named by the Lisp community.

Suppose we want to sort the following list of words according to their lengths (the length was chosen for illustrative purposes, it is not precisely an "expensive" operation).

{"Rosetta", "Code", "is", "a", "programming", "chrestomathy", "site"}

According to the decorate-sort-decorate idiom, we have to "decorate" each element with its key, so each element of the original list becomes a pair. Notice that the key is calculated once (and only once) for each element in the list:

{{"Rosetta", 7}, {"Code", 4}, {"is", 2}, {"a", 1}, {"programming", 11}, {"chrestomathy", 12}, {"site", 4}}

The list is then sorted according to the second element of each pair (the key), perhaps using a custom comparator:

{{"a", 1}, {"is", 2}, {"site", 4}, {"Code", 4}, {"Rosetta", 7}, {"programming", 11}, {"chrestomathy", 12}}

And finally, the list must be undecorated, we have to remove the second element of each pair (the key):

{"a", "is", "site", "Code", "Rosetta", "programming", "chrestomathy"}

So, the decoration acts as a form of memoization.

The Schwartzian transform

Randal L. Schwartz wrote an implementation of the decorate-order-undecorate language in Perl in 1994, which gained much popularity in the Perl community. It was named "Schwartzian transform". Today the terms decorate-order-undecorate and the Schwartzian transform are used interchangeably, even outside the Perl community.

The Wikipedia page states that a solution can be called a "Schwartzian transform" only if it does not use named temporary lists or arrays. The Lisp solution and even the solution shown by Schwartz actually use intermediate lists, but these lists do not have explicit names, instead they use a functional composition of map-sort-map operations.

Task

Write in your programming language, a function, procedure, method, routine, etc. to sort a list of words by length (the key function), using the decorate-sort-undecorate idiom.

  • Bonus 1. If your solution can accept the key function as a callback.
  • Bonus 2. If your solution is also a "Schwartzian transform", this is, it does not use named temporary lists/arrays.

You can, at your choice, show two solutions, the first using intermediate named lists/arrays, and the second one not using them (a Schwartzian transform)(if applicable). The first solution is sometimes more valuable, because the use of named entities makes the code clearer.

References

Fōrmulæ

Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation —i.e. XML, JSON— they are intended for storage and transfer purposes more than visualization and edition.

Programs in Fōrmulæ are created/edited online in its website.

In this page you can see and run the program(s) related to this task and their results. You can also change either the programs or the parameters they are called with, for experimentation, but remember that these programs were created with the main purpose of showing a clear solution of the task, and they generally lack any kind of validation.

Solution

Let us create a list for testing:

Decorate-sort-undecorate: The following is a solution using a decorate-sort-undecorate idiom:

Test case

Schwartzian transform: The following is a solution using a "Schwartzian transform" idiom, and it produces identical results: