Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,30 @@
(require 'timer)
;; returns an 'object' : (&lamdba; message [values])
;; messages : input, output, sample, inspect
(define (make-active)
(let [
(t0 #f) (dt 0)
(t 0) (Kt 0) ; K(t)
(S 0) (K 0)]
(lambda (message . args)
(case message
((output) (// S 2))
((input ) (set! K (car args)) (set! t0 #f))
((inspect) (printf " Active obj : t0 %v t %v S %v " t0 t Kt (// S 2 )))
((sample)
(when (procedure? K)
;; recved new K : init
(unless t0
(set! t0 (first args))
(set! t 0)
(set! Kt (K 0)))
;; integrate K(t) every time 'sample message is received
(set! dt (- (first args) t t0)) ;; compute once K(t)
(set! S (+ S (* dt Kt)))
(set! t (+ t dt))
(set! Kt (K t))
(set! S (+ S (* dt Kt)))))
(else (error "active:bad message" message))))))

View file

@ -0,0 +1,19 @@
(define (experiment)
(define (K t) (sin (* PI t )))
(define A (make-active))
(define (stop) (A 'input 0))
(define (sample t) (A 'sample (// t 1000)))
(define (result) (writeln 'result (A 'output)))
(at 2.5 'seconds 'result)
(every 10 'sample) ;; integrate every 10 ms
(A 'input K)
(wait 2000 'stop))
(experiment) →
3/7/2015 20:34:18 : result
result 0.0002266920372221955
(experiment) →
3/7/2015 20:34:28 : result
result 0.00026510586971023164

View file

@ -0,0 +1,65 @@
// For NSObject, NSTimeInterval and NSThread
import Foundation
// For PI and sin
import Darwin
class ActiveObject:NSObject {
let sampling = 0.1
var K: (t: NSTimeInterval) -> Double
var S: Double
var t0, t1: NSTimeInterval
var thread = NSThread()
func integrateK() {
t0 = t1
t1 += sampling
S += (K(t:t1) + K(t: t0)) * (t1 - t0) / 2
}
func updateObject() {
while true {
integrateK()
usleep(100000)
}
}
init(function: (NSTimeInterval) -> Double) {
S = 0
t0 = 0
t1 = 0
K = function
super.init()
thread = NSThread(target: self, selector: "updateObject", object: nil)
thread.start()
}
func Input(function: (NSTimeInterval) -> Double) {
K = function
}
func Output() -> Double {
return S
}
}
// main
func sine(t: NSTimeInterval) -> Double {
let f = 0.5
return sin(2 * M_PI * f * t)
}
var activeObject = ActiveObject(function: sine)
var date = NSDate()
sleep(2)
activeObject.Input({(t: NSTimeInterval) -> Double in return 0.0})
usleep(500000)
println(activeObject.Output())