Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
|
|
@ -1,6 +1,8 @@
|
|||
The task is to 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.
|
||||
The task is to 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.
|
||||
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.
|
||||
|
||||
|
|
|
|||
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);
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
void swap(void *va, void *vb, size_t s)
|
||||
{
|
||||
char t, *a = (char*)va, *b = (char*)vb;
|
||||
while(--s)
|
||||
while(s--)
|
||||
t = a[s], a[s] = b[s], b[s] = t;
|
||||
}
|
||||
|
|
|
|||
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
|
||||
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
|
||||
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/Rust/generic-swap.rust
Normal file
6
Task/Generic-swap/Rust/generic-swap.rust
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
fn main() {
|
||||
let mut a="Anna".to_owned();
|
||||
let mut b="Bob".to_owned();
|
||||
std::mem::swap(&mut a, &mut b);
|
||||
println!("a={},b={}",a,b);
|
||||
}
|
||||
5
Task/Generic-swap/Tcl/generic-swap-5.tcl
Normal file
5
Task/Generic-swap/Tcl/generic-swap-5.tcl
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
set a 1
|
||||
set b 2
|
||||
puts "before\ta=$a\tb=$b"
|
||||
set a $b[set b $a;lindex {}]
|
||||
puts "after\ta=$a\tb=$b"
|
||||
Loading…
Add table
Add a link
Reference in a new issue