Collections: Set & Map

BanglaCode supports ES6-style collections: Set for unique values and Map for key-value pairs with flexible keys.

Quick Start

// Set - unique values
dhoro mySet = set_srishti([1, 2, 3, 2, 1]);
dekho(set_akar(mySet)); // 3

// Map - key-value pairs
dhoro myMap = map_srishti();
map_set(myMap, "name", "Ankan");
map_set(myMap, [1, 2], "array key");
dekho(map_get(myMap, "name")); // "Ankan"

Set API

  • set_srishti(initialArray?) - Create a new Set.
  • set_add(set, value) - Add unique value.
  • set_has(set, value) - Membership check.
  • set_delete(set, value) - Remove value.
  • set_clear(set) - Remove all values.
  • set_akar(set) - Size of set.
  • set_values(set) - Convert set to array.
  • set_foreach(set, callback) - Iterate values.

Map API

  • map_srishti(entries?) - Create a new Map.
  • map_set(map, key, value) - Insert/update entry.
  • map_get(map, key) - Get value by key.
  • map_has(map, key) - Check key existence.
  • map_delete(map, key) - Delete key.
  • map_clear(map) - Clear all entries.
  • map_akar(map) - Number of entries.
  • map_keys(map), map_values(map), map_entries(map).
  • map_foreach(map, callback) - Iterate entries.

Example: De-duplicate + Count

dhoro words = ["a", "b", "a", "c", "b", "a"];

// Unique words
dhoro uniqueWords = set_values(set_srishti(words));
dekho(uniqueWords); // ["a", "b", "c"]

// Frequency count
dhoro freq = map_srishti();
ghuriye (dhoro i = 0; i < dorghyo(words); i = i + 1) {
  dhoro w = words[i];
  dhoro n = map_get(freq, w);
  jodi (n == khali) {
    map_set(freq, w, 1);
  } nahole {
    map_set(freq, w, n + 1);
  }
}

map_foreach(freq, kaj(value, key) {
  dekho(key, "=>", value);
});