Mental Model
Here you will learn more about the aktor development model, how it compares to vanilla js code and which bennefits it brings to your workflow.
This code is a simple math example that calculates the following formula as vanilla JS: It does the same as the Reaktor below. What is the difference between the two? The Aktor implements an interpreter pattern, which allows for
- tracking of the execution of the code
- caching of the results
- improved error handling
- aktors are async by default
- the graph of aktors can be visualized
- The execution can be stopped at any time and resumed later
- It collects statistics about the execution like the runtime of each aktor function
"Functionally" they are the same. When you write aktor code keep a mental picture of the vanilla JS code: It is all about function composition.
An aktor is a function that takes parameters and returns a value.
Math Example Reaktor
You can find the below example, as a template in the App Store.
Definition of Functions and Aktors
import { z } from 'zod';
import { aktorIfElse, createAktorFunction, registerReaktorDefinition } from '@operaide/aktor';
function sum({ x, y }: { x: number; y: number }) {
return x + y;
}
const aktorSum = createAktorFunction('aktorSum', sum);
function multiply({ x, y }: { x: number; y: number }) {
return x * y;
}
const aktorMultiply = createAktorFunction('aktorMultiply', multiply);
function gt({ x, y }: { x: number; y: number }) {
return x > y;
}
const aktorGt = createAktorFunction('aktorGt', gt);
function ifElse(p: { if_: boolean; then_: number; else_: number }) {
return p.if_ ? p.then_ : p.else_;
}
Vanilla JS Method
/**
* @param a
* @param b
* @param c
*/
export function mathExample({ a, b, c }: { a: number; b: number; c: number }) {
return sum({
x: a,
y: sum({
x: multiply({ x: b, y: c }),
y: ifElse({ if_: gt({ x: a, y: b }), then_: b, else_: a }),
}),
});
}
Reaktor Implementation
registerReaktorDefinition({
reaktorDefinitionId: 'simple-math',
label: 'Math example ',
description: 'a + (b * c) + (a > b ? b : a)',
createReaktor({ a, b, c }) {
return aktorSum({
x: a,
y: aktorSum({
x: aktorMultiply({ x: b, y: c }),
y: aktorIfElse({ if_: aktorGt({ x: a, y: b }), then_: b, else_: a }),
}),
});
},
inputSchema: z.object({
a: z.number().openapi({ example: 1 }),
b: z.number().openapi({ example: 2 }),
c: z.number().openapi({ example: 20 }),
}),
outputSchema: z.number(),
});