Decorate-sort-undecorate idiom: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (→{{header|Raku}}: change explicit ST to use an arity two comparitor to disable automatic ST. same output) |
(Added FreeBasic) |
||
Line 149:
)
END</syntaxhighlight>▼
▲</syntaxhighlight>
{{out}}
<pre>[ "a", "is", "Code", "site", "Rosetta", "programming", "chrestomathy" ]</pre>▼
▲[ "a", "is", "Code", "site", "Rosetta", "programming", "chrestomathy" ]
=={{header|
FreeBASIC doesn't normally print string lists in "quoted" form though I've added the quotes here to be consistent with the other solutions.
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}}
Line 199 ⟶ 243:
];
console.log(schwartzian(example, (e) => e.length));</syntaxhighlight>
▲</syntaxhighlight>
{{out}}
<pre>[ 'a', 'is', 'Code', 'site', 'Rosetta', 'programming', 'chrestomathy' ]</pre>
Line 235 ⟶ 277:
console.log(schwartzian(example, (e) => Array.from(e).reverse().join("")));
</syntaxhighlight>
{{out}}
[ 'a', '
=={{header|jq}}==
Line 263 ⟶ 302:
# Illustration
["Rosetta", "Code", "is", "a", "programming", "chrestomathy", "site"]
| sort_by(length), sort_by_decorator(length)</syntaxhighlight>
▲</syntaxhighlight>
{{output}}
▲<pre>[
["a","is","Code","site","Rosetta","programming","chrestomathy"]</pre>
▲["a","is","Code","site","Rosetta","programming","chrestomathy"]
=={{header|Julia}}==
Line 294 ⟶ 330:
TEST = ["Rosetta", "Code", "is", "a", "programming", "chrestomathy", "site"]
print(TEST, "=>", schwartzian(TEST, len))</syntaxhighlight>
<pre>['Rosetta', 'Code', 'is', 'a', 'programming', 'chrestomathy', 'site'] => ['a', 'is', 'Code', 'site', 'Rosetta', 'programming', 'chrestomathy']</pre>▼
▲['Rosetta', 'Code', 'is', 'a', 'programming', 'chrestomathy', 'site'] => ['a', 'is', 'Code', 'site', 'Rosetta', 'programming', 'chrestomathy']
=={{header|Raku}}==
Line 332 ⟶ 366:
var length = Fn.new { |s| s.count }
schwartzian.call(words, length)</syntaxhighlight>
{{out}}
<pre>["a", "is", "site", "Code", "Rosetta", "programming", "chrestomathy"]</pre>
▲["a", "is", "site", "Code", "Rosetta", "programming", "chrestomathy"]
|