Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -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.

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

@ -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;
}

View file

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

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,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 @@
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);
}

View 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"