1
0
Fork 0
forked from fun/fun

Added a maps usage example. (0.37.59)

This commit is contained in:
Johannes Findeisen 2026-01-19 23:40:39 +01:00
commit f073df040c
2 changed files with 63 additions and 1 deletions

View file

@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.10) cmake_minimum_required(VERSION 3.10)
project(fun VERSION 0.37.58 LANGUAGES C) project(fun VERSION 0.37.59 LANGUAGES C)
set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON) set(CMAKE_C_STANDARD_REQUIRED ON)

62
examples/maps.fun Executable file
View file

@ -0,0 +1,62 @@
#!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2026 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2026-01-19
*/
/*
* Maps datatype focused examples
*
* This script exclusively demonstrates the built-in maps datatype:
* - Creating maps with literals and empty maps
* - Reading and writing entries via [key]
* - Checking for key existence with has(map, key)
* - Getting keys(map) and values(map)
* - Using nested maps
*/
// Create an empty map and add entries
user = {}
user["name"] = "Ada"
user["age"] = 37
print(user) // -> {"name": "Ada", "age": 37}
// Read an entry by key
print(user["name"]) // -> Ada
// Update an existing entry
user["age"] = 38
print(user["age"]) // -> 38
// Check if a key exists
print(has(user, "age")) // -> 1 (true)
print(has(user, "email")) // -> 0 (false)
// Keys and values (order may vary)
print(keys(user)) // -> [name, age]
print(values(user)) // -> ["Ada", 38]
// Nested maps
address = { "city": "London", "zip": "E1" }
user["address"] = address
print(user["address"]) // -> {"city": "London", "zip": "E1"}
print(user["address"]["city"]) // -> London
/* Expected output:
{"name": Ada, "age": 37}
Ada
38
1
0
[name, age]
[Ada, 38]
{"zip": E1, "city": London}
London
*/