Decorate-sort-undecorate idiom: Difference between revisions
add RPL
m (→{{header|Lua}}) |
(add RPL) |
||
Line 733:
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|RPL}}==
This implementation accepts the key function as a callback, but uses local named variables.
≪ → seq func
≪ { }
1 seq SIZE '''FOR''' j
seq j GET
DUP func EVAL
2 →LIST 1 →LIST +
'''NEXT'''
≫ ≫ '<span style="color:blue">DECOR</span>’ STO
≪ '''IF''' DUP SIZE 2 ≥ '''THEN'''
LIST→ → len
≪ len 1 FOR n
1 n 1 - '''START'''
DUP2 2 GET SWAP 2 GET
'''IF''' < '''THEN''' SWAP '''END'''
n ROLLD
'''NEXT''' n ROLLD
-1 '''STEP''' len →LIST
≫ '''END'''
≫ '<span style="color:blue">SKSORT</span>’ STO
≪ → seq
≪ { }
1 seq SIZE '''FOR''' j
seq j GET 1 GET +
'''NEXT'''
≫ ≫ '<span style="color:blue">UNDECOR</span>’ STO
{ "Rosetta" "Code" "is" "a" "programming" "chrestomathy" "site" } ≪ SIZE ≫ <span style="color:blue">DECOR KSORT UNDECOR</span>
{{out}}
<pre>
1: {"a" "is" "Code" "site" "Rosetta" "programming" "chrestomathy" }
</pre>
=={{header|Ruby}}==
Arrays have a <code>sort_by</code> method which does a Schwartzian transform.
|