Jump to content

Set of real numbers: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(add {{omit from}} Pascal/Free Pascal/Delphi)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 226:
 
[0, 0] is empty 0</pre>
 
=={{header|C sharp}}==
<lang csharp>using System;
 
namespace RosettaCode.SetOfRealNumbers
{
public class Set<TValue>
{
public Set(Predicate<TValue> contains)
{
Contains = contains;
}
 
public Predicate<TValue> Contains
{
get;
private set;
}
 
public Set<TValue> Union(Set<TValue> set)
{
return new Set<TValue>(value => Contains(value) || set.Contains(value));
}
 
public Set<TValue> Intersection(Set<TValue> set)
{
return new Set<TValue>(value => Contains(value) && set.Contains(value));
}
 
public Set<TValue> Difference(Set<TValue> set)
{
return new Set<TValue>(value => Contains(value) && !set.Contains(value));
}
}
}</lang>
Test:
<lang csharp>using Microsoft.VisualStudio.TestTools.UnitTesting;
using RosettaCode.SetOfRealNumbers;
 
namespace RosettaCode.SetOfRealNumbersTest
{
[TestClass]
public class SetTest
{
[TestMethod]
public void TestUnion()
{
var set =
new Set<double>(value => 0d < value && value <= 1d).Union(
new Set<double>(value => 0d <= value && value < 2d));
Assert.IsTrue(set.Contains(0d));
Assert.IsTrue(set.Contains(1d));
Assert.IsFalse(set.Contains(2d));
}
 
[TestMethod]
public void TestIntersection()
{
var set =
new Set<double>(value => 0d <= value && value < 2d).Intersection(
new Set<double>(value => 1d < value && value <= 2d));
Assert.IsFalse(set.Contains(0d));
Assert.IsFalse(set.Contains(1d));
Assert.IsFalse(set.Contains(2d));
}
 
[TestMethod]
public void TestDifference()
{
var set =
new Set<double>(value => 0d <= value && value < 3d).Difference(
new Set<double>(value => 0d < value && value < 1d));
Assert.IsTrue(set.Contains(0d));
Assert.IsTrue(set.Contains(1d));
Assert.IsTrue(set.Contains(2d));
 
set =
new Set<double>(value => 0d <= value && value < 3d).Difference(
new Set<double>(value => 0d <= value && value <= 1d));
Assert.IsFalse(set.Contains(0d));
Assert.IsFalse(set.Contains(1d));
Assert.IsTrue(set.Contains(2d));
}
}
}</lang>
 
=={{header|C++}}==
Line 382 ⟶ 467:
 
Approximate length of A - B is 2.07587</pre>
 
=={{header|C sharp}}==
<lang csharp>using System;
 
namespace RosettaCode.SetOfRealNumbers
{
public class Set<TValue>
{
public Set(Predicate<TValue> contains)
{
Contains = contains;
}
 
public Predicate<TValue> Contains
{
get;
private set;
}
 
public Set<TValue> Union(Set<TValue> set)
{
return new Set<TValue>(value => Contains(value) || set.Contains(value));
}
 
public Set<TValue> Intersection(Set<TValue> set)
{
return new Set<TValue>(value => Contains(value) && set.Contains(value));
}
 
public Set<TValue> Difference(Set<TValue> set)
{
return new Set<TValue>(value => Contains(value) && !set.Contains(value));
}
}
}</lang>
Test:
<lang csharp>using Microsoft.VisualStudio.TestTools.UnitTesting;
using RosettaCode.SetOfRealNumbers;
 
namespace RosettaCode.SetOfRealNumbersTest
{
[TestClass]
public class SetTest
{
[TestMethod]
public void TestUnion()
{
var set =
new Set<double>(value => 0d < value && value <= 1d).Union(
new Set<double>(value => 0d <= value && value < 2d));
Assert.IsTrue(set.Contains(0d));
Assert.IsTrue(set.Contains(1d));
Assert.IsFalse(set.Contains(2d));
}
 
[TestMethod]
public void TestIntersection()
{
var set =
new Set<double>(value => 0d <= value && value < 2d).Intersection(
new Set<double>(value => 1d < value && value <= 2d));
Assert.IsFalse(set.Contains(0d));
Assert.IsFalse(set.Contains(1d));
Assert.IsFalse(set.Contains(2d));
}
 
[TestMethod]
public void TestDifference()
{
var set =
new Set<double>(value => 0d <= value && value < 3d).Difference(
new Set<double>(value => 0d < value && value < 1d));
Assert.IsTrue(set.Contains(0d));
Assert.IsTrue(set.Contains(1d));
Assert.IsTrue(set.Contains(2d));
 
set =
new Set<double>(value => 0d <= value && value < 3d).Difference(
new Set<double>(value => 0d <= value && value <= 1d));
Assert.IsFalse(set.Contains(0d));
Assert.IsFalse(set.Contains(1d));
Assert.IsTrue(set.Contains(2d));
}
}
}</lang>
 
=={{header|Clojure}}==
Line 2,013:
not empty</lang>
 
=={{header|Perl 6Phix}}==
{{trans|Go}}
<lang Phix>enum ID,ARGS
function cf(sequence f, atom x) return call_func(f[ID],f[ARGS]&x) end function
function Union(sequence a, b, atom x) return cf(a,x) or cf(b,x) end function constant r_Union = routine_id("Union")
function Inter(sequence a, b, atom x) return cf(a,x) and cf(b,x) end function constant r_Inter = routine_id("Inter")
function Diffr(sequence a, b, atom x) return cf(a,x) and not cf(b,x) end function constant r_Diffr = routine_id("Diffr")
function OpOp(atom a, b, x) return a < x and x < b end function constant r_OpOp = routine_id("OpOp")
function ClCl(atom a, b, x) return a <= x and x <= b end function constant r_ClCl = routine_id("ClCl")
function OpCl(atom a, b, x) return a < x and x <= b end function constant r_OpCl = routine_id("OpCl")
function ClOp(atom a, b, x) return a <= x and x < b end function constant r_ClOp = routine_id("ClOp")
-- expected
-- ---- desc ----, 0 1 2, --------------- set method ---------------
constant s = {{"(0,1] u [0,2)", {1,1,0}, {r_Union,{{r_OpCl,{0,1}},{r_ClOp,{0,2}}}}},
{"[0,2) n (1,2]", {0,0,0}, {r_Inter,{{r_ClOp,{0,2}},{r_OpCl,{1,2}}}}},
{"[0,3) - (0,1)", {1,1,1}, {r_Diffr,{{r_ClOp,{0,3}},{r_OpOp,{0,1}}}}},
{"[0,3) - [0,1]", {0,0,1}, {r_Diffr,{{r_ClOp,{0,3}},{r_ClCl,{0,1}}}}}},
tf = {"True","False"}
 
for i=1 to length(s) do
sequence {desc, expect, method} = s[i]
for x=0 to 2 do
bool r = cf(method,x)
string error = iff(r!=expect[x+1]?"error":"")
printf(1,"%d in %s : %s %s\n", {x, desc, tf[2-r],error})
end for
printf(1,"\n")
end for</lang>
{{out}}
<pre>
0 in (0,1] u [0,2) : True
1 in (0,1] u [0,2) : True
2 in (0,1] u [0,2) : False
 
0 in [0,2) n (1,2] : False
1 in [0,2) n (1,2] : False
2 in [0,2) n (1,2] : False
 
0 in [0,3) - (0,1) : True
1 in [0,3) - (0,1) : True
2 in [0,3) - (0,1) : True
 
0 in [0,3) - [0,1] : False
1 in [0,3) - [0,1] : False
2 in [0,3) - [0,1] : True
</pre>
Extra credit - also translated from Go, but with an extended loop and crude summation, inspired by Java/Kotlin.
<lang Phix>function aspxx(atom x) return abs(sin(PI*x*x))>0.5 end function constant r_aspxx = routine_id("aspxx")
function aspx(atom x) return abs(sin(PI*x))>0.5 end function constant r_aspx = routine_id("aspx")
 
constant A = {r_Inter,{{r_OpOp,{0,10}},{r_aspxx,{}}}},
B = {r_Inter,{{r_OpOp,{0,10}},{r_aspx,{}}}},
C = {r_Diffr,{A,B}}
atom x = 0, step = 0.00001, count = 0
while x<=10 do
count += cf(C,x)
x += step
end while
printf(1,"Approximate length of A-B: %.5f\n",{count*step})</lang>
{{out}}
<pre>
Approximate length of A-B: 2.07587
</pre>
 
=={{header|Python}}==
<lang python>class Setr():
def __init__(self, lo, hi, includelo=True, includehi=False):
self.eqn = "(%i<%sX<%s%i)" % (lo,
'=' if includelo else '',
'=' if includehi else '',
hi)
 
def __contains__(self, X):
return eval(self.eqn, locals())
 
# union
def __or__(self, b):
ans = Setr(0,0)
ans.eqn = "(%sor%s)" % (self.eqn, b.eqn)
return ans
 
# intersection
def __and__(self, b):
ans = Setr(0,0)
ans.eqn = "(%sand%s)" % (self.eqn, b.eqn)
return ans
 
# difference
def __sub__(self, b):
ans = Setr(0,0)
ans.eqn = "(%sand not%s)" % (self.eqn, b.eqn)
return ans
 
def __repr__(self):
return "Setr%s" % self.eqn
 
 
sets = [
Setr(0,1, 0,1) | Setr(0,2, 1,0),
Setr(0,2, 1,0) & Setr(1,2, 0,1),
Setr(0,3, 1,0) - Setr(0,1, 0,0),
Setr(0,3, 1,0) - Setr(0,1, 1,1),
]
settexts = '(0, 1] ∪ [0, 2);[0, 2) ∩ (1, 2];[0, 3) − (0, 1);[0, 3) − [0, 1]'.split(';')
 
for s,t in zip(sets, settexts):
print("Set %s %s. %s" % (t,
', '.join("%scludes %i"
% ('in' if v in s else 'ex', v)
for v in range(3)),
s.eqn))</lang>
 
;Output:
<pre>Set (0, 1] ∪ [0, 2) includes 0, includes 1, excludes 2. ((0<X<=1)or(0<=X<2))
Set [0, 2) ∩ (1, 2] excludes 0, excludes 1, excludes 2. ((0<=X<2)and(1<X<=2))
Set [0, 3) − (0, 1) includes 0, includes 1, includes 2. ((0<=X<3)and not(0<X<1))
Set [0, 3) − [0, 1] excludes 0, excludes 1, includes 2. ((0<=X<3)and not(0<=X<=1))
</pre>
 
=={{header|Racket}}==
This is a simple representation of sets as functions (so obviously no good way to the the extra set length).
<lang Racket>
#lang racket
 
;; Use a macro to allow infix operators
(require (only-in racket [#%app #%%app]))
(define-for-syntax infixes '())
(define-syntax (definfix stx)
(syntax-case stx ()
[(_ (x . xs) body ...) #'(definfix x (λ xs body ...))]
[(_ x body) (begin (set! infixes (cons #'x infixes)) #'(define x body))]))
(define-syntax (#%app stx)
(syntax-case stx ()
[(_ X op Y)
(and (identifier? #'op) (ormap (λ(o) (free-identifier=? #'op o)) infixes))
#'(#%%app op X Y)]
[(_ f x ...) #'(#%%app f x ...)]))
 
 
;; Ranges: (X +-+ Y) => [X,Y]; (X --- Y) => (X,Y); and same for `+--' and `--+'
;; Simple implementation as functions
 
;; Constructors
(definfix ((+-+ X Y) n) (<= X n Y)) ; [X,Y]
(definfix ((--- X Y) n) (< X n Y)) ; (X,Y)
(definfix ((+-- X Y) n) (and (<= X n) (< n Y))) ; [X,Y)
(definfix ((--+ X Y) n) (and (< X n) (<= n Y))) ; (X,Y]
(definfix ((== X) n) (= X n)) ; [X,X]
;; Set operations
(definfix ((∪ . Rs) n) (ormap (λ(p) (p n)) Rs))
(definfix ((∩ . Rs) n) (andmap (λ(p) (p n)) Rs))
(definfix ((∖ R1 R2) n) (and (R1 n) (not (R2 n)))) ; set-minus, not backslash
(define ((¬ R) n) (not (R n)))
;; Special sets
(define (∅ n) #f)
(define (ℜ n) #t)
 
(define-syntax-rule (try set)
(apply printf "~a => ~a ~a ~a\n" (~s #:width 23 'set)
(let ([pred set]) (for/list ([i 3]) (if (pred i) 'Y 'N)))))
(try ((0 --+ 1) ∪ (0 +-- 2)))
(try ((0 +-- 2) ∩ (1 --+ 2)))
(try ((0 +-- 3) ∖ (0 --- 1)))
(try ((0 +-- 3) ∖ (0 +-+ 1)))
</lang>
 
Output:
<pre>
((0 --+ 1) ∪ (0 +-- 2)) => Y Y N
((0 +-- 2) ∩ (1 --+ 2)) => N N N
((0 +-- 3) ∖ (0 --- 1)) => Y Y Y
((0 +-- 3) ∖ (0 +-+ 1)) => N N Y
</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2018.10}}
<lang perl6>class Iv {
Line 2,215 ⟶ 2,391:
(9.9582461641931,9.99166319154791)
Length A − B = 2.07586484118467</pre>
 
=={{header|Phix}}==
{{trans|Go}}
<lang Phix>enum ID,ARGS
function cf(sequence f, atom x) return call_func(f[ID],f[ARGS]&x) end function
function Union(sequence a, b, atom x) return cf(a,x) or cf(b,x) end function constant r_Union = routine_id("Union")
function Inter(sequence a, b, atom x) return cf(a,x) and cf(b,x) end function constant r_Inter = routine_id("Inter")
function Diffr(sequence a, b, atom x) return cf(a,x) and not cf(b,x) end function constant r_Diffr = routine_id("Diffr")
function OpOp(atom a, b, x) return a < x and x < b end function constant r_OpOp = routine_id("OpOp")
function ClCl(atom a, b, x) return a <= x and x <= b end function constant r_ClCl = routine_id("ClCl")
function OpCl(atom a, b, x) return a < x and x <= b end function constant r_OpCl = routine_id("OpCl")
function ClOp(atom a, b, x) return a <= x and x < b end function constant r_ClOp = routine_id("ClOp")
-- expected
-- ---- desc ----, 0 1 2, --------------- set method ---------------
constant s = {{"(0,1] u [0,2)", {1,1,0}, {r_Union,{{r_OpCl,{0,1}},{r_ClOp,{0,2}}}}},
{"[0,2) n (1,2]", {0,0,0}, {r_Inter,{{r_ClOp,{0,2}},{r_OpCl,{1,2}}}}},
{"[0,3) - (0,1)", {1,1,1}, {r_Diffr,{{r_ClOp,{0,3}},{r_OpOp,{0,1}}}}},
{"[0,3) - [0,1]", {0,0,1}, {r_Diffr,{{r_ClOp,{0,3}},{r_ClCl,{0,1}}}}}},
tf = {"True","False"}
 
for i=1 to length(s) do
sequence {desc, expect, method} = s[i]
for x=0 to 2 do
bool r = cf(method,x)
string error = iff(r!=expect[x+1]?"error":"")
printf(1,"%d in %s : %s %s\n", {x, desc, tf[2-r],error})
end for
printf(1,"\n")
end for</lang>
{{out}}
<pre>
0 in (0,1] u [0,2) : True
1 in (0,1] u [0,2) : True
2 in (0,1] u [0,2) : False
 
0 in [0,2) n (1,2] : False
1 in [0,2) n (1,2] : False
2 in [0,2) n (1,2] : False
 
0 in [0,3) - (0,1) : True
1 in [0,3) - (0,1) : True
2 in [0,3) - (0,1) : True
 
0 in [0,3) - [0,1] : False
1 in [0,3) - [0,1] : False
2 in [0,3) - [0,1] : True
</pre>
Extra credit - also translated from Go, but with an extended loop and crude summation, inspired by Java/Kotlin.
<lang Phix>function aspxx(atom x) return abs(sin(PI*x*x))>0.5 end function constant r_aspxx = routine_id("aspxx")
function aspx(atom x) return abs(sin(PI*x))>0.5 end function constant r_aspx = routine_id("aspx")
 
constant A = {r_Inter,{{r_OpOp,{0,10}},{r_aspxx,{}}}},
B = {r_Inter,{{r_OpOp,{0,10}},{r_aspx,{}}}},
C = {r_Diffr,{A,B}}
atom x = 0, step = 0.00001, count = 0
while x<=10 do
count += cf(C,x)
x += step
end while
printf(1,"Approximate length of A-B: %.5f\n",{count*step})</lang>
{{out}}
<pre>
Approximate length of A-B: 2.07587
</pre>
 
=={{header|Python}}==
<lang python>class Setr():
def __init__(self, lo, hi, includelo=True, includehi=False):
self.eqn = "(%i<%sX<%s%i)" % (lo,
'=' if includelo else '',
'=' if includehi else '',
hi)
 
def __contains__(self, X):
return eval(self.eqn, locals())
 
# union
def __or__(self, b):
ans = Setr(0,0)
ans.eqn = "(%sor%s)" % (self.eqn, b.eqn)
return ans
 
# intersection
def __and__(self, b):
ans = Setr(0,0)
ans.eqn = "(%sand%s)" % (self.eqn, b.eqn)
return ans
 
# difference
def __sub__(self, b):
ans = Setr(0,0)
ans.eqn = "(%sand not%s)" % (self.eqn, b.eqn)
return ans
 
def __repr__(self):
return "Setr%s" % self.eqn
 
 
sets = [
Setr(0,1, 0,1) | Setr(0,2, 1,0),
Setr(0,2, 1,0) & Setr(1,2, 0,1),
Setr(0,3, 1,0) - Setr(0,1, 0,0),
Setr(0,3, 1,0) - Setr(0,1, 1,1),
]
settexts = '(0, 1] ∪ [0, 2);[0, 2) ∩ (1, 2];[0, 3) − (0, 1);[0, 3) − [0, 1]'.split(';')
 
for s,t in zip(sets, settexts):
print("Set %s %s. %s" % (t,
', '.join("%scludes %i"
% ('in' if v in s else 'ex', v)
for v in range(3)),
s.eqn))</lang>
 
;Output:
<pre>Set (0, 1] ∪ [0, 2) includes 0, includes 1, excludes 2. ((0<X<=1)or(0<=X<2))
Set [0, 2) ∩ (1, 2] excludes 0, excludes 1, excludes 2. ((0<=X<2)and(1<X<=2))
Set [0, 3) − (0, 1) includes 0, includes 1, includes 2. ((0<=X<3)and not(0<X<1))
Set [0, 3) − [0, 1] excludes 0, excludes 1, includes 2. ((0<=X<3)and not(0<=X<=1))
</pre>
 
=={{header|Racket}}==
This is a simple representation of sets as functions (so obviously no good way to the the extra set length).
<lang Racket>
#lang racket
 
;; Use a macro to allow infix operators
(require (only-in racket [#%app #%%app]))
(define-for-syntax infixes '())
(define-syntax (definfix stx)
(syntax-case stx ()
[(_ (x . xs) body ...) #'(definfix x (λ xs body ...))]
[(_ x body) (begin (set! infixes (cons #'x infixes)) #'(define x body))]))
(define-syntax (#%app stx)
(syntax-case stx ()
[(_ X op Y)
(and (identifier? #'op) (ormap (λ(o) (free-identifier=? #'op o)) infixes))
#'(#%%app op X Y)]
[(_ f x ...) #'(#%%app f x ...)]))
 
 
;; Ranges: (X +-+ Y) => [X,Y]; (X --- Y) => (X,Y); and same for `+--' and `--+'
;; Simple implementation as functions
 
;; Constructors
(definfix ((+-+ X Y) n) (<= X n Y)) ; [X,Y]
(definfix ((--- X Y) n) (< X n Y)) ; (X,Y)
(definfix ((+-- X Y) n) (and (<= X n) (< n Y))) ; [X,Y)
(definfix ((--+ X Y) n) (and (< X n) (<= n Y))) ; (X,Y]
(definfix ((== X) n) (= X n)) ; [X,X]
;; Set operations
(definfix ((∪ . Rs) n) (ormap (λ(p) (p n)) Rs))
(definfix ((∩ . Rs) n) (andmap (λ(p) (p n)) Rs))
(definfix ((∖ R1 R2) n) (and (R1 n) (not (R2 n)))) ; set-minus, not backslash
(define ((¬ R) n) (not (R n)))
;; Special sets
(define (∅ n) #f)
(define (ℜ n) #t)
 
(define-syntax-rule (try set)
(apply printf "~a => ~a ~a ~a\n" (~s #:width 23 'set)
(let ([pred set]) (for/list ([i 3]) (if (pred i) 'Y 'N)))))
(try ((0 --+ 1) ∪ (0 +-- 2)))
(try ((0 +-- 2) ∩ (1 --+ 2)))
(try ((0 +-- 3) ∖ (0 --- 1)))
(try ((0 +-- 3) ∖ (0 +-+ 1)))
</lang>
 
Output:
<pre>
((0 --+ 1) ∪ (0 +-- 2)) => Y Y N
((0 +-- 2) ∩ (1 --+ 2)) => N N N
((0 +-- 3) ∖ (0 --- 1)) => Y Y Y
((0 +-- 3) ∖ (0 +-+ 1)) => N N Y
</pre>
 
=={{header|REXX}}==
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.