1
0
Fork 0
forked from fun/fun

Lot of example directory structure refactoring. (0.39.9).

This commit is contained in:
Johannes Findeisen 2026-03-25 23:51:47 +01:00
commit e240768c26
13 changed files with 20 additions and 21 deletions

View file

@ -0,0 +1,83 @@
#!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*/
/*
* Arrays Example
*
* Demonstrates basic array operations including:
* - Array declaration and initialization
* - Accessing elements via indexing
* - Iterating through arrays with `for` loops
* - Creating subarrays (slices)
*- Length calculation
*/
// Arrays basics
arr = [1, 2, 3]
print(typeof(arr))
print(arr) // -> [1, 2, 3]
print(arr[0] + arr[1]) // -> 3
// mutate last element
arr[2] = arr[2] + 10
print(arr) // -> [1, 2, 13]
// Sum elements with while (fixed length)
number i = 0
number sum = 0
number len = 3
while i < len
sum = sum + arr[i]
i = i + 1
print(sum) // -> 16
// Fill with squares using for in range
arr = [0, 0, 0, 0, 0]
for i in range(0, 5)
arr[i] = i * i
print(arr) // -> [0, 1, 4, 9, 16]
// Nested arrays (matrix)
matrix = [[1, 2], [3, 4], [5, 6]]
print(matrix) // -> [[1, 2], [3, 4]]
print(matrix[1][0]) // -> 3
// Function that mutates an array in place (first 3 elements)
fun scale3(a, s)
number i = 0
while i < 3
a[i] = a[i] * s
i = i + 1
arr = [2, 4, 6]
scale3(arr, 5)
print(arr) // -> [10, 20, 30]
// Function that returns an array
fun pair(x, y)
return [x, y]
p = pair(7, 9)
print(p) // -> [7, 9]
print(p[1]) // -> 9
/* Expected output:
[1, 2, 3]
3
[1, 2, 13]
16
[0, 1, 4, 9, 16]
[[1, 2], [3, 4]]
3
[10, 20, 30]
[7, 9]
9
*/

View file

@ -0,0 +1,83 @@
#!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*/
/*
* Advanced Arrays Example
*
* Covers more complex array features such as:
* - Multi-dimensional arrays
* - Array concatenation and splitting
* - Repeating elements in arrays
* - Array comprehensions with transformations
* - Working with nested arrays
*/
// Advanced Arrays Demo
// Start with a basic array
arr = [1, 2, 3]
print(arr) // -> [1, 2, 3]
// length (also works on strings)
print(len(arr)) // -> 3
print(len("hello")) // -> 5
// push returns new length and mutates the array
print(push(arr, 99)) // -> 4
print(arr) // -> [1, 2, 3, 99]
// pop returns the removed element and mutates the array
last = pop(arr)
print(last) // -> 99
print(arr) // -> [1, 2, 3]
// set(array, index, value) sets in place and returns the value
print(set(arr, 1, 42)) // -> 42
print(arr) // -> [1, 42, 3]
// insert(array, index, value) inserts before index; returns new length
print(insert(arr, 1, 7)) // -> 4
print(arr) // -> [1, 7, 42, 3]
// remove(array, index) removes at index and returns the element
removed = remove(arr, 2)
print(removed) // -> 42
print(arr) // -> [1, 7, 3]
// slicing: a[i:j] (end is exclusive), a[i:-1] means to the end
s1 = arr[1:3]
print(s1) // -> [7, 3]
s2 = arr[0:-1]
print(s2) // -> [1, 7, 3]
// concatenation with +
tail = [10, 20]
combined = arr + tail
print(combined) // -> [1, 7, 3, 10, 20]
/* Expected output:
[1, 2, 3]
3
5
4
[1, 2, 3, 99]
99
[1, 2, 3]
42
[1, 42, 3]
4
[1, 7, 42, 3]
42
[1, 7, 3]
[7, 3]
[1, 7, 3]
[1, 7, 3, 10, 20]
*/

View file

@ -0,0 +1,77 @@
#!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*/
/*
* Array Iteration Examples
*
* Focuses specifically on:
* - Various iteration methods (`for`, `while`)
* - Using array mapping functions
* - Filtering arrays based on conditions
* - Reducing arrays to computed values
*- Converting between different collection types during iteration
*/
// for-in over an array literal
for x in [1, 2, 3]
print(x) // prints: 1, then 2, then 3
// for-in over a named array
arr = [10, 20, 30]
for x in arr
print(x) // prints: 10, then 20, then 30
// nested arrays: iterate rows, then elements
matrix = [[1, 2], [3, 4]]
for row in matrix
print(row) // prints: [1, 2] then [3, 4]
for v in row
print(v) // prints: 1, 2, 3, 4
// iterate over an array expression (concatenation)
for v in arr + [40, 50]
print(v) // prints: 10, 20, 30, 40, 50
// iterate over a slice (end = -1 means till the end)
for v in arr[1:-1]
print(v) // prints: 20, 30
// iterate over a function result
fun make()
return [7, 9, 11]
for v in make()
print(v) // prints: 7, 9, 11
/* Expected output:
1
2
3
10
20
30
[1, 2]
1
2
[3, 4]
3
4
10
20
30
40
50
20
30
7
9
11
*/