Refactor to Scheme
This skill guides you through refactoring existing JavaScript logic into Scheme. The project philosophy is Scheme over JS: implement as much as possible in Scheme, using JS only for the bare minimum (primitives, IO, DOM).
Criteria
- •Move to Scheme: Core logic, algorithms, transformation steps, initialization sequences.
- •Keep in JS: Low-level DOM manipulation, browser APIs, Node.js APIs, performance-critical hot loops (only if proven slow in Scheme).
Steps
1. Analyze and Plan
- •Identify the JavaScript file/functionality to convert.
- •Determine dependencies: what JS primitives does it need?
- •Design the Scheme interface.
2. Implement Scheme Replacement
- •Create a new Scheme library (see Create Library skill) or add to an existing one.
- •Implement the logic in Scheme.
- •If you need to call this from JavaScript (e.g., during startup), ensure it's exported or registered.
- •Note: Code in
(define-library ...)is executed when loaded.
- •Note: Code in
3. Bridge Primitives (If needed)
If the new Scheme code needs access to specific JS features it didn't have before, create new primitives (see Implement Primitive skill).
4. Remove/Minimize JavaScript
- •Delete the old JavaScript logic.
- •Refactor the JS entry point to just load/invoke the Scheme code.
5. Update Tests
- •Port existing JS unit tests to Scheme (preferred).
- •If strictly JS interoperability tests, update them to call the new Scheme implementation via
interopor the interpreter instance.
Example: Refactoring Initialization
Before (JS):
javascript
function init() {
setupWorkspace();
loadBlocks();
}
After (Scheme):
scheme
;; src/scheme/init.scm (define (init) (setup-workspace) (load-blocks))
After (JS):
javascript
interpreter.eval('(init)');