Files
gemigreerd-js-maker/scrbl/usecases.scrbl
T
2026-06-08 13:21:57 +02:00

68 lines
1.3 KiB
Racket

#lang scribble/manual
@(require (for-label racket/base js-maker))
@title{js-maker Use Cases}
@author+email["Hans Dijkema" "hans@dijkewijk.nl"]
@defmodule[js-maker/demo/js-usecases]
The demos in @filepath{demo/js-usecases.rkt} use only the public
@racket[js] macro and the compact js-maker 3 form set. They are intentionally
small: their purpose is to show the supported surface language, not to recreate
the larger runtime-helper branch.
@section{Random number}
@codeblock{
(js
(define (randomBetween1And5)
(return (+ (send Math floor (* (send Math random) 5)) 1))))
}
@section{Unique values}
@codeblock{
(js
(define (uniqueValues xs)
(return (send Array from (new Set xs)))))
}
@section{Indexed access}
@codeblock{
(js
(define (arrayAt xs i)
(return (js-ref xs i))))
}
@section{Named let loop}
@codeblock{
(js
(define (sumTo n)
(let loop ([i 0] [acc 0])
(if (> i n)
(return acc)
(loop (+ i 1) (+ acc i))))))
}
@section{Function value}
@codeblock{
(js
(define (makeAdder x)
(return (lambda (y)
(return (+ x y))))))
}
@section{DOM setter}
@codeblock{
(js
(define (setHtml id html)
(let ([el (send document getElementById id)])
(set! (js-dot el innerHTML) html)
(return (js-dot el innerHTML)))))
}