Decorate-sort-undecorate idiom: Difference between revisions

Content added Content deleted
m (→‎{{header|Raku}}: Add a Raku example)
Line 299: Line 299:
['Rosetta', 'Code', 'is', 'a', 'programming', 'chrestomathy', 'site'] => ['a', 'is', 'Code', 'site', 'Rosetta', 'programming', 'chrestomathy']
['Rosetta', 'Code', 'is', 'a', 'programming', 'chrestomathy', 'site'] => ['a', 'is', 'Code', 'site', 'Rosetta', 'programming', 'chrestomathy']
</pre>
</pre>

=={{header|Raku}}==
It is somewhat rare to do, or even need to do an explicit schwartzian transform in Raku. You can pass a transform function to the sort operator, and it will use it to do its comparisons. As long as the transform is arity one (only takes one value,) the sort will ''automatically'' perform a schwartzian transform transparently, behind the scenes.

Here the transform <code>.chars</code> is arity one, so a schwartzian transform is performed automatically by the compiler.
<syntaxhighlight lang="raku" line># automatic schwartzian transform
dd <Rosetta Code is a programming chrestomathy site>.sort: *.chars;

# explicit schwartzian transform
dd <Rosetta Code is a programming chrestomathy site>.map({$_=>.chars}).sort({.value}).map({.key});</syntaxhighlight>
{{out}}
( <code>dd</code> is the built in "data-dumper" function; a verbose and explicit representation of an objects contents.)
<pre>("a", "is", "Code", "site", "Rosetta", "programming", "chrestomathy").Seq
("a", "is", "Code", "site", "Rosetta", "programming", "chrestomathy").Seq</pre>

More complicated transforms may ''require'' an explicit schwartzian transform routine. An example of where an explicit transform is desirable is the <code>schwartzian()</code> routine in the [[P-value_correction#Raku|Raku entry for the P-value_correction task]].


=={{header|Wren}}==
=={{header|Wren}}==