Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
45
Task/Active-object/Raku/active-object.raku
Normal file
45
Task/Active-object/Raku/active-object.raku
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
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.f = &f-of-t;
|
||||
self.now = now;
|
||||
self.init;
|
||||
}
|
||||
|
||||
method Output { self.value }
|
||||
}
|
||||
|
||||
my $a = Integrator.new;
|
||||
|
||||
$a.Input( sub ($t) { sin(2 * π * .5 * $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;
|
||||
Loading…
Add table
Add a link
Reference in a new issue