From f073df040cad8152286a77b79ec53fa2f28503b4 Mon Sep 17 00:00:00 2001 From: hanez Date: Mon, 19 Jan 2026 23:40:39 +0100 Subject: [PATCH] Added a maps usage example. (0.37.59) --- CMakeLists.txt | 2 +- examples/maps.fun | 62 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100755 examples/maps.fun diff --git a/CMakeLists.txt b/CMakeLists.txt index 0033b44..e6bfc41 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ 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_REQUIRED ON) diff --git a/examples/maps.fun b/examples/maps.fun new file mode 100755 index 0000000..c8425a4 --- /dev/null +++ b/examples/maps.fun @@ -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 + * 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 +*/