Added lot more example code. (0.39.8).
This commit is contained in:
parent
3b70d6c92a
commit
564999709c
32 changed files with 1353 additions and 53 deletions
26
examples/algos/deduplicate.fun
Executable file
26
examples/algos/deduplicate.fun
Executable file
|
|
@ -0,0 +1,26 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2026 Johannes Findeisen
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2026-03-25
|
||||
*/
|
||||
|
||||
// De-duplicate an array using a map as a set
|
||||
arr = [1,2,2,3,1,4]
|
||||
seen = {}
|
||||
out = []
|
||||
for x in arr
|
||||
if (!has(seen, to_string(x)))
|
||||
seen[to_string(x)] = 1
|
||||
push(out, x)
|
||||
print(to_string(out))
|
||||
|
||||
/* Expected output:
|
||||
[array n=4]
|
||||
*/
|
||||
60
examples/algos/sort_and_search.fun
Executable file
60
examples/algos/sort_and_search.fun
Executable file
|
|
@ -0,0 +1,60 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2026 Johannes Findeisen
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2026-03-25
|
||||
*/
|
||||
|
||||
// Selection sort + linear/binary search
|
||||
|
||||
fun sel_sort(a)
|
||||
n = len(a)
|
||||
i = 0
|
||||
while (i < n)
|
||||
m = i
|
||||
j = i + 1
|
||||
while (j < n)
|
||||
if (a[j] < a[m]) m = j
|
||||
j = j + 1
|
||||
tmp = a[i]
|
||||
a[i] = a[m]
|
||||
a[m] = tmp
|
||||
i = i + 1
|
||||
return a
|
||||
|
||||
fun lin_find(a, x)
|
||||
i = 0
|
||||
while (i < len(a))
|
||||
if (a[i] == x) return i
|
||||
i = i + 1
|
||||
return -1
|
||||
|
||||
fun bin_find(a, x)
|
||||
lo = 0
|
||||
hi = len(a) - 1
|
||||
while (lo <= hi)
|
||||
mid = (lo + hi) / 2
|
||||
v = a[mid]
|
||||
if (v == x) return mid
|
||||
if (v < x)
|
||||
lo = mid + 1
|
||||
else
|
||||
hi = mid - 1
|
||||
return -1
|
||||
|
||||
arr = [5,1,4,2,3]
|
||||
print("sorted: " + to_string(sel_sort(arr)))
|
||||
print("lin_find 4: " + to_string(lin_find(arr, 4)))
|
||||
print("bin_find 4: " + to_string(bin_find(arr, 4)))
|
||||
|
||||
/* Expected output:
|
||||
sorted: [array n=5]
|
||||
lin_find 4: 2
|
||||
bin_find 4: 2
|
||||
*/
|
||||
44
examples/algos/stack_queue.fun
Executable file
44
examples/algos/stack_queue.fun
Executable file
|
|
@ -0,0 +1,44 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2026 Johannes Findeisen
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2026-03-25
|
||||
*/
|
||||
|
||||
// Array helpers
|
||||
#include <arrays.fun>
|
||||
|
||||
// Stack via array push/pop
|
||||
stack = []
|
||||
push(stack, 1)
|
||||
push(stack, 2)
|
||||
push(stack, 3)
|
||||
print("stack pop: " + to_string(stack[len(stack)-1]))
|
||||
pop = stack[len(stack)-1]
|
||||
// Use array_slice(arr, start, count) to drop the last element
|
||||
stack = array_slice(stack, 0, len(stack)-1)
|
||||
print("stack now: " + to_string(stack))
|
||||
|
||||
// Queue via array push/shift
|
||||
queue = []
|
||||
push(queue, "a")
|
||||
push(queue, "b")
|
||||
push(queue, "c")
|
||||
head = queue[0]
|
||||
// Keep all but the first element
|
||||
queue = array_slice(queue, 1, len(queue)-1)
|
||||
print("queue head: " + to_string(head))
|
||||
print("queue now: " + to_string(queue))
|
||||
|
||||
/* Expected output:
|
||||
stack pop: 3
|
||||
stack now: [array n=2]
|
||||
queue head: a
|
||||
queue now: [array n=2]
|
||||
*/
|
||||
Loading…
Add table
Add a link
Reference in a new issue