Generic swap: Difference between revisions

 
(5 intermediate revisions by 5 users not shown)
Line 863:
B = [1, 2, 3, 4, 5]
</pre>
 
=={{header|Binary Lambda Calculus}}==
From https://github.com/tromp/AIT/blob/master/rosetta/swap.lam we get the 25-bit swap function
<pre>0001100000000101101101110</pre>
 
=={{header|BQN}}==
Line 1,590 ⟶ 1,594:
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Generic_swap}}
 
'''Solution'''
 
Fōrmulæ supports assignments of several symbols (provided as a list) from an expression that reduces to a list of the same cardinality (the expression is first reduced before the actual assignment).
 
This can be used to do a generic swap as follows:
 
[[File:Fōrmulæ - Generic swap 01.png]]
 
'''Test case'''
 
[[File:Fōrmulæ - Generic swap 02.png]]
 
[[File:Fōrmulæ - Generic swap 03.png]]
 
=={{header|Gecho}}==
Line 2,052 ⟶ 2,070:
 
=={{header|langur}}==
Langur does not have an option for using references (so far). The following is not directly applicable to the task, but valuesValues can be swapped using multi-variable assignment, including indexed values (for mutable variables).
 
{{works with|langur|0.10}}
<syntaxhighlight lang="langur">var .abc = [1, 2, 3]
var .def = [5, 6, 7]
Line 2,062 ⟶ 2,079:
writeln .abc
writeln .def</syntaxhighlight>
 
Prior to 0.10, you would use parentheses as follows.
 
<syntaxhighlight lang="langur">(.abc[3], .def[3]) = (.def[3], .abc[3])</syntaxhighlight>
 
{{out}}
Line 2,471 ⟶ 2,484:
swap(locals, "x", "y")
print "AFTER: x=" + x + ", y=" + y</syntaxhighlight>
{{out}}
<pre>BEFORE: x=1, y=2
AFTER: x=2, y=1</pre>
Line 3,667 ⟶ 3,680:
 
Both approaches are illustrated below.
<syntaxhighlight lang="ecmascriptwren">var swap = Fn.new { |l| l.swap(0, 1) }
 
var a = 6
885

edits