But I was thinking for people coming to Harbour from other languages, it would be nice to have this natively.
Adding features to the language will also confirm the language is not stagnant or dead.
1. The "yield" command from Python, which would make it easier to make generators.
2. Named parameters with default values for functions.
3. Native SQL support on in-memory tables.
Use cases:
Store unique values:
The primary purpose of a Set is to ensure that all values are unique. This is useful for scenarios like:
- Removing duplicates from an array: Convert an array to a Set, then convert it back to an array to eliminate duplicates.
- Tracking unique visitors to a website: Store each visitor's ID in a Set to easily count the number of unique visitors.
- Implementing a tag system: Store tags in a Set to prevent users from adding the same tag multiple times.
Perform fast membership checks:
Sets
use a hash table internally, which makes checking if an element exists
in a Set much faster than checking if it exists in an array, especially
for large datasets. This is beneficial for:
- Caching: Quickly check if a value is already cached.
- Filtering: Efficiently filter a collection of items based on whether they exist in a Set.
Perform mathematical set operations:
JavaScript Sets have built-in methods for performing set operations like:
- Union: Combine two Sets to create a new Set containing all the unique elements from both.
- Intersection: Find the common elements between two Sets.
- Difference: Find the elements that are in one Set but not the other.
In JavaScript:
1. Removing Duplicates from an Array
const array = [1, 2, 2, 3, 4, 4, 5];
const uniqueArray = [...new Set(array)];
console.log(uniqueArray); // [1, 2, 3, 4, 5]
2.Checking for Uniqueness
const set = new Set();
set.add(1);
set.add(2);
set.add(2); // Duplicate won't be added
console.log(set); // Set { 1, 2 }
3. Fast Lookups
const set = new Set([1, 2, 3, 4, 5]);
console.log(set.has(3)); // true
console.log(set.has(6)); // false
4. Performing Set Operations (Union, Intersection, Difference)
const set1 = new Set([1, 2, 3, 4]);
const set2 = new Set([3, 4, 5, 6]);
// Union
const union = new Set([...set1, ...set2]);
// Intersection
const intersection = new Set([...set1].filter(x => set2.has(x)));
// Difference
const difference = new Set([...set1].filter(x => !set2.has(x)));
console.log(union); // Set { 1, 2, 3, 4, 5, 6 }
console.log(intersection); // Set { 3, 4 }
console.log(difference); // Set { 1, 2 }
5. Tracking State or History
const visitedPages = new Set();
visitedPages.add('home');
visitedPages.add('about');
visitedPages.add('home'); // Will not be added again
console.log(visitedPages); // Set { 'home', 'about' }
6. Efficient Filtering and Set Operations
const array1 = [1, 2, 3, 4, 5];
const array2 = [4, 5, 6, 7, 8];
const uniqueValues = new Set([...array1, ...array2]);
console.log([...uniqueValues]); // [1, 2, 3, 4, 5, 6, 7, 8]
Summary
In general, Sets are perfect for use cases where:
- You need to store unique values.
- You want fast membership checking or lookups.
- You're performing set-based operations like unions, intersections, and differences.
- You want to remove duplicates from arrays or other data structures.