Skip to content

Instantly share code, notes, and snippets.

; Define a function to send events
(defn event [k v] nil)
; Define the data structure used to model a component
(defstruct component-s :name :handlers :state)
; Create a component by creating an instance of the data structure
(defn make-component [name handlers]
(struct component-s name handlers {}))
(program
; Define a component.
(component "Comp1"
; Event handler for :init events
(on :init [v s]
; Trigger some events
(event :print nil)
(event :set-msg "Ho")
(event :print nil)
; Set this components state
(defmacro program [& components]
`(run-components ~@components))
(defmacro component [name & handlers]
`(make-component ~name (conj {} ~@handlers)))
(defmacro on [handler args & body]
{handler `(fn ~args ~@body)})
(run-components
(make-component "Comp1"
{:init (fn [v s]
(event :set-msg "Ho")
(event :print nil)
{:msg "Hi"})
:print (fn [v s]
(println (:msg s)))
:set-msg (fn [v s]
{:msg v})})
(run-components
(make-component "Comp1"
{:init (fn [v s]
(event :print nil)
(event :set-msg "Ho")
(event :print nil)
{:msg "Hi"})
:print (fn [v s]
(println (:msg s)))
:set-msg (fn [v s]
fun do-op a b op {
match op
with '+: a + b
with '-: a - b
with '*: a * b
with '/: a / b
with _: error
}
flow gen a b ops -> res {
@std.in;
a = [[int\]] <- 0;
op = [[char\]] <- ' ';
b = [[int\]] <- 0;
r = [[int\]] <- 0;
0 {
clock => [[int ms]] {
("\r" + ms/1000.0 + ":" + a + op + b + "=" + r) ->std.out;
#include <list>
#include <iostream>
// Replace with real mutex.
class Mutex
{
public:
void lock () {}
void unlock () {}
};
template <class T> void map (std::deque<T> list, void (*f) (T)) {
for (std::deque<T>::iterator iter = list.begin(); iter != list.end(); iter++) {
#ifdef PARALLEL_MAP
ThreadPool::getIdle()->run<T>(f, *iter);
#else
f(*iter);
#endif
}
}
template <class T, class A1> void map (std::deque<T> list, void (*f) (T, A1), A1 arg1) {
typedef unsigned int MessageID;
typedef unsigned int Conversation;
class Message
{
public:
virtual ~Message () {}
virtual const MessageID& messageID () const=0;
virtual const char* senderName () const=0;
};