Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #34 Create a set of objects

Declare and initialize a set x containing objects of type T.

#include <unordered_set>
std::unordered_set<T, hasher, eq> x;
#include <unordered_set>
std::unordered_set<T> x;
using System.Collections.Generic;
HashSet<T> x = new HashSet<T>();
import std.container: redBlackTree;
auto x = redBlackTree!T;
var x = new Set<T>();
x := make(map[T]bool)
x := make(map[T]struct{})
import Data.Set
x = empty :: Set T
let x = new Set();
import java.util.Set;
import java.util.HashSet;
Set<T> x = new HashSet<T>();
import java.util.Set;
import java.util.HashSet;
Set<T> x = new HashSet<T>();
x.add(a);
x.add(b);
use Moops;
use Set::Object qw();
class T {}
class Set::Object::T extends Set::Object {
    method BUILDARGS(T @items) { return {data => \@items}; }
    method insert(T @items) { $self->next::method(@items); }
}
my $x = Set::Object::T->new(T->new, T->new, T->new);
class T(object):
    pass

x = set(T())
require 'set'
x = Set.new
use std::collections::HashSet;
let x: HashSet<T> = HashSet::new();
val x = Set[T]()

Do you know the best way to do this in your language ?
New implementation...

Idiom created by

programming-idioms.org

Related idioms