Set: Difference between revisions

Content added Content deleted
No edit summary
m (→‎{{header|Sidef}}: updated the class name, as the Set class is now built-in)
Line 4,832: Line 4,832:
=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Perl}}
{{trans|Perl}}
<lang ruby>class Set(*set) {
<lang ruby>class MySet(*set) {


method init {
method init {
var elems = set;
var elems = set
set = Hash.new;
set = Hash()
elems.each { |e| self += e }
elems.each { |e| self += e }
}
}


method +(elem) {
method +(elem) {
set{elem} = elem;
set{elem} = elem
self;
self
}
}


method del(elem) {
method del(elem) {
set.delete(elem);
set.delete(elem)
}
}


method has(elem) {
method has(elem) {
set.has_key(elem);
set.has_key(elem)
}
}


method ∪(Set that) {
method ∪(MySet that) {
Set(set.values..., that.values...);
MySet(set.values..., that.values...)
}
}


method ∩(Set that) {
method ∩(MySet that) {
Set(set.keys.grep{ |k| k ∈ that } \
MySet(set.keys.grep{ |k| k ∈ that } \
.map { |k| set{k} }...);
.map { |k| set{k} }...)
}
}


method ∖(Set that) {
method ∖(MySet that) {
Set(set.keys.grep{|k| !(k ∈ that) } \
MySet(set.keys.grep{|k| !(k ∈ that) } \
.map {|k| set{k} }...);
.map {|k| set{k} }...)
}
}


method ^(Set that) {
method ^(MySet that) {
var d = ((self ∖ that) ∪ (that ∖ self));
var d = ((self ∖ that) ∪ (that ∖ self))
Set(d.values...);
MySet(d.values...)
}
}


method count { set.len }
method count { set.len }


method ≡(Set that) {
method ≡(MySet that) {
(self ∖ that -> count.is_zero) && (that ∖ self -> count.is_zero);
(self ∖ that -> count.is_zero) && (that ∖ self -> count.is_zero)
}
}


method values { set.values }
method values { set.values }


method ⊆(Set that) {
method ⊆(MySet that) {
that.set.keys.each { |k|
that.set.keys.each { |k|
k ∈ self || return false;
k ∈ self || return false
}
}
return true;
return true
}
}


Line 4,893: Line 4,893:


class Object {
class Object {
method ∈(Set set) {
method ∈(MySet set) {
set.has(self);
set.has(self)
}
}
}</lang>
}</lang>


Usage example:
Usage example:
<lang ruby>var x = Set(1, 2, 3);
<lang ruby>var x = MySet(1, 2, 3)
5..7 -> each { |i| x += i };
5..7 -> each { |i| x += i }


var y = Set(1, 2, 4, x);
var y = MySet(1, 2, 4, x)


say "set x is: #{x}";
say "set x is: #{x}"
say "set y is: #{y}";
say "set y is: #{y}"


[1,2,3,4,x].each { |elem|
[1,2,3,4,x].each { |elem|
say ("#{elem} is ", elem ∈ y ? '' : 'not', " in y");
say ("#{elem} is ", elem ∈ y ? '' : 'not', " in y")
}
}


var (w, z);
var (w, z)
say ("union: ", x ∪ y);
say ("union: ", x ∪ y)
say ("intersect: ", x ∩ y);
say ("intersect: ", x ∩ y)
say ("z = x ∖ y = ", z = (x ∖ y) );
say ("z = x ∖ y = ", z = (x ∖ y) )
say ("y is ", x ⊆ y ? "" : "not ", "a subset of x");
say ("y is ", x ⊆ y ? "" : "not ", "a subset of x")
say ("z is ", x ⊆ z ? "" : "not ", "a subset of x");
say ("z is ", x ⊆ z ? "" : "not ", "a subset of x")
say ("z = (x ∪ y) ∖ (x ∩ y) = ", z = ((x ∪ y) ∖ (x ∩ y)));
say ("z = (x ∪ y) ∖ (x ∩ y) = ", z = ((x ∪ y) ∖ (x ∩ y)))
say ("w = x ^ y = ", w = (x ^ y));
say ("w = x ^ y = ", w = (x ^ y))
say ("w is ", w ≡ z ? "" : "not ", "equal to z");
say ("w is ", w ≡ z ? "" : "not ", "equal to z")
say ("w is ", w ≡ x ? "" : "not ", "equal to x");</lang>
say ("w is ", w ≡ x ? "" : "not ", "equal to x")</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 4,940: Line 4,940:
w is not equal to x
w is not equal to x
</pre>
</pre>

=={{header|Simula}}==
=={{header|Simula}}==
<lang simula>SIMSET
<lang simula>SIMSET