Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
3
Task/Generic-swap/00-META.yaml
Normal file
3
Task/Generic-swap/00-META.yaml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Generic_swap
|
||||
note: Basic language learning
|
||||
19
Task/Generic-swap/00-TASK.txt
Normal file
19
Task/Generic-swap/00-TASK.txt
Normal 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>
|
||||
|
||||
2
Task/Generic-swap/11l/generic-swap.11l
Normal file
2
Task/Generic-swap/11l/generic-swap.11l
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
F swap(&a, &b)
|
||||
(a, b) = (b, a)
|
||||
20
Task/Generic-swap/360-Assembly/generic-swap.360
Normal file
20
Task/Generic-swap/360-Assembly/generic-swap.360
Normal 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
|
||||
11
Task/Generic-swap/6502-Assembly/generic-swap-1.6502
Normal file
11
Task/Generic-swap/6502-Assembly/generic-swap-1.6502
Normal 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
|
||||
5
Task/Generic-swap/6502-Assembly/generic-swap-2.6502
Normal file
5
Task/Generic-swap/6502-Assembly/generic-swap-2.6502
Normal 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
|
||||
12
Task/Generic-swap/6502-Assembly/generic-swap-3.6502
Normal file
12
Task/Generic-swap/6502-Assembly/generic-swap-3.6502
Normal 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
|
||||
11
Task/Generic-swap/6502-Assembly/generic-swap-4.6502
Normal file
11
Task/Generic-swap/6502-Assembly/generic-swap-4.6502
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
lda #$33
|
||||
ldx #$44
|
||||
ldy #$55
|
||||
|
||||
pha
|
||||
phx
|
||||
phy
|
||||
|
||||
pla ;a=#$55
|
||||
ply ;y=#$44
|
||||
plx ;x=#$33
|
||||
2
Task/Generic-swap/68000-Assembly/generic-swap-1.68000
Normal file
2
Task/Generic-swap/68000-Assembly/generic-swap-1.68000
Normal 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
|
||||
4
Task/Generic-swap/68000-Assembly/generic-swap-2.68000
Normal file
4
Task/Generic-swap/68000-Assembly/generic-swap-2.68000
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
MOVE.L ($00FF0000),D0
|
||||
MOVE.L ($00FF1000),D1
|
||||
MOVE.L D0,($00FF1000)
|
||||
MOVE.L D1,($00FF0000)
|
||||
2
Task/Generic-swap/68000-Assembly/generic-swap-3.68000
Normal file
2
Task/Generic-swap/68000-Assembly/generic-swap-3.68000
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
MOVE.L #$1234ABCD,D0
|
||||
SWAP D0 ;now D0 = #$ABCD1234
|
||||
22
Task/Generic-swap/8086-Assembly/generic-swap.8086
Normal file
22
Task/Generic-swap/8086-Assembly/generic-swap.8086
Normal 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.
|
||||
1
Task/Generic-swap/8th/generic-swap-1.8th
Normal file
1
Task/Generic-swap/8th/generic-swap-1.8th
Normal file
|
|
@ -0,0 +1 @@
|
|||
swap
|
||||
1
Task/Generic-swap/8th/generic-swap-2.8th
Normal file
1
Task/Generic-swap/8th/generic-swap-2.8th
Normal file
|
|
@ -0,0 +1 @@
|
|||
dup @ -rot !
|
||||
6
Task/Generic-swap/ACL2/generic-swap.acl2
Normal file
6
Task/Generic-swap/ACL2/generic-swap.acl2
Normal 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)))
|
||||
13
Task/Generic-swap/ALGOL-68/generic-swap.alg
Normal file
13
Task/Generic-swap/ALGOL-68/generic-swap.alg
Normal 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))
|
||||
25
Task/Generic-swap/AWK/generic-swap.awk
Normal file
25
Task/Generic-swap/AWK/generic-swap.awk
Normal 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)
|
||||
}
|
||||
42
Task/Generic-swap/Action-/generic-swap.action
Normal file
42
Task/Generic-swap/Action-/generic-swap.action
Normal 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
|
||||
10
Task/Generic-swap/Ada/generic-swap-1.ada
Normal file
10
Task/Generic-swap/Ada/generic-swap-1.ada
Normal 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;
|
||||
7
Task/Generic-swap/Ada/generic-swap-2.ada
Normal file
7
Task/Generic-swap/Ada/generic-swap-2.ada
Normal 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);
|
||||
12
Task/Generic-swap/Aime/generic-swap.aime
Normal file
12
Task/Generic-swap/Aime/generic-swap.aime
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
void
|
||||
__swap(&, &,,)
|
||||
{
|
||||
set(0, $3);
|
||||
set(1, $2);
|
||||
}
|
||||
|
||||
void
|
||||
swap(&, &)
|
||||
{
|
||||
xcall(xcall, __swap);
|
||||
}
|
||||
12
Task/Generic-swap/Amazing-Hopper/generic-swap.hopper
Normal file
12
Task/Generic-swap/Amazing-Hopper/generic-swap.hopper
Normal 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
|
||||
16
Task/Generic-swap/AmigaE/generic-swap.amiga
Normal file
16
Task/Generic-swap/AmigaE/generic-swap.amiga
Normal 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
|
||||
1
Task/Generic-swap/AppleScript/generic-swap.applescript
Normal file
1
Task/Generic-swap/AppleScript/generic-swap.applescript
Normal file
|
|
@ -0,0 +1 @@
|
|||
set {x,y} to {y,x}
|
||||
1
Task/Generic-swap/Applesoft-BASIC/generic-swap.basic
Normal file
1
Task/Generic-swap/Applesoft-BASIC/generic-swap.basic
Normal file
|
|
@ -0,0 +1 @@
|
|||
A=43:B=47:H=A:A=B:B=H:?" A="A" B="B;
|
||||
10
Task/Generic-swap/Arc/generic-swap.arc
Normal file
10
Task/Generic-swap/Arc/generic-swap.arc
Normal 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))
|
||||
8
Task/Generic-swap/Arturo/generic-swap-1.arturo
Normal file
8
Task/Generic-swap/Arturo/generic-swap-1.arturo
Normal 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]
|
||||
12
Task/Generic-swap/Arturo/generic-swap-2.arturo
Normal file
12
Task/Generic-swap/Arturo/generic-swap-2.arturo
Normal 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]
|
||||
6
Task/Generic-swap/AutoHotkey/generic-swap.ahk
Normal file
6
Task/Generic-swap/AutoHotkey/generic-swap.ahk
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
Swap(ByRef Left, ByRef Right)
|
||||
{
|
||||
temp := Left
|
||||
Left := Right
|
||||
Right := temp
|
||||
}
|
||||
1
Task/Generic-swap/Axe/generic-swap.axe
Normal file
1
Task/Generic-swap/Axe/generic-swap.axe
Normal file
|
|
@ -0,0 +1 @@
|
|||
Exch(°A,°B,2)
|
||||
12
Task/Generic-swap/BASIC256/generic-swap.basic
Normal file
12
Task/Generic-swap/BASIC256/generic-swap.basic
Normal 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
|
||||
7
Task/Generic-swap/BBC-BASIC/generic-swap-1.basic
Normal file
7
Task/Generic-swap/BBC-BASIC/generic-swap-1.basic
Normal 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$
|
||||
15
Task/Generic-swap/BBC-BASIC/generic-swap-2.basic
Normal file
15
Task/Generic-swap/BBC-BASIC/generic-swap-2.basic
Normal 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
|
||||
1
Task/Generic-swap/BQN/generic-swap.bqn
Normal file
1
Task/Generic-swap/BQN/generic-swap.bqn
Normal file
|
|
@ -0,0 +1 @@
|
|||
a‿b ⌽↩
|
||||
5
Task/Generic-swap/BaCon/generic-swap.bacon
Normal file
5
Task/Generic-swap/BaCon/generic-swap.bacon
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
x = 1
|
||||
y$ = "hello"
|
||||
|
||||
SWAP x, y$
|
||||
PRINT y$
|
||||
17
Task/Generic-swap/Batch-File/generic-swap.bat
Normal file
17
Task/Generic-swap/Batch-File/generic-swap.bat
Normal 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
|
||||
8
Task/Generic-swap/Beads/generic-swap.beads
Normal file
8
Task/Generic-swap/Beads/generic-swap.beads
Normal 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]
|
||||
1
Task/Generic-swap/Bracmat/generic-swap.bracmat
Normal file
1
Task/Generic-swap/Bracmat/generic-swap.bracmat
Normal file
|
|
@ -0,0 +1 @@
|
|||
(!a.!b):(?b.?a)
|
||||
1
Task/Generic-swap/Burlesque/generic-swap.blq
Normal file
1
Task/Generic-swap/Burlesque/generic-swap.blq
Normal file
|
|
@ -0,0 +1 @@
|
|||
\/
|
||||
6
Task/Generic-swap/C++/generic-swap-1.cpp
Normal file
6
Task/Generic-swap/C++/generic-swap-1.cpp
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
template<typename T> void swap(T& left, T& right)
|
||||
{
|
||||
T tmp(left);
|
||||
left = right;
|
||||
right = tmp;
|
||||
}
|
||||
1
Task/Generic-swap/C++/generic-swap-2.cpp
Normal file
1
Task/Generic-swap/C++/generic-swap-2.cpp
Normal file
|
|
@ -0,0 +1 @@
|
|||
std::swap(x,y);
|
||||
6
Task/Generic-swap/C++/generic-swap-3.cpp
Normal file
6
Task/Generic-swap/C++/generic-swap-3.cpp
Normal 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);
|
||||
}
|
||||
6
Task/Generic-swap/C-sharp/generic-swap-1.cs
Normal file
6
Task/Generic-swap/C-sharp/generic-swap-1.cs
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
static void Swap<T>(ref T a, ref T b)
|
||||
{
|
||||
T temp = a;
|
||||
a = b;
|
||||
b = temp;
|
||||
}
|
||||
3
Task/Generic-swap/C-sharp/generic-swap-2.cs
Normal file
3
Task/Generic-swap/C-sharp/generic-swap-2.cs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
int a = 1;
|
||||
int b = 2;
|
||||
Swap(ref a, ref b); // Type parameter is inferred.
|
||||
3
Task/Generic-swap/C-sharp/generic-swap-3.cs
Normal file
3
Task/Generic-swap/C-sharp/generic-swap-3.cs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
int a = 1;
|
||||
int b = 2;
|
||||
(a, b) = (b, a);
|
||||
6
Task/Generic-swap/C/generic-swap-1.c
Normal file
6
Task/Generic-swap/C/generic-swap-1.c
Normal 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;
|
||||
}
|
||||
1
Task/Generic-swap/C/generic-swap-2.c
Normal file
1
Task/Generic-swap/C/generic-swap-2.c
Normal file
|
|
@ -0,0 +1 @@
|
|||
#define Swap(X,Y) do{ __typeof__ (X) _T = X; X = Y; Y = _T; }while(0)
|
||||
32
Task/Generic-swap/C/generic-swap-3.c
Normal file
32
Task/Generic-swap/C/generic-swap-3.c
Normal 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);
|
||||
}
|
||||
5
Task/Generic-swap/CMake/generic-swap-1.cmake
Normal file
5
Task/Generic-swap/CMake/generic-swap-1.cmake
Normal 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)
|
||||
5
Task/Generic-swap/CMake/generic-swap-2.cmake
Normal file
5
Task/Generic-swap/CMake/generic-swap-2.cmake
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
set(x 42)
|
||||
set(y "string")
|
||||
swap(x y)
|
||||
message(STATUS ${x}) # -- string
|
||||
message(STATUS ${y}) # -- 42
|
||||
5
Task/Generic-swap/CMake/generic-swap-3.cmake
Normal file
5
Task/Generic-swap/CMake/generic-swap-3.cmake
Normal 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
|
||||
3
Task/Generic-swap/CMake/generic-swap-4.cmake
Normal file
3
Task/Generic-swap/CMake/generic-swap-4.cmake
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
string(TOUPPER CACHE x)
|
||||
set(y "string")
|
||||
swap(x y) # CMake Error... set given invalid arguments for CACHE mode.
|
||||
74
Task/Generic-swap/COBOL/generic-swap.cobol
Normal file
74
Task/Generic-swap/COBOL/generic-swap.cobol
Normal 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.
|
||||
1
Task/Generic-swap/Chapel/generic-swap-1.chapel
Normal file
1
Task/Generic-swap/Chapel/generic-swap-1.chapel
Normal file
|
|
@ -0,0 +1 @@
|
|||
a <=> b
|
||||
1
Task/Generic-swap/Chapel/generic-swap-2.chapel
Normal file
1
Task/Generic-swap/Chapel/generic-swap-2.chapel
Normal file
|
|
@ -0,0 +1 @@
|
|||
(a, b) = (b, a)
|
||||
3
Task/Generic-swap/Clojure/generic-swap.clj
Normal file
3
Task/Generic-swap/Clojure/generic-swap.clj
Normal 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
|
||||
3
Task/Generic-swap/ColdFusion/generic-swap.cfm
Normal file
3
Task/Generic-swap/ColdFusion/generic-swap.cfm
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
<cfset temp = a />
|
||||
<cfset a = b />
|
||||
<cfset b = temp />
|
||||
3
Task/Generic-swap/Common-Lisp/generic-swap.lisp
Normal file
3
Task/Generic-swap/Common-Lisp/generic-swap.lisp
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
(rotatef a b)
|
||||
|
||||
(psetq a b b a)
|
||||
12
Task/Generic-swap/Computer-zero-Assembly/generic-swap.0asm
Normal file
12
Task/Generic-swap/Computer-zero-Assembly/generic-swap.0asm
Normal 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
|
||||
1
Task/Generic-swap/Crystal/generic-swap.crystal
Normal file
1
Task/Generic-swap/Crystal/generic-swap.crystal
Normal file
|
|
@ -0,0 +1 @@
|
|||
a, b = b, a
|
||||
24
Task/Generic-swap/D/generic-swap.d
Normal file
24
Task/Generic-swap/D/generic-swap.d
Normal 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);
|
||||
}
|
||||
12
Task/Generic-swap/DCL/generic-swap.dcl
Normal file
12
Task/Generic-swap/DCL/generic-swap.dcl
Normal 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
|
||||
2
Task/Generic-swap/Dc/generic-swap-1.dc
Normal file
2
Task/Generic-swap/Dc/generic-swap-1.dc
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
1 2 SaSbLaLb f
|
||||
=2 1
|
||||
2
Task/Generic-swap/Dc/generic-swap-2.dc
Normal file
2
Task/Generic-swap/Dc/generic-swap-2.dc
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
1 2 r f
|
||||
=2 1
|
||||
8
Task/Generic-swap/Delphi/generic-swap-1.delphi
Normal file
8
Task/Generic-swap/Delphi/generic-swap-1.delphi
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
procedure Swap_T(var a, b: T);
|
||||
var
|
||||
temp: T;
|
||||
begin
|
||||
temp := a;
|
||||
a := b;
|
||||
b := temp;
|
||||
end;
|
||||
26
Task/Generic-swap/Delphi/generic-swap-2.delphi
Normal file
26
Task/Generic-swap/Delphi/generic-swap-2.delphi
Normal 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.
|
||||
5
Task/Generic-swap/E/generic-swap-1.e
Normal file
5
Task/Generic-swap/E/generic-swap-1.e
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
def swap(&left, &right) {
|
||||
def t := left
|
||||
left := right
|
||||
right := t
|
||||
}
|
||||
3
Task/Generic-swap/E/generic-swap-2.e
Normal file
3
Task/Generic-swap/E/generic-swap-2.e
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
def swap([left, right]) {
|
||||
return [right, left]
|
||||
}
|
||||
8
Task/Generic-swap/EMal/generic-swap.emal
Normal file
8
Task/Generic-swap/EMal/generic-swap.emal
Normal 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)
|
||||
20
Task/Generic-swap/EchoLisp/generic-swap.l
Normal file
20
Task/Generic-swap/EchoLisp/generic-swap.l
Normal 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)
|
||||
21
Task/Generic-swap/Elena/generic-swap.elena
Normal file
21
Task/Generic-swap/Elena/generic-swap.elena
Normal 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)
|
||||
}
|
||||
10
Task/Generic-swap/Elixir/generic-swap-1.elixir
Normal file
10
Task/Generic-swap/Elixir/generic-swap-1.elixir
Normal 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
|
||||
4
Task/Generic-swap/Elixir/generic-swap-2.elixir
Normal file
4
Task/Generic-swap/Elixir/generic-swap-2.elixir
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
swap = fn x,y -> [y|x] end
|
||||
[x|y] = swap.(1,2)
|
||||
x # => 2
|
||||
y # => 1
|
||||
9
Task/Generic-swap/Elixir/generic-swap-3.elixir
Normal file
9
Task/Generic-swap/Elixir/generic-swap-3.elixir
Normal 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
|
||||
6
Task/Generic-swap/Emacs-Lisp/generic-swap-1.l
Normal file
6
Task/Generic-swap/Emacs-Lisp/generic-swap-1.l
Normal 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)
|
||||
2
Task/Generic-swap/Emacs-Lisp/generic-swap-2.l
Normal file
2
Task/Generic-swap/Emacs-Lisp/generic-swap-2.l
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
(defmacro swap (a b)
|
||||
`(setq ,b (prog1 ,a (setq ,a ,b))))
|
||||
8
Task/Generic-swap/Emacs-Lisp/generic-swap-3.l
Normal file
8
Task/Generic-swap/Emacs-Lisp/generic-swap-3.l
Normal 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)
|
||||
4
Task/Generic-swap/Erlang/generic-swap-1.erl
Normal file
4
Task/Generic-swap/Erlang/generic-swap-1.erl
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
1> L = [a, 2].
|
||||
[a,2]
|
||||
2> lists:reverse(L).
|
||||
[2,a]
|
||||
4
Task/Generic-swap/Erlang/generic-swap-2.erl
Normal file
4
Task/Generic-swap/Erlang/generic-swap-2.erl
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
1> T = {2,a}.
|
||||
{2,a}
|
||||
2> list_to_tuple(lists:reverse(tuple_to_list(T))).
|
||||
{a,2}
|
||||
9
Task/Generic-swap/Euphoria/generic-swap.euphoria
Normal file
9
Task/Generic-swap/Euphoria/generic-swap.euphoria
Normal 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})
|
||||
1
Task/Generic-swap/F-Sharp/generic-swap.fs
Normal file
1
Task/Generic-swap/F-Sharp/generic-swap.fs
Normal file
|
|
@ -0,0 +1 @@
|
|||
let swap (a,b) = (b,a)
|
||||
1
Task/Generic-swap/Factor/generic-swap.factor
Normal file
1
Task/Generic-swap/Factor/generic-swap.factor
Normal file
|
|
@ -0,0 +1 @@
|
|||
swap
|
||||
3
Task/Generic-swap/Falcon/generic-swap.falcon
Normal file
3
Task/Generic-swap/Falcon/generic-swap.falcon
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
a = 1
|
||||
b = 2
|
||||
a,b = arr = b,a
|
||||
1
Task/Generic-swap/Fish/generic-swap.fish
Normal file
1
Task/Generic-swap/Fish/generic-swap.fish
Normal file
|
|
@ -0,0 +1 @@
|
|||
$
|
||||
1
Task/Generic-swap/Forth/generic-swap.fth
Normal file
1
Task/Generic-swap/Forth/generic-swap.fth
Normal file
|
|
@ -0,0 +1 @@
|
|||
swap
|
||||
43
Task/Generic-swap/Fortran/generic-swap.f
Normal file
43
Task/Generic-swap/Fortran/generic-swap.f
Normal 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
|
||||
19
Task/Generic-swap/Free-Pascal-Lazarus/generic-swap.pas
Normal file
19
Task/Generic-swap/Free-Pascal-Lazarus/generic-swap.pas
Normal 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.
|
||||
26
Task/Generic-swap/FreeBASIC/generic-swap.basic
Normal file
26
Task/Generic-swap/FreeBASIC/generic-swap.basic
Normal 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
|
||||
1
Task/Generic-swap/Frink/generic-swap.frink
Normal file
1
Task/Generic-swap/Frink/generic-swap.frink
Normal file
|
|
@ -0,0 +1 @@
|
|||
[b,a] = [a,b]
|
||||
28
Task/Generic-swap/FutureBasic/generic-swap.basic
Normal file
28
Task/Generic-swap/FutureBasic/generic-swap.basic
Normal 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
|
||||
6
Task/Generic-swap/GW-BASIC/generic-swap.basic
Normal file
6
Task/Generic-swap/GW-BASIC/generic-swap.basic
Normal 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
|
||||
9
Task/Generic-swap/Gambas/generic-swap.gambas
Normal file
9
Task/Generic-swap/Gambas/generic-swap.gambas
Normal 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
|
||||
1
Task/Generic-swap/Gecho/generic-swap-1.gecho
Normal file
1
Task/Generic-swap/Gecho/generic-swap-1.gecho
Normal file
|
|
@ -0,0 +1 @@
|
|||
1 !0 2 !1
|
||||
1
Task/Generic-swap/Gecho/generic-swap-2.gecho
Normal file
1
Task/Generic-swap/Gecho/generic-swap-2.gecho
Normal file
|
|
@ -0,0 +1 @@
|
|||
&0 &1 !0 pop !1
|
||||
1
Task/Generic-swap/Go/generic-swap-1.go
Normal file
1
Task/Generic-swap/Go/generic-swap-1.go
Normal file
|
|
@ -0,0 +1 @@
|
|||
a, b = b, a
|
||||
14
Task/Generic-swap/Go/generic-swap-2.go
Normal file
14
Task/Generic-swap/Go/generic-swap-2.go
Normal 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)
|
||||
}
|
||||
51
Task/Generic-swap/Go/generic-swap-3.go
Normal file
51
Task/Generic-swap/Go/generic-swap-3.go
Normal 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)
|
||||
}
|
||||
15
Task/Generic-swap/Gri/generic-swap-1.gri
Normal file
15
Task/Generic-swap/Gri/generic-swap-1.gri
Normal 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"
|
||||
15
Task/Generic-swap/Gri/generic-swap-2.gri
Normal file
15
Task/Generic-swap/Gri/generic-swap-2.gri
Normal 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"
|
||||
1
Task/Generic-swap/Groovy/generic-swap-1.groovy
Normal file
1
Task/Generic-swap/Groovy/generic-swap-1.groovy
Normal file
|
|
@ -0,0 +1 @@
|
|||
(a, b) = [b, a]
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue