From 6372160c5b3a4415e4a5f9c516d02dcf465302c8 Mon Sep 17 00:00:00 2001 From: Florian Loitsch Date: Thu, 30 Jan 2025 15:08:41 +0100 Subject: [PATCH] Add Map.map examples. --- lib/core/collections.toit | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/core/collections.toit b/lib/core/collections.toit index 2ec84c663..e7f243e2a 100644 --- a/lib/core/collections.toit +++ b/lib/core/collections.toit @@ -3200,6 +3200,19 @@ class Map extends HashedInsertionOrderedCollection_: The $block is invoked with two arguments for each entry in this instance: the key and the value. The returned value becomes the new value for the key. + + # Examples + ``` + map := { "a": 1, "b": 2 } + + // Double the values. (Key is not used). + doubled := map.map: | _ value | value * 2 + print doubled // => { "a": 2, "b": 4 } + + // Prefix the values with the key. + prefixed := map.map: | key value | "$key-$value" + print prefixed // => { "a": "a-1", "b": "b-2" } + ``` */ map [block] -> Map: result := Map