Using Raku++ to create website functionality
Gifts this year
Two talented Raku community members – Andrey Shitov and Tokuhiro Matsuno – have developed two new Raku compilers using Generative AI tools. Andrey’s Raku++ is written in C++ and Tokuhiro’s mutsu is written in Rust. Both offer ways to write Website functionality in Raku by clever leveraging of WASM. [ 1 ]
I consider both Raku++ and mutsu to be amazing gifts to the Raku community this year, so thank you to both Andrey and Tokuhiro.
Since I far prefer writing programs in Raku than in Javascript, I was excited to try one of the above to move functionality in the new documentation website to Raku.
The new Raku documentation website deployment is still being developed / tested, but both Raku++ and mutsu can be invoked in the website by clicking on the terminal icon on the right-hand side.
For the purposes of this article, I can only use one of these projects, and there is a lot of documentation to read. In addition, I have a bit more familiarity with C++ than Rust, so that slightly tilted my decision to use Raku++ for this test as I was expecting to need to look under the hood given the newness of the project. To be honest, there was never any need to look at the underlying code, so I offer my humble appologies to Tokuhiro for not choosing mutsu and I will look at it in the future.
Greetings in other languages
If you take a look at the new Raku documentation website, you will see a drop-down to select the UX language. The goal is to have Raku documentation in multiple languages, and for the UI language to be independent of the content language (current multi-lingual sites change both the navigation and content together which is a pain if you just want to check on some item in another language, but have to work out how to get to it in the other language).
Essentially all user-facing text is held in HTML spans with a key, and the web page is shipped with a map from from the language / key to the text to be used.
What I want to explore is how much of the functionality currently implemented in Javascript can be implemented in Raku, and whether there is a performance hit. Articles about WASM indicate that Javascript will be faster for this sort of functionality and that other language implementations in WASM will be faster for calculation heavy functionality, so this particular example should not have any performance benefit.
The existing implementation
The following is an HTML file with a stripped down version of the Javascript-only program (view it here).
Code1.
<!DOCTYPE html><html lang="en"><head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <meta charset="UTF-8"> <title>Greet the world</title> <script> var dictionary = { "en": { "LangName": "English", "HappyBirthday": "Happy Birthday", "Congratulations": "Congratulations", "HappyHolidays": "Happy holidays" }, "uk": { "LangName": "Українська", "HappyBirthday": "З днем ародження", "Congratulations": "Вітаю", "HappyHolidays": "Зі святами" }, "cy": { "LangName": "Cymraeg", "HappyBirthday": "Pen-blwydd hapus", "Congratulations": "Llongyfarchiadau", "HappyHolidays": "Gwyliau hapus" }, "ja": { "LangName": "日本語", "HappyBirthday": "お誕生日おめでとう", "Congratulations": "おめでとう", "HappyHolidays": "楽しい休日を" }, "nl": { "LangName": "Nederlands", "HappyBirthday": "Gefeliciteerd met je verjaardag", "Congratulations": "Gefeliciteerd", "HappyHolidays": "Fijne feestdagen" }, "epo": { "LangName": "Esperanto", "HappyBirthday": "Feliĉan naskiĝtagon", "Congratulations": "Gratulojn", "HappyHolidays": "Feliĉajn feriojn" } }; </script> <script> function setUI( tSpans, newLang ) { tSpans.forEach( function (elem) { token = elem.getAttribute('data-UIToken'); elem.innerHTML = dictionary[newLang][token]; }); }; </script> <script> function makeUISelector( initial, selectorElem ) { var options = ''; Object.keys(dictionary).forEach(function(key) { isSel = key == initial ? ' selected' : ''; options = options + '<option value="' + key + '"' + isSel + '>' + dictionary[key]['LangName'] + '</option>'; }); selectorElem.innerHTML = options; }; </script> <script> var defLang = 'en'; var uiselector; var token_spans; document.addEventListener('DOMContentLoaded', () => { uiselector = document.getElementById('Elucid8_ui-switch-contents'); token_spans = document.querySelectorAll('.Elucid8-ui'); makeUISelector( defLang, uiselector ); setUI( token_spans, defLang ); uiselector.addEventListener('change', (event) => { newLang = event.target.value; setUI( token_spans, newLang ); }); // placeholder for Raku link }); </script></head><body><table style="border: medium groove black;"><tr><td>Greeting language</td><td>Javascript: <select id="Elucid8_ui-switch-contents"></select></td></tr><tr><td>Greeting 1</td><td><span class="Elucid8-ui" data-uitoken="HappyBirthday">Greeting 1</span></td></tr> <tr><td>Greeting 2</td><td><span class="Elucid8-ui" data-uitoken="Congratulations">Greeting 2</span></td></tr> <tr><td>Greeting 3</td><td><span class="Elucid8-ui" data-uitoken="HappyHolidays">Greeting 3</span></td></tr></table></body></html>
Main components
The Javascript program has been split into four components between script tags:
📜 a dictionary which contains the three greetings and language name keyed by language and token
📜 code to generate the drop-down selection based on the languages in the dictionary
📜 code to replace the text of spans with the selected language
📜 code that runs when the DOM has settled, including a search to gather into an Array all the DOM elements containing spans that have tokens that need replacing.
Moving into Raku?
The first step is to move the dictionary to Raku (again this is nowhere near optimal given that the dictionary is simply a native Javascript object).
Apart from the hash definition, the Raku program is trivial. Lets assume this is the content of a text file called r-funtions.raku:
Code2.
our %h = en => %( LangName => "English", HappyBirthday => "Happy Birthday", Congratulations => "Congratulations", HappyHolidays => "Happy holidays", ), uk => %( LangName => "Українська", HappyBirthday => "З днем народження", Congratulations => "Вітаю", HappyHolidays => "Зі святами", ), cy => %( LangName => "Cymraeg", HappyBirthday => "Pen-blwydd hapus", Congratulations => "Llongyfarchiadau", HappyHolidays => "Gwyliau hapus", ), ja => %( LangName => "日本語", HappyBirthday => "お誕生日おめでとう", Congratulations => "おめでとう", HappyHolidays => "楽しい休日を", ), nl => %( LangName => "Nederlands", HappyBirthday => "Gefeliciteerd met je verjaardag", Congratulations => "Gefeliciteerd", HappyHolidays => "Fijne feestdagen", ), epo => %( LangName => "Esperanto", HappyBirthday => "Feliĉan naskiĝtagon", Congratulations => "Gratulojn", HappyHolidays => "Feliĉajn feriojn", ),;sub rDictionary( $lang, $token ) is export { %h{$lang}{$token}}
After installing the raku++ compiler as described in Install raku++ , we compile this to a Javascript file called rFunctions.js as follows
Code3.
raku++ -o rFunctions.js --js --standalone r-functions.raku
Notes:
📜 The raku++ version must be > 5.11.0.
📜 The rFunctions.js file needs to be served to the webpage, so the source used below is from a github repo via a cdn (directly sourcing from github runs into a NoSniff error).
📜 The options for raku++ are given in more detail in the project’s documentation, however
▹ -o is the output (compiled) file name;
▹ –js indicates the compilation target;
▹ –standalone indicates only one file is to be created; and
▹ the final option is the input file name
📜 Note the is export trait on the Raku function, which magically allows it to be used within a Javascript program. This piece of magic really makes life easy.
In order to link the Raku to the webpage, we add two scripts into the document head section, namely
Code4.
<!-- this should read '<script' but WordPress forbids it -->
src="http://cdn.jsdelivr.net/gh/finanalyst/Advent2026_Articles/rFunctions.js">
</script>
<script>
function rSetUI( tSpans, newLang ) {
tSpans.forEach( function (elem) {
token = elem.getAttribute('data-UIToken');
elem.innerHTML = rDictionary(newLang,token);
});
};
</script>
The github repo containing the sample output pages and the rFunctions.js also contains the RakuDoc source of this article.
In addition, we add the following in place of the comment // placeholder for Raku call.
Code5.
ruiselector = document.getElementById('rElucid8_ui-switch-contents');rtoken_spans = document.querySelectorAll('.r-Elucid8-ui');makeUISelector( defLang, ruiselector );rSetUI( rtoken_spans, defLang );ruiselector.addEventListener('change', (event) => { newLang = event.target.value; rSetUI( rtoken_spans, newLang );});
Finally the table needs another column for the Raku greetings.
Code6.
<table style="border: medium groove black;"><tr><td>Greeting language</td><td>Javascript: <select id="Elucid8_ui-switch-contents"></select></td> <td>Raku: <select id="rElucid8_ui-switch-contents"></select></td></tr><tr><td>Greeting 1</td><td><span class="Elucid8-ui" data-uitoken="HappyBirthday">-</span></td><td><span class="r-Elucid8-ui" data-uitoken="HappyBirthday">-</span></td></tr><tr><td>Greeting 2</td><td><span class="Elucid8-ui" data-uitoken="Congratulations">-</span></td><td><span class="r-Elucid8-ui" data-uitoken="Congratulations">-</span></td></tr><tr><td>Greeting 3</td><td><span class="Elucid8-ui" data-uitoken="HappyHolidays">-</span></td><td><span class="r-Elucid8-ui" data-uitoken="HappyHolidays">-</span></td></tr></table>
The new page
Populating DOM elements from within Raku
In order to introspect the DOM of the HTML file, and to modify it, raku++ has the use js pragma. The pragma creates a new JS object, which can then be treated as the global This.
We add the following function to our r-functions.raku file:
Code7.
use js;sub changeLang( $new-lang, $tag-class) is export { my $doc = JS.document; my @targets = $doc.querySelectorAll( $tag-class ); for @targets -> $elem { my $token = $elem.getAttribute('data-UIToken'); $elem.innerHTML = %h{ $new-lang}{$token}; }}
Then we replace the snippet in Code5 with
Code8.
ruiselector = document.getElementById('rElucid8_ui-switch-contents');makeUISelector( defLang, ruiselector );changeLang( defLang, '.r-Elucid8-ui');rSetUI( rtoken_spans, defLang );ruiselector.addEventListener('change', (event) => { newLang = event.target.value; changeLang( newLang, '.r-Elucid8-ui');});
Moving the selector creation to Raku
Now we take the final step by populating the language selector from the keys of the hash, and attach a listener to the selector all inside a Raku function.
We add a new Raku function to r-functions.raku, namely
Code9.
sub setSelector( $selectorElem, $tag-class, $initial ) is export { my $doc = JS.document; my $sel = $doc.getElementById( $selectorElem ); my $options = [~] %h.map({ '<option value="' ~ .key ~ '"' ~ { .key eq $initial ?? ' selected' !! ''} ~ '>' ~ .value<LangName> ~ '</option>' }); $sel<innerHTML> = $options; changeLang( $initial, $tag-class ); # set up Event listener on selector $sel.addEventListener('change', -> $el { my $new-language = $el.target.value; changeLang( $new-language, $tag-class ); });}
Now the entire snippet in Code5 can be replaced with
Code10.
setSelector('rElucid8_ui-switch-contents','.r-Elucid8-ui', defLang );
And the result can be viewed here
Some concluding remarks
After reading online tutorials about WASM, I was expecting a far more complex problem, and I planned this article to have very simple steps, sticking very closely to the Javascript version.
In the end, it was remarkably easy, and I now look forward to replacing other Javascript functionality with Raku functions, especially when regexen are needed. Compared to the elegance and simplicity pf Raku’s regexen, Javascript’s are quite horrific.
Footnotes
1 |^| I believe that the original intent was for Rakudo to offer a similar sort of backend, but development of that initiative preceeded the work on WASM, and the run time model for Rakudo was inherently multi-threaded, while Javascript in a browser is robustly single-threaded. No doubt this will change.