Active object: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 1: Line 1:
[[Category:Object oriented]]
{{task|Concurrency}}
{{task|Concurrency}}
[[Category:Object oriented]]{{requires|Concurrency}}{{requires|Objects}}{{requires|Mutable State}}
{{requires|Concurrency}}{{requires|Objects}}{{requires|Mutable State}}


In [[object-oriented programming]] an object is active when its state depends on clock. Usually an active object encapsulates a [[task]] that updates the object's state. To the outer world the object looks like a normal object with methods that can be called from outside. Implementation of such methods must have a certain synchronization mechanism with the encapsulated task in order to prevent object's state corruption.
In [[object-oriented programming]] an object is active when its state depends on clock. Usually an active object encapsulates a [[task]] that updates the object's state. To the outer world the object looks like a normal object with methods that can be called from outside. Implementation of such methods must have a certain synchronization mechanism with the encapsulated task in order to prevent object's state corruption.
Line 18: Line 19:
Verify that now the object's output is approximately 0 (the sine has the period of 2s). The accuracy of the result will depend on the [[OS]] scheduler time slicing and the accuracy of the clock.
Verify that now the object's output is approximately 0 (the sine has the period of 2s). The accuracy of the result will depend on the [[OS]] scheduler time slicing and the accuracy of the clock.


{{omit from|VBScript}}


=={{header|Ada}}==
=={{header|Ada}}==
<syntaxhighlight lang=ada>with Ada.Calendar; use Ada.Calendar;
<syntaxhighlight lang="ada">with Ada.Calendar; use Ada.Calendar;
with Ada.Numerics; use Ada.Numerics;
with Ada.Numerics; use Ada.Numerics;
with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions;
with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions;
Line 93: Line 93:
=={{header|BBC BASIC}}==
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang=bbcbasic> INSTALL @lib$+"CLASSLIB"
<syntaxhighlight lang="bbcbasic"> INSTALL @lib$+"CLASSLIB"
INSTALL @lib$+"TIMERLIB"
INSTALL @lib$+"TIMERLIB"
INSTALL @lib$+"NOWAIT"
INSTALL @lib$+"NOWAIT"
Line 125: Line 125:
{{libheader|pthread}}
{{libheader|pthread}}


<syntaxhighlight lang=c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <unistd.h>
#include <unistd.h>
Line 200: Line 200:
=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
{{works with|C# 6}}
{{works with|C# 6}}
<syntaxhighlight lang=csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Threading.Tasks;
using System.Threading.Tasks;


Line 284: Line 284:
=={{header|C++}}==
=={{header|C++}}==
{{works with|C++14|}}
{{works with|C++14|}}
<syntaxhighlight lang=cpp>#include <atomic>
<syntaxhighlight lang="cpp">#include <atomic>
#include <chrono>
#include <chrono>
#include <cmath>
#include <cmath>
Line 377: Line 377:


=={{header|Clojure}}==
=={{header|Clojure}}==
<syntaxhighlight lang=clojure>(ns active-object
<syntaxhighlight lang="clojure">(ns active-object
(:import (java.util Timer TimerTask)))
(:import (java.util Timer TimerTask)))


Line 418: Line 418:
{{libheader|Bordeaux Threads}}
{{libheader|Bordeaux Threads}}


<syntaxhighlight lang=lisp>
<syntaxhighlight lang="lisp">
(defclass integrator ()
(defclass integrator ()
((input :initarg :input :writer input :reader %input)
((input :initarg :input :writer input :reader %input)
Line 507: Line 507:
{{trans|Python}}
{{trans|Python}}
Crystal currently runs all code in a single thread, so a trivial example wouldn't have any issues with thread safety. However, this behavior will likely change in the future. This example was written with that in mind, and is somewhat more complex to show better idioms and be future-proof.
Crystal currently runs all code in a single thread, so a trivial example wouldn't have any issues with thread safety. However, this behavior will likely change in the future. This example was written with that in mind, and is somewhat more complex to show better idioms and be future-proof.
<syntaxhighlight lang=ruby>require "math"
<syntaxhighlight lang="ruby">require "math"
require "time"
require "time"


Line 591: Line 591:
=={{header|D}}==
=={{header|D}}==
{{trans|Java}}
{{trans|Java}}
<syntaxhighlight lang=D>import core.thread;
<syntaxhighlight lang="d">import core.thread;
import std.datetime;
import std.datetime;
import std.math;
import std.math;
Line 671: Line 671:
{{libheader| System.Classes}}
{{libheader| System.Classes}}
{{Trans|Python}}
{{Trans|Python}}
<syntaxhighlight lang=Delphi>
<syntaxhighlight lang="delphi">
program Active_object;
program Active_object;


Line 765: Line 765:
=={{header|E}}==
=={{header|E}}==


<syntaxhighlight lang=e>def makeIntegrator() {
<syntaxhighlight lang="e">def makeIntegrator() {
var value := 0.0
var value := 0.0
var input := fn { 0.0 }
var input := fn { 0.0 }
Line 819: Line 819:
=={{header|EchoLisp}}==
=={{header|EchoLisp}}==
We use the functions (at ..) : scheduling, (wait ...), and (every ...) ot the timer.lib. The accuracy will be function of the browser's functions setTimeout and setInterval ...
We use the functions (at ..) : scheduling, (wait ...), and (every ...) ot the timer.lib. The accuracy will be function of the browser's functions setTimeout and setInterval ...
<syntaxhighlight lang=lisp>
<syntaxhighlight lang="lisp">
(require 'timer)
(require 'timer)
Line 852: Line 852:
</syntaxhighlight>
</syntaxhighlight>
{{Out}}
{{Out}}
<syntaxhighlight lang=lisp>
<syntaxhighlight lang="lisp">
(define (experiment)
(define (experiment)
(define (K t) (sin (* PI t )))
(define (K t) (sin (* PI t )))
Line 876: Line 876:
=={{header|Erlang}}==
=={{header|Erlang}}==
I could not see what time to use between each integration so it is the argument to task().
I could not see what time to use between each integration so it is the argument to task().
<syntaxhighlight lang=Erlang>
<syntaxhighlight lang="erlang">
-module( active_object ).
-module( active_object ).
-export( [delete/1, input/2, new/0, output/1, task/1] ).
-export( [delete/1, input/2, new/0, output/1, task/1] ).
Line 942: Line 942:


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang=fsharp>open System
<syntaxhighlight lang="fsharp">open System
open System.Threading
open System.Threading


Line 988: Line 988:
=={{header|Factor}}==
=={{header|Factor}}==
Working with dynamic quotations requires the stack effect to be known in advance. The apply-stack-effect serves this purpose.
Working with dynamic quotations requires the stack effect to be known in advance. The apply-stack-effect serves this purpose.
<syntaxhighlight lang=factor>USING: accessors alarms calendar combinators kernel locals math
<syntaxhighlight lang="factor">USING: accessors alarms calendar combinators kernel locals math
math.constants math.functions prettyprint system threads ;
math.constants math.functions prettyprint system threads ;
IN: rosettacode.active
IN: rosettacode.active
Line 1,034: Line 1,034:
=={{header|FBSL}}==
=={{header|FBSL}}==
The Dynamic Assembler and Dynamic C JIT compilers integrated in FBSL v3.5 handle multithreading perfectly well. However, pure FBSL infrastructure has never been designed especially to support own multithreading nor can it handle long long integers natively. Yet a number of tasks with careful design and planning are quite feasible in pure FBSL too:
The Dynamic Assembler and Dynamic C JIT compilers integrated in FBSL v3.5 handle multithreading perfectly well. However, pure FBSL infrastructure has never been designed especially to support own multithreading nor can it handle long long integers natively. Yet a number of tasks with careful design and planning are quite feasible in pure FBSL too:
<syntaxhighlight lang=qbasic>#APPTYPE CONSOLE
<syntaxhighlight lang="qbasic">#APPTYPE CONSOLE


#INCLUDE <Include\Windows.inc>
#INCLUDE <Include\Windows.inc>
Line 1,134: Line 1,134:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<syntaxhighlight lang=freebasic>#define twopi 6.2831853071795864769252867665590057684
<syntaxhighlight lang="freebasic">#define twopi 6.2831853071795864769252867665590057684
dim shared as double S = 0 'set up the state as a global variable
dim shared as double S = 0 'set up the state as a global variable
dim shared as double t0, t1, ta
dim shared as double t0, t1, ta
Line 1,168: Line 1,168:
=={{header|Go}}==
=={{header|Go}}==
Using time.Tick to sample K at a constant frequency. Three goroutines are involved, main, aif, and tk. Aif controls access to the accumulator s and the integration function K. Tk and main must talk to aif through channels to access s and K.
Using time.Tick to sample K at a constant frequency. Three goroutines are involved, main, aif, and tk. Aif controls access to the accumulator s and the integration function K. Tk and main must talk to aif through channels to access s and K.
<syntaxhighlight lang=go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 1,255: Line 1,255:
=={{header|Groovy}}==
=={{header|Groovy}}==
{{trans|Java}}
{{trans|Java}}
<syntaxhighlight lang=groovy>/**
<syntaxhighlight lang="groovy">/**
* Integrates input function K over time
* Integrates input function K over time
* S + (t1 - t0) * (K(t1) + K(t0)) / 2
* S + (t1 - t0) * (K(t1) + K(t0)) / 2
Line 1,330: Line 1,330:
=={{header|Haskell}}==
=={{header|Haskell}}==


<syntaxhighlight lang=haskell>module Integrator (
<syntaxhighlight lang="haskell">module Integrator (
newIntegrator, input, output, stop,
newIntegrator, input, output, stop,
Time, timeInterval
Time, timeInterval
Line 1,412: Line 1,412:
Implementation:
Implementation:


<syntaxhighlight lang=J>coclass 'activeobject'
<syntaxhighlight lang="j">coclass 'activeobject'
require'dates'
require'dates'


Line 1,439: Line 1,439:
Task example (code):
Task example (code):


<syntaxhighlight lang=J>cocurrent 'testrig'
<syntaxhighlight lang="j">cocurrent 'testrig'


delay=: 6!:3
delay=: 6!:3
Line 1,467: Line 1,467:


=={{header|Java}}==
=={{header|Java}}==
<syntaxhighlight lang=java>/**
<syntaxhighlight lang="java">/**
* Integrates input function K over time
* Integrates input function K over time
* S + (t1 - t0) * (K(t1) + K(t0)) / 2
* S + (t1 - t0) * (K(t1) + K(t0)) / 2
Line 1,545: Line 1,545:
{{trans|E}}
{{trans|E}}


<syntaxhighlight lang=javascript>function Integrator(sampleIntervalMS) {
<syntaxhighlight lang="javascript">function Integrator(sampleIntervalMS) {
var inputF = function () { return 0.0 };
var inputF = function () { return 0.0 };
var sum = 0.0;
var sum = 0.0;
Line 1,574: Line 1,574:
Test program as a HTML fragment:
Test program as a HTML fragment:


<syntaxhighlight lang=html4strict><p><span id="a">Test running...</span> <code id="b">-</code></p>
<syntaxhighlight lang="html4strict"><p><span id="a">Test running...</span> <code id="b">-</code></p>


<script type="text/javascript">
<script type="text/javascript">
Line 1,599: Line 1,599:
Julia has inheritance of data structures and first-class types, but structures do not have methods.
Julia has inheritance of data structures and first-class types, but structures do not have methods.
Instead, methods are functions with multiple dispatch based on argument type.
Instead, methods are functions with multiple dispatch based on argument type.
<syntaxhighlight lang=julia>mutable struct Integrator
<syntaxhighlight lang="julia">mutable struct Integrator
func::Function
func::Function
runningsum::Float64
runningsum::Float64
Line 1,643: Line 1,643:
{{trans|Java}}
{{trans|Java}}
Athough this is a faithful translation of the Java entry, on my machine the output of the latter is typically an order of magnitude smaller than this version. I have no idea why.
Athough this is a faithful translation of the Java entry, on my machine the output of the latter is typically an order of magnitude smaller than this version. I have no idea why.
<syntaxhighlight lang=scala>// version 1.2.0
<syntaxhighlight lang="scala">// version 1.2.0


import kotlin.math.*
import kotlin.math.*
Line 1,720: Line 1,720:
=={{header|Lingo}}==
=={{header|Lingo}}==
Parent script "Integrator":
Parent script "Integrator":
<syntaxhighlight lang=Lingo>property _sum
<syntaxhighlight lang="lingo">property _sum
property _func
property _func
property _timeLast
property _timeLast
Line 1,763: Line 1,763:


In some movie script:
In some movie script:
<syntaxhighlight lang=Lingo>global gIntegrator
<syntaxhighlight lang="lingo">global gIntegrator


-- entry point
-- entry point
Line 1,788: Line 1,788:
=={{header|Lua}}==
=={{header|Lua}}==
Pure/native Lua is not multithreaded, so this task should perhaps be marked "omit from|Lua" if following the implicit ''intent'' of the task. However, the explicit ''wording'' of the task does not seem to require a multithreaded solution. Perhaps this is ''cheating'', but I thought it might interest the reader to see the integrator portion nonetheless, so it is demonstrated using a mock sampling method at various intervals (to ''simulate'' multithreaded updates).
Pure/native Lua is not multithreaded, so this task should perhaps be marked "omit from|Lua" if following the implicit ''intent'' of the task. However, the explicit ''wording'' of the task does not seem to require a multithreaded solution. Perhaps this is ''cheating'', but I thought it might interest the reader to see the integrator portion nonetheless, so it is demonstrated using a mock sampling method at various intervals (to ''simulate'' multithreaded updates).
<syntaxhighlight lang=lua>local seconds = os.clock
<syntaxhighlight lang="lua">local seconds = os.clock


local integrator = {
local integrator = {
Line 1,839: Line 1,839:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang=Mathematica>Block[{start = SessionTime[], K, t0 = 0, t1, kt0, S = 0},
<syntaxhighlight lang="mathematica">Block[{start = SessionTime[], K, t0 = 0, t1, kt0, S = 0},
K[t_] = Sin[2 Pi f t] /. f -> 0.5; kt0 = K[t0];
K[t_] = Sin[2 Pi f t] /. f -> 0.5; kt0 = K[t0];
While[True, t1 = SessionTime[] - start;
While[True, t1 = SessionTime[] - start;
Line 1,854: Line 1,854:
Of course, it is necessary to take some precautions when accessing or updating the shared object. We use a lock for this purpose.
Of course, it is necessary to take some precautions when accessing or updating the shared object. We use a lock for this purpose.


<lang>
<syntaxhighlight lang="text">
# Active object.
# Active object.
# Compile with "nim c --threads:on".
# Compile with "nim c --threads:on".
Line 1,981: Line 1,981:
Not totally certain this is a correct implementation since the value coming out is not close to zero. It does show all of the basics of multithreading and object synchronization though.
Not totally certain this is a correct implementation since the value coming out is not close to zero. It does show all of the basics of multithreading and object synchronization though.


<syntaxhighlight lang=ooRexx>
<syntaxhighlight lang="oorexx">
integrater = .integrater~new(.routines~sine) -- start the integrater function
integrater = .integrater~new(.routines~sine) -- start the integrater function
call syssleep 2
call syssleep 2
Line 2,064: Line 2,064:


With a high precision timer the result is around -.0002
With a high precision timer the result is around -.0002
<syntaxhighlight lang=oxygenbasic>
<syntaxhighlight lang="oxygenbasic">
double MainTime
double MainTime


Line 2,239: Line 2,239:


=={{header|Oz}}==
=={{header|Oz}}==
<syntaxhighlight lang=oz>declare
<syntaxhighlight lang="oz">declare
fun {Const X}
fun {Const X}
fun {$ _} X end
fun {$ _} X end
Line 2,301: Line 2,301:


=={{header|Perl}}==
=={{header|Perl}}==
<syntaxhighlight lang=perl>#!/usr/bin/perl
<syntaxhighlight lang="perl">#!/usr/bin/perl


use strict;
use strict;
Line 2,375: Line 2,375:
{{libheader|Phix/Class}}
{{libheader|Phix/Class}}


<!--<syntaxhighlight lang=Phix>-->
<!--<syntaxhighlight lang="phix">-->
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"0.8.2"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"0.8.2"</span><span style="color: #0000FF;">)</span>
Line 2,461: Line 2,461:
Just lock everything, it is not that hard, and you should never need much more than the stuff below.
Just lock everything, it is not that hard, and you should never need much more than the stuff below.


<!--<syntaxhighlight lang=Phix>-->
<!--<syntaxhighlight lang="phix">-->
<span style="color: #004080;">sequence</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #008080;">enum</span> <span style="color: #000000;">TERMINATE</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">INTERVAL</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">KFUN</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">VALUE</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">T0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">K0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ID</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ISIZE</span><span style="color: #0000FF;">=$</span>
<span style="color: #008080;">enum</span> <span style="color: #000000;">TERMINATE</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">INTERVAL</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">KFUN</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">VALUE</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">T0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">K0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ID</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ISIZE</span><span style="color: #0000FF;">=$</span>
Line 2,534: Line 2,534:


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<syntaxhighlight lang=PicoLisp>(load "@lib/math.l")
<syntaxhighlight lang="picolisp">(load "@lib/math.l")


(class +Active)
(class +Active)
Line 2,581: Line 2,581:
=={{header|PureBasic}}==
=={{header|PureBasic}}==
Using the open-source precompiler [http://www.development-lounge.de/viewtopic.php?t=5915 SimpleOOP].
Using the open-source precompiler [http://www.development-lounge.de/viewtopic.php?t=5915 SimpleOOP].
<syntaxhighlight lang=PureBasic>Prototype.d ValueFunction(f.d, t.d)
<syntaxhighlight lang="purebasic">Prototype.d ValueFunction(f.d, t.d)


Class IntegralClass
Class IntegralClass
Line 2,659: Line 2,659:




<syntaxhighlight lang=python>from time import time, sleep
<syntaxhighlight lang="python">from time import time, sleep
from threading import Thread
from threading import Thread


Line 2,700: Line 2,700:
=={{header|Racket}}==
=={{header|Racket}}==


<syntaxhighlight lang=racket>
<syntaxhighlight lang="racket">
#lang racket
#lang racket


Line 2,738: Line 2,738:
There is some jitter in the timer, but it is typically accurate to within a few thousandths of a second.
There is some jitter in the timer, but it is typically accurate to within a few thousandths of a second.


<syntaxhighlight lang=perl6>class Integrator {
<syntaxhighlight lang="raku" line>class Integrator {
has $.f is rw = sub ($t) { 0 };
has $.f is rw = sub ($t) { 0 };
has $.now is rw;
has $.now is rw;
Line 2,791: Line 2,791:
=={{header|Rust}}==
=={{header|Rust}}==


<syntaxhighlight lang=rust>#![feature(mpsc_select)]
<syntaxhighlight lang="rust">#![feature(mpsc_select)]


extern crate num;
extern crate num;
Line 2,966: Line 2,966:
=={{header|Scala}}==
=={{header|Scala}}==


<syntaxhighlight lang=Scala>object ActiveObject {
<syntaxhighlight lang="scala">object ActiveObject {


class Integrator {
class Integrator {
Line 3,017: Line 3,017:
=={{header|Smalltalk}}==
=={{header|Smalltalk}}==


<syntaxhighlight lang=Smalltalk>
<syntaxhighlight lang="smalltalk">
Object subclass:#Integrator
Object subclass:#Integrator
instanceVariableNames:'tickRate input s thread'
instanceVariableNames:'tickRate input s thread'
Line 3,081: Line 3,081:
</syntaxhighlight>
</syntaxhighlight>
running:
running:
<syntaxhighlight lang=Smalltalk>Integrator example</syntaxhighlight>
<syntaxhighlight lang="smalltalk">Integrator example</syntaxhighlight>


output:
output:
Line 3,104: Line 3,104:
=={{header|SuperCollider}}==
=={{header|SuperCollider}}==
Instead of writing a class, here we just use an environment to encapsulate state.
Instead of writing a class, here we just use an environment to encapsulate state.
<syntaxhighlight lang=SuperCollider>
<syntaxhighlight lang="supercollider">
(
(
a = TaskProxy { |envir|
a = TaskProxy { |envir|
Line 3,139: Line 3,139:


=={{header|Swift}}==
=={{header|Swift}}==
<syntaxhighlight lang=Swift>// For NSObject, NSTimeInterval and NSThread
<syntaxhighlight lang="swift">// For NSObject, NSTimeInterval and NSThread
import Foundation
import Foundation
// For PI and sin
// For PI and sin
Line 3,214: Line 3,214:


This implementation Tcl 8.6 for object support (for the active integrator object) and coroutine support (for the controller task). It could be rewritten to only use 8.5 plus the TclOO library.
This implementation Tcl 8.6 for object support (for the active integrator object) and coroutine support (for the controller task). It could be rewritten to only use 8.5 plus the TclOO library.
<syntaxhighlight lang=Tcl>package require Tcl 8.6
<syntaxhighlight lang="tcl">package require Tcl 8.6
oo::class create integrator {
oo::class create integrator {
variable e sum delay tBase t0 k0 aid
variable e sum delay tBase t0 k0 aid
Line 3,271: Line 3,271:
Since this object is CPU intensive, shutting it down when done is crucial. To facilitate this, the IDisposable pattern was used.
Since this object is CPU intensive, shutting it down when done is crucial. To facilitate this, the IDisposable pattern was used.


<syntaxhighlight lang=vbnet>Module Module1
<syntaxhighlight lang="vbnet">Module Module1
Sub Main()
Sub Main()
Line 3,355: Line 3,355:


What I've done instead is to pre-compute the number of updates performed on my machine for a given function and time period which is a fairly stable figure (though it will obviously be different on other machines). I've then used this figure to measure elapsed time for each update. On average this gives results of around 0.003 seconds which I consider acceptable in the circumstances.
What I've done instead is to pre-compute the number of updates performed on my machine for a given function and time period which is a fairly stable figure (though it will obviously be different on other machines). I've then used this figure to measure elapsed time for each update. On average this gives results of around 0.003 seconds which I consider acceptable in the circumstances.
<syntaxhighlight lang=ecmascript>import "scheduler" for Scheduler
<syntaxhighlight lang="ecmascript">import "scheduler" for Scheduler
import "timer" for Timer
import "timer" for Timer


Line 3,419: Line 3,419:
{{trans|Python}}
{{trans|Python}}
Uses cheese ball thread safety: since the integrator runs continuously and I don't want to queue the output, just sample it, strong references are used as they change atomically.
Uses cheese ball thread safety: since the integrator runs continuously and I don't want to queue the output, just sample it, strong references are used as they change atomically.
<syntaxhighlight lang=zkl>class Integrator{
<syntaxhighlight lang="zkl">class Integrator{
// continuously integrate a function `K`, at each `interval` seconds'
// continuously integrate a function `K`, at each `interval` seconds'
fcn init(f,interval=1e-4){
fcn init(f,interval=1e-4){
Line 3,438: Line 3,438:
fcn setF(f) { K.set(f) }
fcn setF(f) { K.set(f) }
}</syntaxhighlight>
}</syntaxhighlight>
<syntaxhighlight lang=zkl>ai:=Integrator(fcn(t){ ((0.0).pi*t).sin() });
<syntaxhighlight lang="zkl">ai:=Integrator(fcn(t){ ((0.0).pi*t).sin() });
Atomic.sleep(2);
Atomic.sleep(2);
ai.sample().println();
ai.sample().println();
Line 3,451: Line 3,451:
</pre>
</pre>
{{omit from|6502 Assembly|Not an object-oriented language.}}
{{omit from|6502 Assembly|Not an object-oriented language.}}
{{omit from|68000 Assembly}}
{{omit from|8080 Assembly}}
{{omit from|8080 Assembly}}
{{omit from|8086 Assembly}}
{{omit from|8086 Assembly}}
{{omit from|68000 Assembly}}
{{omit from|ACL2}}
{{omit from|ACL2}}
{{omit from|ARM Assembly}}
{{omit from|ARM Assembly}}
Line 3,461: Line 3,461:
{{omit from|LaTeX}}
{{omit from|LaTeX}}
{{omit from|Locomotive Basic}}
{{omit from|Locomotive Basic}}
{{omit from|Make}}
{{omit from|Metafont}}
{{omit from|M4}}
{{omit from|M4}}
{{omit from|Make}}
{{omit from|Maxima}}
{{omit from|Maxima}}
{{omit from|Metafont}}
{{omit from|ML/I}}
{{omit from|ML/I}}
{{omit from|Octave}}
{{omit from|Octave}}
{{omit from|PlainTeX}}
{{omit from|PlainTeX}}
{{omit from|TI-89 BASIC}} <!-- Does not have concurrency or background processes. -->
{{omit from|Retro}}
{{omit from|Retro}}
{{omit from|TI-89 BASIC}} <!-- Does not have concurrency or background processes. -->
{{omit from|UNIX Shell}}
{{omit from|UNIX Shell}}
{{omit from|VBScript}}
{{omit from|x86 Assembly}}
{{omit from|x86 Assembly}}
{{omit from|Z80 Assembly}}
{{omit from|Z80 Assembly}}