Set of real numbers: Difference between revisions

(→‎{{header|Ruby}}: bug fix, change display format.)
Line 1,745:
40 contiguous subsets, total length 2.075864841184667
</pre>
 
=={{header|zkl}}==
{{trans|D}}
No ∞
<lang zkl>class RealSet{
fcn init(fx){ var [const] contains=fx; }
fcn holds(x){ contains(x) }
fcn __opAdd(rs){ RealSet('wrap(x){ contains(x) or rs.contains(x) }) }
fcn __opSub(rs){ RealSet('wrap(x){ contains(x) and not rs.contains(x) }) }
fcn intersection(rs) { RealSet('wrap(x){ contains(x) and rs.contains(x) }) }
}</lang>
The python method could used but the zkl compiler is slow when used in code to generate code.
 
The method used is a bit inefficient because it closes the contains function of the other set so you can build quite a long call chain as you create new sets.
<lang zkl>tester := TheVault.Test.UnitTester.UnitTester();
 
// test union
s:=RealSet(fcn(x){ 0.0 < x <= 1.0 }) +
RealSet(fcn(x){ 0.0 <= x < 1.0 });
tester.testRun(s.holds(0.0),Void,True,__LINE__);
tester.testRun(s.holds(1.0),Void,True,__LINE__);
tester.testRun(s.holds(2.0),Void,False,__LINE__);
 
// test difference
s1 := RealSet(fcn(x){ 0.0 <= x < 3.0 }) -
RealSet(fcn(x){ 0.0 < x < 1.0 });
tester.testRun(s1.holds(0.0),Void,True,__LINE__);
tester.testRun(s1.holds(0.5),Void,False,__LINE__);
tester.testRun(s1.holds(1.0),Void,True,__LINE__);
tester.testRun(s1.holds(2.0),Void,True,__LINE__);
 
s2 := RealSet(fcn(x){ 0.0 <= x < 3.0 }) -
RealSet(fcn(x){ 0.0 <= x <= 1.0 });
tester.testRun(s2.holds(0.0),Void,False,__LINE__);
tester.testRun(s2.holds(1.0),Void,False,__LINE__);
tester.testRun(s2.holds(2.0),Void,True,__LINE__);
 
// test intersection
s := RealSet(fcn(x){ 0.0 <= x < 2.0 }).intersection(
RealSet(fcn(x){ 1.0 < x <= 2.0 }));
tester.testRun(s.holds(0.0),Void,False,__LINE__);
tester.testRun(s.holds(1.0),Void,False,__LINE__);
tester.testRun(s.holds(2.0),Void,False,__LINE__);</lang>
{{out}}
<pre>
$ zkl bbb
===================== Unit Test 1 =====================
Test 1 passed!
===================== Unit Test 2 =====================
Test 2 passed!
...
===================== Unit Test 12 =====================
Test 12 passed!
===================== Unit Test 13 =====================
Test 13 passed!
</pre>
 
 
{{omit from|Lilypond}}
Anonymous user