Active object: Difference between revisions

Content added Content deleted
(→‎{{header|Perl 6}}: Add a Perl 6 example)
Line 1,631: Line 1,631:


$x->delete;</lang>
$x->delete;</lang>

=={{header|Perl 6}}==
{{works with|Rakudo|2018.12}}
There is some jitter in the timer, but it is typically accurate to within a few thousandths of a second.

<lang perl6>class Integrator {
has $.f is rw = sub ($t) { 0 };
has $.now is rw;
has $.value is rw = 0;
has $.integrator is rw;

method init() {
self.value = &(self.f)(0);
self.integrator = Thread.new(
:code({
loop {
my $t1 = now;
self.value += (&(self.f)(self.now) + &(self.f)($t1)) * ($t1 - self.now) / 2;
self.now = $t1;
sleep .001;
}
}),
:app_lifetime(True)
).run
}

method Input (&f-of-t) {
self.init;
self.f = &f-of-t;
self.now = now;
}

method Output { self.value }
}

my $a = Integrator.new;

$a.Input( sub ($t) {sin(pi * $t) } );

say "Initial value: ", $a.Output;

sleep 2;

say "After 2 seconds: ", $a.Output;

$a.Input( sub ($t) { 0 } );

sleep .5;

say "f(0): ", $a.Output;</lang>

{{out|Typical output}}
<pre>Initial value: 0
After 2 seconds: -0.0005555887464620366
f(0): 0</pre>


=={{header|OxygenBasic}}==
=={{header|OxygenBasic}}==