Lot of example directory structure refactoring. (0.39.9).
This commit is contained in:
parent
564999709c
commit
e240768c26
13 changed files with 20 additions and 21 deletions
57
examples/snippets/include_namespace.fun
Normal file
57
examples/snippets/include_namespace.fun
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
#!/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
|
||||
*
|
||||
* Added: 2025-09-30
|
||||
*/
|
||||
|
||||
// Demonstration of the include-as namespace feature.
|
||||
//
|
||||
// Run examples (without installing) by pointing FUN_LIB_DIR to the repo lib:
|
||||
// Linux/macOS/FreeBSD:
|
||||
// FUN_LIB_DIR="$(pwd)/lib" ./build/fun examples/snippets/include_namespace.fun
|
||||
// Windows (CMD):
|
||||
// set FUN_LIB_DIR=%CD%\lib && build-debug\fun.exe examples\snippets\include_namespace.fun
|
||||
// Windows (PowerShell):
|
||||
// $env:FUN_LIB_DIR="$PWD\lib"; .\build\fun.exe examples\snippets\include_namespace.fun
|
||||
|
||||
// Import stdlib math helpers into alias 'm'
|
||||
#include <utils/math.fun> as m
|
||||
|
||||
// Import a local module (this repository file) into alias 'mod'
|
||||
#include "examples/namespaced_mod.fun" as mod
|
||||
|
||||
print("=== include-as namespace demo ===")
|
||||
|
||||
print("Using m.add and m.times from <utils/math.fun>:")
|
||||
print("m.add(2, 3) = " + to_string(m.add(2, 3)))
|
||||
print("m.times(4, 5) = " + to_string(m.times(4, 5)))
|
||||
|
||||
print("")
|
||||
print("Using mod.hello and mod.Greeter from namespaced_mod.fun:")
|
||||
print(mod.hello("Fun"))
|
||||
|
||||
g = mod.Greeter("Hi")
|
||||
g.say("World")
|
||||
|
||||
print("")
|
||||
print("=== done ===")
|
||||
|
||||
/* Expected output:
|
||||
=== include-as namespace demo ===
|
||||
Using m.add and m.times from <utils/math.fun>:
|
||||
m.add(2, 3) = 5
|
||||
m.times(4, 5) = 20
|
||||
|
||||
Using mod.hello and mod.Greeter from namespaced_mod.fun:
|
||||
Hello, Fun!
|
||||
Hi World
|
||||
|
||||
=== done ===
|
||||
*/
|
||||
54
examples/snippets/maps_iterate.fun
Normal file
54
examples/snippets/maps_iterate.fun
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
#!/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
|
||||
*/
|
||||
|
||||
/*
|
||||
* Iterating through maps in Fun
|
||||
*
|
||||
* This example shows common patterns to iterate over a map:
|
||||
* - for k in keys(m): access value via m[k]
|
||||
* - for v in values(m): iterate values directly
|
||||
* - Notes about non-deterministic key order unless you define one
|
||||
*/
|
||||
|
||||
// Define a sample map
|
||||
user = { "name": "Ada", "age": 37, "city": "London" }
|
||||
|
||||
print("-- Iterate keys, then index map --")
|
||||
for k in keys(user)
|
||||
print(k + ": " + to_string(user[k]))
|
||||
|
||||
print("-- Iterate values only --")
|
||||
for v in values(user)
|
||||
print(v)
|
||||
|
||||
// If you need a specific order, provide it explicitly
|
||||
order = ["name", "city", "age"]
|
||||
print("-- Deterministic order by explicit key list --")
|
||||
for k in order
|
||||
if has(user, k)
|
||||
print(k + ": " + to_string(user[k]))
|
||||
|
||||
/* Expected output (key order in the first two blocks may vary):
|
||||
-- Iterate keys, then index map --
|
||||
name: Ada
|
||||
age: 37
|
||||
city: London
|
||||
-- Iterate values only --
|
||||
Ada
|
||||
37
|
||||
London
|
||||
-- Deterministic order by explicit key list --
|
||||
name: Ada
|
||||
city: London
|
||||
age: 37
|
||||
*/
|
||||
Loading…
Add table
Add a link
Reference in a new issue