Decorate-sort-undecorate idiom: Difference between revisions

Content added Content deleted
m (→‎{{header|Raku}}: change explicit ST to use an arity two comparitor to disable automatic ST. same output)
(Added FreeBasic)
Line 149: Line 149:
)
)


END</syntaxhighlight>
END
</syntaxhighlight>
{{out}}
{{out}}
<pre>[ "a", "is", "Code", "site", "Rosetta", "programming", "chrestomathy" ]</pre>
<pre>
[ "a", "is", "Code", "site", "Rosetta", "programming", "chrestomathy" ]
</pre>


=={{header|Fōrmulæ}}==
=={{header|FreeBASIC}}==
FreeBASIC doesn't normally print string lists in "quoted" form though I've added the quotes here to be consistent with the other solutions.
<syntaxhighlight lang="vb">Type map
a As String
b As Integer
End Type


Sub Sort(array() As map)
Dim As Integer i, j, min
Dim As Integer lb = Lbound(array), ub = Ubound(array)
For i = lb To ub - 1
min = i
For j = i + 1 To ub
If array(1,j).b <= array(1,min).b Then min = j
Next j
Swap array(min,1).a, array(i,1).a
Swap array(1,min).b, array(1,i).b
Next i
End Sub

Sub Schwartzian(a() As String)
Dim As Integer p, lb = Lbound(a), ub = Ubound(a)
Dim As map e(lb To ub, lb To ub)
' Decorate
For p = lb To ub
e(p,1).a = a(p)
e(1,p).b = Len(a(p))
Next
' Sort
Sort(e())
' Undecorate
Print "[";
For p = lb To ub
Print !"\"" & e(p,1).a & !"\", ";
Next
Print Chr(8) & Chr(8) & "]"
End Sub

Dim As String words(6) = {"Rosetta", "Code", "is", "a", "programming", "chrestomathy", "site"}
Schwartzian(words())

Sleep</syntaxhighlight>
{{out}}
<pre>["a", "is", "site", "Code", "Rosetta", "programming", "chrestomathy"]</pre>

=={{header|Fōrmulæ}}==
{{FormulaeEntry|page=https://formulae.org/?script=examples/Decorate-sort-undecorate_idiom}}
{{FormulaeEntry|page=https://formulae.org/?script=examples/Decorate-sort-undecorate_idiom}}


Line 199: Line 243:
];
];


console.log(schwartzian(example, (e) => e.length));
console.log(schwartzian(example, (e) => e.length));</syntaxhighlight>
</syntaxhighlight>

{{out}}
{{out}}
<pre>[ 'a', 'is', 'Code', 'site', 'Rosetta', 'programming', 'chrestomathy' ]</pre>
<pre>[ 'a', 'is', 'Code', 'site', 'Rosetta', 'programming', 'chrestomathy' ]</pre>
Line 235: Line 277:
console.log(schwartzian(example, (e) => Array.from(e).reverse().join("")));
console.log(schwartzian(example, (e) => Array.from(e).reverse().join("")));
</syntaxhighlight>
</syntaxhighlight>

{{out}}
{{out}}
<pre>[ 'a', 'is', 'Code', 'site', 'Rosetta', 'programming', 'chrestomathy' ]
<pre>
[ 'a', 'is', 'Code', 'site', 'Rosetta', 'programming', 'chrestomathy' ]
[ 'a', 'Rosetta', 'Code', 'site', 'programming', 'is', 'chrestomathy' ]</pre>
[ 'a', 'Rosetta', 'Code', 'site', 'programming', 'is', 'chrestomathy' ]
</pre>


=={{header|jq}}==
=={{header|jq}}==
Line 263: Line 302:
# Illustration
# Illustration
["Rosetta", "Code", "is", "a", "programming", "chrestomathy", "site"]
["Rosetta", "Code", "is", "a", "programming", "chrestomathy", "site"]
| sort_by(length), sort_by_decorator(length)
| sort_by(length), sort_by_decorator(length)</syntaxhighlight>
</syntaxhighlight>
{{output}}
{{output}}
<pre>["a","is","Code","site","Rosetta","programming","chrestomathy"]
<pre>
["a","is","Code","site","Rosetta","programming","chrestomathy"]
["a","is","Code","site","Rosetta","programming","chrestomathy"]</pre>
["a","is","Code","site","Rosetta","programming","chrestomathy"]
</pre>


=={{header|Julia}}==
=={{header|Julia}}==
Line 294: Line 330:
TEST = ["Rosetta", "Code", "is", "a", "programming", "chrestomathy", "site"]
TEST = ["Rosetta", "Code", "is", "a", "programming", "chrestomathy", "site"]


print(TEST, "=>", schwartzian(TEST, len))
print(TEST, "=>", schwartzian(TEST, len))</syntaxhighlight>
</syntaxhighlight>{{out}}
{{out}}
<pre>['Rosetta', 'Code', 'is', 'a', 'programming', 'chrestomathy', 'site'] => ['a', 'is', 'Code', 'site', 'Rosetta', 'programming', 'chrestomathy']</pre>
<pre>
['Rosetta', 'Code', 'is', 'a', 'programming', 'chrestomathy', 'site'] => ['a', 'is', 'Code', 'site', 'Rosetta', 'programming', 'chrestomathy']
</pre>


=={{header|Raku}}==
=={{header|Raku}}==
Line 332: Line 366:
var length = Fn.new { |s| s.count }
var length = Fn.new { |s| s.count }
schwartzian.call(words, length)</syntaxhighlight>
schwartzian.call(words, length)</syntaxhighlight>

{{out}}
{{out}}
<pre>["a", "is", "site", "Code", "Rosetta", "programming", "chrestomathy"]</pre>
<pre>
["a", "is", "site", "Code", "Rosetta", "programming", "chrestomathy"]
</pre>