renamed samples to examples
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
|
||||
fun x(a, b, c)
|
||||
print('a = ', a, '\nb = ', b, '\nc = ', c, '\n\n');
|
||||
|
||||
x(1, 2, 3, 4, 5);
|
||||
x(1, 2, 3, 4);
|
||||
x(1, 2, 3);
|
||||
x(1, 2);
|
||||
x(1);
|
||||
x();
|
||||
@@ -0,0 +1,40 @@
|
||||
|
||||
fun copy(L) {
|
||||
|
||||
L2 = [];
|
||||
|
||||
i = 0;
|
||||
while i < count(L): {
|
||||
L2[i] = L[i];
|
||||
i = i + 1;
|
||||
}
|
||||
|
||||
return L2;
|
||||
}
|
||||
|
||||
fun bubble_sort(L, less) {
|
||||
if less == none:
|
||||
fun less(a, b) return a < b;
|
||||
|
||||
L = copy(L);
|
||||
|
||||
do {
|
||||
swapped = false;
|
||||
|
||||
i = 0;
|
||||
while i < count(L)-1 and not swapped: {
|
||||
if less(L[i+1], L[i]): {
|
||||
swapped = true;
|
||||
tmp = L[i+1];
|
||||
L[i+1] = L[i];
|
||||
L[i] = tmp;
|
||||
}
|
||||
i = i + 1;
|
||||
}
|
||||
} while swapped;
|
||||
|
||||
return L;
|
||||
}
|
||||
|
||||
|
||||
print(bubble_sort([3, 2, 1, 6, 0-2]));
|
||||
@@ -0,0 +1,21 @@
|
||||
|
||||
b = newBuffer(16);
|
||||
|
||||
b[2] = 10;
|
||||
|
||||
print(b, '\n');
|
||||
|
||||
fun toArray(buffer) {
|
||||
|
||||
i = 0;
|
||||
r = [];
|
||||
|
||||
while i < count(buffer): {
|
||||
r[i] = buffer[i];
|
||||
i = i+1;
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
print(toArray(b), '\n');
|
||||
@@ -0,0 +1,14 @@
|
||||
|
||||
fun fail_at_depth(depth)
|
||||
{
|
||||
fun fail(current_depth)
|
||||
{
|
||||
assert(current_depth < depth);
|
||||
fail(current_depth + 1);
|
||||
}
|
||||
|
||||
fail(0);
|
||||
}
|
||||
|
||||
|
||||
fail_at_depth(9);
|
||||
@@ -0,0 +1 @@
|
||||
print('Hello, world!\n');
|
||||
@@ -0,0 +1,5 @@
|
||||
|
||||
l = [1, 2, 3, 4];
|
||||
|
||||
print('The list contains ', count(l), ' items.\n');
|
||||
print('The list is: ', l, '.\n');
|
||||
@@ -0,0 +1,6 @@
|
||||
|
||||
dirname = '.';
|
||||
dir = files.openDir(dirname);
|
||||
|
||||
while (filename = files.nextDirItem(dir)) != none:
|
||||
print(filename, '\n');
|
||||
@@ -0,0 +1,19 @@
|
||||
# This script creates a lot of objects, so it can be used to stress the garbage collector.
|
||||
|
||||
print('Start\n');
|
||||
|
||||
i = 0;
|
||||
n = 1000;
|
||||
|
||||
while i < n: {
|
||||
|
||||
1 + 1 * 1;
|
||||
i = i + 1;
|
||||
|
||||
fun printPercent()
|
||||
print(100.0 * i / n, '%\n');
|
||||
|
||||
printPercent();
|
||||
}
|
||||
|
||||
print('End\n');
|
||||
@@ -0,0 +1,7 @@
|
||||
buff = newBuffer(1024);
|
||||
name = 'samples/bubble_sort.noja';
|
||||
hdle = files.openFile(name, files.READ);
|
||||
n = files.read(hdle, buff);
|
||||
resl = bufferToString(sliceBuffer(buff, 0, n));
|
||||
print('Read ', n, ' bytes.\n');
|
||||
print(resl);
|
||||
@@ -0,0 +1,37 @@
|
||||
# Implementation of a stack using a linked list.
|
||||
|
||||
stack = {count: 0, head: none};
|
||||
|
||||
fun push(stack, value) {
|
||||
|
||||
node = {prev: none, item: value};
|
||||
|
||||
node.prev = stack.head;
|
||||
stack.head = node;
|
||||
stack.count = stack.count + 1;
|
||||
}
|
||||
|
||||
fun pop(stack) {
|
||||
|
||||
if stack.head == none:
|
||||
return none;
|
||||
|
||||
value = stack.head.item;
|
||||
stack.head = stack.head.prev;
|
||||
stack.count = stack.count - 1;
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
push(stack, 1);
|
||||
push(stack, 2);
|
||||
push(stack, 3);
|
||||
x = pop(stack);
|
||||
y = pop(stack);
|
||||
z = pop(stack);
|
||||
w = pop(stack);
|
||||
|
||||
assert(x == 3);
|
||||
assert(y == 2);
|
||||
assert(z == 1);
|
||||
assert(w == none);
|
||||
@@ -0,0 +1,6 @@
|
||||
A = 'Hello';
|
||||
B = ', ';
|
||||
C = 'world';
|
||||
D = strcat(A, B, C);
|
||||
|
||||
print(D, '\n');
|
||||
Reference in New Issue
Block a user