Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,3 @@
---
from: http://rosettacode.org/wiki/Generic_swap
note: Basic language learning

View file

@ -0,0 +1,19 @@
;Task:
Write a generic swap function or operator which exchanges the values of two variables (or, more generally, any two storage places that can be assigned), regardless of their types.
If your solution language is statically typed please describe the way your language provides genericity.
If variables are typed in the given language, it is permissible that the two variables be constrained to having a mutually compatible type, such that each is permitted to hold the value previously stored in the other without a type violation.
That is to say, solutions do not have to be capable of exchanging, say, a string and integer value, if the underlying storage locations are not attributed with types that permit such an exchange.
Generic swap is a task which brings together a few separate issues in programming language semantics.
Dynamically typed languages deal with values in a generic way quite readily, but do not necessarily make it easy to write a function to destructively swap two variables, because this requires indirection upon storage places or upon the syntax designating storage places.
Functional languages, whether static or dynamic, do not necessarily allow a destructive operation such as swapping two variables regardless of their generic capabilities.
Some static languages have difficulties with generic programming due to a lack of support for ([[Parametric Polymorphism]]).
Do your best!
<br><br>

View file

@ -0,0 +1,2 @@
F swap(&a, &b)
(a, b) = (b, a)

View file

@ -0,0 +1,20 @@
SWAP CSECT , control section start
BAKR 14,0 stack caller's registers
LR 12,15 entry point address to reg.12
USING SWAP,12 use as base
MVC A,=C'5678____' init field A
MVC B,=C'____1234' init field B
LA 2,L address of length field in reg.2
WTO TEXT=(2) Write To Operator, results in:
* +5678________1234
XC A,B XOR A,B
XC B,A XOR B,A
XC A,B XOR A,B. A holds B, B holds A
WTO TEXT=(2) Write To Operator, results in:
* +____12345678____
PR , return to caller
LTORG , literals displacement
L DC H'16' halfword containg decimal 16
A DS CL8 field A, 8 bytes
B DS CL8 field B, 8 bytes
END SWAP program end

View file

@ -0,0 +1,11 @@
;swap X with Y
pha ;push accumulator
txa
pha ;push x
tya
pha ;push y
pla
tax ;pop y into x
pla
tay ;pop x into y
pla ;pop accumulator

View file

@ -0,0 +1,5 @@
sta temp ;a label representing a zero page memory address
txa
pha ;push x
ldx temp
pla ;pop x into accumulator

View file

@ -0,0 +1,12 @@
temp equ $00
temp2 equ $01 ;these values don't matter.
lda temp
pha ;push temp
lda temp2
pha ;push temp2
pla
sta temp ;pop temp2 into temp
pla
sta temp2 ;pop temp into temp2

View file

@ -0,0 +1,11 @@
lda #$33
ldx #$44
ldy #$55
pha
phx
phy
pla ;a=#$55
ply ;y=#$44
plx ;x=#$33

View file

@ -0,0 +1,2 @@
EXG D0,D1 ;swap the contents of D0 and D1
EXG A0,A1 ;swap the contents of A0 and A1

View file

@ -0,0 +1,4 @@
MOVE.L ($00FF0000),D0
MOVE.L ($00FF1000),D1
MOVE.L D0,($00FF1000)
MOVE.L D1,($00FF0000)

View file

@ -0,0 +1,2 @@
MOVE.L #$1234ABCD,D0
SWAP D0 ;now D0 = #$ABCD1234

View file

@ -0,0 +1,22 @@
xchg ax,bx ;exchanges ax with bx
xchg ah,al ;swap the high and low bytes of ax
;XCHG a register with memory
mov dx,0FFFFh
mov word ptr [ds:userRam],dx
mov si,offset userRam
mov ax,1234h
xchg ax,[si] ;exchange ax with the value stored at userRam. Now, ax = 0FFFFh and the value stored at userRam = 1234h
;XCHG a register with a value on the stack.
mov ax,1234h
mov bx,4567h
push bx
push bp
mov bp,sp ;using [sp] as an operand for XCHG will not work. You need to use bp instead.
xchg ax,[2+bp] ;exchanges AX with the value that was pushed from BX onto the stack. Now, AX = 4567h,
;and the entry on the stack just underneath the top of the stack is 1234h.

View file

@ -0,0 +1 @@
swap

View file

@ -0,0 +1 @@
dup @ -rot !

View file

@ -0,0 +1,6 @@
(defun swap (pair)
(cons (cdr pair)
(car pair)))
(let ((p (cons 1 2)))
(cw "Before: ~x0~%After: ~x1~%" p (swap p)))

View file

@ -0,0 +1,13 @@
MODE GENMODE = STRING;
GENMODE v1:="Francis Gary Powers", v2:="Vilyam Fisher";
PRIO =:= = 1;
OP =:= = (REF GENMODE v1, v2)VOID: (
GENMODE tmp:=v1; v1:=v2; v2:=tmp
);
v1 =:= v2;
print(("v1: ",v1, ", v2: ", v2, new line))

View file

@ -0,0 +1,25 @@
# syntax: GAWK -f GENERIC_SWAP.AWK
BEGIN {
printf("%s version %s\n",ARGV[0],PROCINFO["version"])
foo = 1
bar = "a"
printf("\n%s %s\n",foo,bar)
rc = swap("foo","bar") # ok
printf("%s %s %s\n",foo,bar,rc?"ok":"ng")
printf("\n%s %s\n",foo,bar)
rc = swap("FOO","BAR") # ng
printf("%s %s %s\n",foo,bar,rc?"ok":"ng")
exit(0)
}
function swap(a1,a2, tmp) { # strings or numbers only; no arrays
if (a1 in SYMTAB && a2 in SYMTAB) {
if (isarray(SYMTAB[a1]) || isarray(SYMTAB[a2])) {
return(0)
}
tmp = SYMTAB[a1]
SYMTAB[a1] = SYMTAB[a2]
SYMTAB[a2] = tmp
return(1)
}
return(0)
}

View file

@ -0,0 +1,42 @@
INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit
PROC Swap(BYTE POINTER ptr1,ptr2 INT size)
BYTE tmp
INT i
FOR i=0 TO size-1
DO
tmp=ptr1^ ptr1^=ptr2^ ptr2^=tmp
ptr1==+1 ptr2==+1
OD
RETURN
PROC Main()
BYTE b1=[13],b2=[56]
INT i1=[65234],i2=[534]
REAL r1,r2
CHAR ARRAY s1="abcde",s2="XYZ"
Put(125) PutE() ;clear the screen
ValR("32.5",r1) ValR("-0.63",r2)
Print("Swap bytes: ")
PrintB(b1) Put(32) PrintB(b2) Print(" -> ")
Swap(@b1,@b2,1)
PrintB(b1) Put(32) PrintBE(b2)
Print("Swap integers: ")
PrintI(i1) Put(32) PrintI(i2) Print(" -> ")
Swap(@i1,@i2,2)
PrintI(i1) Put(32) PrintIE(i2)
Print("Swap floats: ")
PrintR(r1) Put(32) PrintR(r2) Print(" -> ")
Swap(r1,r2,6)
PrintR(r1) Put(32) PrintRE(r2)
Print("Swap strings: ")
Print(s1) Put(32) Print(s2) Print(" -> ")
Swap(@s1,@s2,2)
Print(s1) Put(32) PrintE(s2)
RETURN

View file

@ -0,0 +1,10 @@
generic
type Swap_Type is private; -- Generic parameter
procedure Generic_Swap (Left, Right : in out Swap_Type);
procedure Generic_Swap (Left, Right : in out Swap_Type) is
Temp : constant Swap_Type := Left;
begin
Left := Right;
Right := Temp;
end Generic_Swap;

View file

@ -0,0 +1,7 @@
with Generic_Swap;
...
type T is ...
procedure T_Swap is new Generic_Swap (Swap_Type => T);
A, B : T;
...
T_Swap (A, B);

View file

@ -0,0 +1,12 @@
void
__swap(&, &,,)
{
set(0, $3);
set(1, $2);
}
void
swap(&, &)
{
xcall(xcall, __swap);
}

View file

@ -0,0 +1,12 @@
#include <flow.h>
DEF-MAIN(argv,argc)
DIM(10) AS-INT-RAND( 10, random array )
SET( single var, 0.5768 )
PRNL( "SINGLE VAR: ", single var, "\nRANDOM ARRAY: ", random array )
single var <-> random array
PRNL( "SINGLE VAR: ", single var, "\nRANDOM ARRAY: ", random array )
END

View file

@ -0,0 +1,16 @@
PROC swap(a,b) IS b,a
PROC main()
DEF v1, v2, x
v1 := 10
v2 := 20
v1, v2 := swap(v1,v2)
WriteF('\d \d\n', v1,v2) -> 20 10
v1 := [ 10, 20, 30, 40 ]
v2 := [ 50, 60, 70, 80 ]
v1, v2 := swap(v1,v2)
ForAll({x}, v1, `WriteF('\d ',x)) -> 50 60 70 80
WriteF('\n')
ForAll({x}, v2, `WriteF('\d ',x)) -> 10 20 30 40
WriteF('\n')
ENDPROC

View file

@ -0,0 +1 @@
set {x,y} to {y,x}

View file

@ -0,0 +1 @@
A=43:B=47:H=A:A=B:B=H:?" A="A" B="B;

View file

@ -0,0 +1,10 @@
(mac myswap (a b)
(w/uniq gx
`(let ,gx a
(= a b)
(= b ,gx))))
(with (a 1
b 2)
(myswap a b)
(prn "a:" a #\Newline "b:" b))

View file

@ -0,0 +1,8 @@
swap: function [a,b]-> @[b,a]
c: 1
d: 2
print [c d]
[c,d]: swap c d
print [c d]

View file

@ -0,0 +1,12 @@
swap: function [a,b].exportable [
tmp: var b
let b var a
let a tmp
]
c: 1
d: 2
print [c d]
swap 'c 'd
print [c d]

View file

@ -0,0 +1,6 @@
Swap(ByRef Left, ByRef Right)
{
temp := Left
Left := Right
Right := temp
}

View file

@ -0,0 +1 @@
Exch(°A,°B,2)

View file

@ -0,0 +1,12 @@
global a, b
a = "one"
b = "two"
print a, b
call swap(a, b)
print a, b
end
subroutine swap(a, b)
temp = a : a = b : b = temp
end subroutine

View file

@ -0,0 +1,7 @@
a = 1.23 : b = 4.56
SWAP a,b
PRINT a,b
a$ = "Hello " : b$ = "world!"
SWAP a$,b$
PRINT a$,b$

View file

@ -0,0 +1,15 @@
a = 1.23 : b = 4.56
PROCswap(^a,^b, 5)
PRINT a,b
a$ = "Hello " : b$ = "world!"
PROCswap(^a$,^b$, 6)
PRINT a$,b$
END
DEF PROCswap(a%, b%, s%)
LOCAL i%
FOR i% = 0 TO s%-1
SWAP a%?i%,b%?i%
NEXT
ENDPROC

View file

@ -0,0 +1 @@
ab

View file

@ -0,0 +1,5 @@
x = 1
y$ = "hello"
SWAP x, y$
PRINT y$

View file

@ -0,0 +1,17 @@
@echo off
setlocal enabledelayedexpansion
set a=1
set b=woof
echo %a%
echo %b%
call :swap a b
echo %a%
echo %b%
goto :eof
:swap
set temp1=!%1!
set temp2=!%2!
set %1=%temp2%
set %2=%temp1%
goto :eof

View file

@ -0,0 +1,8 @@
beads 1 program 'Generic swap'
var
a = [1 2 "Beads" 3 4]
b = [1 2 "Language" 4 5]
calc main_init
swap a[4] <=> b[3]

View file

@ -0,0 +1 @@
(!a.!b):(?b.?a)

View file

@ -0,0 +1 @@
\/

View file

@ -0,0 +1,6 @@
template<typename T> void swap(T& left, T& right)
{
T tmp(left);
left = right;
right = tmp;
}

View file

@ -0,0 +1 @@
std::swap(x,y);

View file

@ -0,0 +1,6 @@
template<class T>
void swap(T &lhs, T &rhs){
T tmp = std::move(lhs);
lhs = std::move(rhs);
rhs = std::move(tmp);
}

View file

@ -0,0 +1,6 @@
static void Swap<T>(ref T a, ref T b)
{
T temp = a;
a = b;
b = temp;
}

View file

@ -0,0 +1,3 @@
int a = 1;
int b = 2;
Swap(ref a, ref b); // Type parameter is inferred.

View file

@ -0,0 +1,3 @@
int a = 1;
int b = 2;
(a, b) = (b, a);

View file

@ -0,0 +1,6 @@
void swap(void *va, void *vb, size_t s)
{
char t, *a = (char*)va, *b = (char*)vb;
while(s--)
t = a[s], a[s] = b[s], b[s] = t;
}

View file

@ -0,0 +1 @@
#define Swap(X,Y) do{ __typeof__ (X) _T = X; X = Y; Y = _T; }while(0)

View file

@ -0,0 +1,32 @@
#include <stdio.h>
#define Swap(X,Y) do{ __typeof__ (X) _T = X; X = Y; Y = _T; }while(0)
struct test
{
int a, b, c;
};
int main()
{
struct test t = { 1, 2, 3 };
struct test h = { 4, 5, 6 };
double alfa = 0.45, omega = 9.98;
struct test *pt = &t;
struct test *th = &h;
printf("%d %d %d\n", t.a, t.b, t.c );
Swap(t, h);
printf("%d %d %d\n", t.a, t.b, t.c );
printf("%d %d %d\n", h.a, h.b, h.c );
printf("%lf\n", alfa);
Swap(alfa, omega);
printf("%lf\n", alfa);
printf("%d\n", pt->a);
Swap(pt, th);
printf("%d\n", pt->a);
}

View file

@ -0,0 +1,5 @@
function(swap var1 var2)
set(_SWAP_TEMPORARY "${${var1}}")
set(${var1} "${${var2}}" PARENT_SCOPE)
set(${var2} "${_SWAP_TEMPORARY}" PARENT_SCOPE)
endfunction(swap)

View file

@ -0,0 +1,5 @@
set(x 42)
set(y "string")
swap(x y)
message(STATUS ${x}) # -- string
message(STATUS ${y}) # -- 42

View file

@ -0,0 +1,5 @@
set(x 42)
set(_SWAP_TEMPORARY "string")
swap(x _SWAP_TEMPORARY)
message(STATUS ${x}) # -- 42
message(STATUS ${_SWAP_TEMPORARY}) # -- 42

View file

@ -0,0 +1,3 @@
string(TOUPPER CACHE x)
set(y "string")
swap(x y) # CMake Error... set given invalid arguments for CACHE mode.

View file

@ -0,0 +1,74 @@
PROGRAM-ID. SWAP-DEMO.
AUTHOR. Bill Gunshannon.
INSTALLATION. Home.
DATE-WRITTEN. 16 December 2021.
************************************************************
** Program Abstract:
** A simple program to demonstrate the SWAP subprogram.
**
************************************************************
DATA DIVISION.
WORKING-STORAGE SECTION.
01 Val1 PIC X(72).
01 Val2 PIC X(72).
PROCEDURE DIVISION.
Main-Program.
DISPLAY 'Enter a Value: ' WITH NO ADVANCING.
ACCEPT Val1.
DISPLAY 'Enter another Value: ' WITH NO ADVANCING.
ACCEPT Val2.
DISPLAY ' ' .
DISPLAY 'First value: ' FUNCTION TRIM(Val1) .
DISPLAY 'Second value: ' FUNCTION TRIM(Val2) .
CALL "SWAP" USING BY REFERENCE Val1, BY REFERENCE Val2.
DISPLAY ' '.
DISPLAY 'After SWAP '.
DISPLAY ' '.
DISPLAY 'First value: ' FUNCTION TRIM(Val1).
DISPLAY 'Second value: ' FUNCTION TRIM(Val2).
STOP RUN.
END PROGRAM SWAP-DEMO.
IDENTIFICATION DIVISION.
PROGRAM-ID. SWAP.
AUTHOR. Bill Gunshannon.
INSTALLATION. Home.
DATE-WRITTEN. 16 December 2021.
************************************************************
** Program Abstract:
** SWAP any Alphanumeric value. Only limit is 72
** character size. But that can be adjusted for
** whatever use one needs.
************************************************************
DATA DIVISION.
WORKING-STORAGE SECTION.
01 TEMP PIC X(72).
LINKAGE SECTION.
01 Field1 PIC X(72).
01 Field2 PIC X(72).
PROCEDURE DIVISION
USING BY REFERENCE Field1, BY REFERENCE Field2.
MOVE Field1 to TEMP.
MOVE Field2 to Field1.
MOVE TEMP to Field2.
GOBACK.
END PROGRAM SWAP.

View file

@ -0,0 +1 @@
a <=> b

View file

@ -0,0 +1 @@
(a, b) = (b, a)

View file

@ -0,0 +1,3 @@
(defn swap [pair] (reverse pair)) ; returns a list
(defn swap [[a b]] '(b a)) ; returns a list
(defn swap [[a b]] [b a]) ; returns a vector

View file

@ -0,0 +1,3 @@
<cfset temp = a />
<cfset a = b />
<cfset b = temp />

View file

@ -0,0 +1,3 @@
(rotatef a b)
(psetq a b b a)

View file

@ -0,0 +1,12 @@
LDA 29
STA 31
LDA 30
STA 29
LDA 31
STA 30
STP
---
org 29
byte $0F
byte $E0
byte $00

View file

@ -0,0 +1 @@
a, b = b, a

View file

@ -0,0 +1,24 @@
import std.algorithm: swap; // from Phobos standard library
// The D solution uses templates and it's similar to the C++ one:
void mySwap(T)(ref T left, ref T right) {
auto temp = left;
left = right;
right = temp;
}
void main() {
import std.stdio;
int[] a = [10, 20];
writeln(a);
// The std.algorithm standard library module
// contains a generic swap:
swap(a[0], a[1]);
writeln(a);
// Using mySwap:
mySwap(a[0], a[1]);
writeln(a);
}

View file

@ -0,0 +1,12 @@
$ a1 = 123
$ a2 = "hello"
$ show symbol a*
$ gosub swap
$ show symbol a*
$ exit
$
$ swap:
$ t = a1
$ a1 = a2
$ a2 = t
$ return

View file

@ -0,0 +1,2 @@
1 2 SaSbLaLb f
=2 1

View file

@ -0,0 +1,2 @@
1 2 r f
=2 1

View file

@ -0,0 +1,8 @@
procedure Swap_T(var a, b: T);
var
temp: T;
begin
temp := a;
a := b;
b := temp;
end;

View file

@ -0,0 +1,26 @@
program GenericSwap;
type
TSwap = class
class procedure Swap<T>(var left, right: T);
end;
class procedure TSwap.Swap<T>(var left, right: T);
var
temp : T;
begin
temp := left;
left := right;
right := temp;
end;
var
a, b : integer;
begin
a := 5;
b := 3;
writeln('Before swap: a=', a, ' b=', b);
TSwap.Swap<integer>(a, b);
writeln('After swap: a=', a, ' b=', b);
end.

View file

@ -0,0 +1,5 @@
def swap(&left, &right) {
def t := left
left := right
right := t
}

View file

@ -0,0 +1,3 @@
def swap([left, right]) {
return [right, left]
}

View file

@ -0,0 +1,8 @@
fun swap = Pair by var a, var b do return var%var(b => a) end
int a = 1
int b = 2
writeLine("before swap: a=" + a + ", b=" + b)
Pair pair = swap(a, b)
a = pair[0]
b = pair[1]
writeLine(" after swap: a=" + a + ", b=" + b)

View file

@ -0,0 +1,20 @@
;; 1)
;; a macro will do it, as shown in Racket (same syntax)
(define-syntax-rule (swap a b)
(let ([tmp a])
(set! a b)
(set! b tmp)))
(define A 666)
(define B "simon")
(swap A B)
A → "simon"
B → 666
;; 2)
;; The list-swap! function allows to swap two items inside a list, regardless of their types
;; This physically alters the list
(define L ' ( 1 2 3 4 🎩 ))
(list-swap! L 1 ' 🎩 )
→ (🎩 2 3 4 1)

View file

@ -0,0 +1,21 @@
import extensions;
swap(ref object v1, ref object v2)
{
var tmp := v1;
v1 := v2;
v2 := tmp
}
public program()
{
var n := 2;
var s := "abc";
console.printLine(n," ",s);
swap(ref n, ref s);
console.printLine(n," ",s)
}

View file

@ -0,0 +1,10 @@
x = 4
y = 5
{y,x} = {x,y}
y # => 4
x # => 5
[x,y] = [y,x]
x # => 4
y # => 5

View file

@ -0,0 +1,4 @@
swap = fn x,y -> [y|x] end
[x|y] = swap.(1,2)
x # => 2
y # => 1

View file

@ -0,0 +1,9 @@
swap_tuple = fn {x,y} -> {y,x} end
{a,b} = swap_tuple.({1,:ok})
a # => :ok
b # => 1
swap_list = fn [x,y] -> [y,x] end
[a,b] = swap_list.([1,"2"])
a # => "2"
b # => 1

View file

@ -0,0 +1,6 @@
(defun swap (a-sym b-sym)
"Swap values of the variables given by A-SYM and B-SYM."
(let ((a-val (symbol-value a-sym)))
(set a-sym (symbol-value b-sym))
(set b-sym a-val)))
(swap 'a 'b)

View file

@ -0,0 +1,2 @@
(defmacro swap (a b)
`(setq ,b (prog1 ,a (setq ,a ,b))))

View file

@ -0,0 +1,8 @@
(require 'cl-lib)
(defmacro swap (a b)
`(cl-psetf ,a ,b
,b ,a))
(setq lst (list 123 456))
(swap (car lst) (cadr lst))
;; now lst is '(456 123)

View file

@ -0,0 +1,4 @@
1> L = [a, 2].
[a,2]
2> lists:reverse(L).
[2,a]

View file

@ -0,0 +1,4 @@
1> T = {2,a}.
{2,a}
2> list_to_tuple(lists:reverse(tuple_to_list(T))).
{a,2}

View file

@ -0,0 +1,9 @@
include std/console.e -- for display
object x = 3.14159
object y = "Rosettacode"
{y,x} = {x,y}
display("x is now []",{x})
display("y is now []",{y})

View file

@ -0,0 +1 @@
let swap (a,b) = (b,a)

View file

@ -0,0 +1 @@
swap

View file

@ -0,0 +1,3 @@
a = 1
b = 2
a,b = arr = b,a

View file

@ -0,0 +1 @@
$

View file

@ -0,0 +1 @@
swap

View file

@ -0,0 +1,43 @@
MODULE Genericswap
IMPLICIT NONE
INTERFACE Swap
MODULE PROCEDURE Swapint, Swapreal, Swapstring
END INTERFACE
CONTAINS
SUBROUTINE Swapint(a, b)
INTEGER, INTENT(IN OUT) :: a, b
INTEGER :: temp
temp = a ; a = b ; b = temp
END SUBROUTINE Swapint
SUBROUTINE Swapreal(a, b)
REAL, INTENT(IN OUT) :: a, b
REAL :: temp
temp = a ; a = b ; b = temp
END SUBROUTINE Swapreal
SUBROUTINE Swapstring(a, b)
CHARACTER(*), INTENT(IN OUT) :: a, b
CHARACTER(len(a)) :: temp
temp = a ; a = b ; b = temp
END SUBROUTINE Swapstring
END MODULE Genericswap
PROGRAM EXAMPLE
USE Genericswap
IMPLICIT NONE
INTEGER :: i1 = 1, i2 = 2
REAL :: r1 = 1.0, r2 = 2.0
CHARACTER(3) :: s1="abc", s2="xyz"
CALL Swap(i1, i2)
CALL Swap(r1, r2)
CALL Swap(s1, s2)
WRITE(*,*) i1, i2 ! Prints 2 and 1
WRITE(*,*) r1, r2 ! Prints 2.0 and 1.0
WRITE(*,*) s1, s2 ! Prints xyz and abc
END PROGRAM EXAMPLE

View file

@ -0,0 +1,19 @@
{$ifdef fpc}{$mode delphi}{$H+}{$endif}
{ note this is compiled with delphi mode but will only compile in Free Pascal }
{ Delphi doesn't support this syntax }
procedure swap<T>(var left,right:T);
var
temp:T;
begin
temp:=left;
left:=right;
right:=temp;
end;
var
a:string = 'Test';
b:string = 'me';
begin
writeln(a:6,b:6);
swap<string>(a,b);
writeln(a:6,b:6);
end.

View file

@ -0,0 +1,26 @@
' FB 1.05.0
#Macro Declare_Swap(T)
Sub Swap_##T(ByRef t1 As T, ByRef t2 As T)
Dim temp As T = t2
t2 = t1
t1 = temp
End Sub
#EndMacro
Dim As Integer i, j
i = 1 : j = 2
Declare_Swap(Integer) ' expands the macro
Swap_Integer(i, j)
Print i, j
Dim As String s, t
s = "Hello" : t = "World"
Declare_Swap(String)
Swap_String(s, t)
Print s, t
Print
Print "Press any key to exit"
Sleep

View file

@ -0,0 +1 @@
[b,a] = [a,b]

View file

@ -0,0 +1,28 @@
window 1, @"Generic Swap", (0,0,480,270)
text ,,,,, 60
long i, j
double x, y
CFStringRef a, b
i = 1059 : j = 62
print i, j
swap i, j
print i, j
print
x = 1.23 : y = 4.56
print x, y
swap x, y
print x, y
print
a = @"Hello" : b = @"World!"
print a, b
swap a, b
print a, b
HandleEvents

View file

@ -0,0 +1,6 @@
10 CLS : REM 10 HOME for Applesoft BASIC
20 A = 1 : B = 2
30 PRINT " A=";A," B=";B
40 T = A : A = B : B = T
50 PRINT " A=";A," B=";B
60 END

View file

@ -0,0 +1,9 @@
Public Sub Main()
Dim vA As Variant = " World"
Dim vB As Variant = 1
Swap vA, vB
Print vA; vB
End

View file

@ -0,0 +1 @@
1 !0 2 !1

View file

@ -0,0 +1 @@
&0 &1 !0 pop !1

View file

@ -0,0 +1 @@
a, b = b, a

View file

@ -0,0 +1,14 @@
package main
import "fmt"
func swap(a, b *interface{}) {
*a, *b = *b, *a
}
func main() {
var a, b interface{} = 3, "four"
fmt.Println(a, b)
swap(&a, &b)
fmt.Println(a, b)
}

View file

@ -0,0 +1,51 @@
package main
import (
"fmt"
"reflect"
)
func swap(a, b interface{}) error {
ta := reflect.TypeOf(a)
tb := reflect.TypeOf(b)
if ta != tb {
return fmt.Errorf("swap args are different types: %v and %v", ta, tb)
}
if ta.Kind() != reflect.Ptr {
return fmt.Errorf("swap args must be pointers")
}
ea := reflect.ValueOf(a).Elem()
eb := reflect.ValueOf(b).Elem()
temp := reflect.New(ea.Type()).Elem()
temp.Set(ea)
ea.Set(eb)
eb.Set(temp)
return nil
}
func main() {
a, b := 3, "cats"
fmt.Println("a b:", a, b)
err := swap(a, b)
fmt.Println(err, "\n")
c, d := 3, 4
fmt.Println("c d:", c, d)
err = swap(c, d)
fmt.Println(err, "\n")
e, f := 3, 4
fmt.Println("e f:", e, f)
swap(&e, &f)
fmt.Println("e f:", e, f, "\n")
type mult struct {
int
string
}
g, h := mult{3, "cats"}, mult{4, "dogs"}
fmt.Println("g h:", g, h)
swap(&g, &h)
fmt.Println("g h:", g, h)
}

View file

@ -0,0 +1,15 @@
`Swap Vars &.a. &.b.'
{
new .temp.
.temp. = \.word2.
\.word2. = \.word3.
\.word3. = .temp.
delete .temp.
}
.foo. = 123
.bar. = 456
Swap Vars &.foo. &.bar.
show .foo. " " .bar.
# prints "456 123"

View file

@ -0,0 +1,15 @@
`Swap Syns &\a &\b'
{
new \temp
\temp = "\.word2."
\.word2. = "\.word3."
\.word3. = "\temp"
delete \temp
}
\quux = "one"
\xyzzy = "two"
Swap Syns &\quux &\xyzzy
show "\quux \xyzzy"
# prints "two one"

View file

@ -0,0 +1 @@
(a, b) = [b, a]

Some files were not shown because too many files have changed in this diff Show more