Word Count — Mere → DOM

Client-side text stats, computed in a Mere function compiled to Wasm. Click Count to run count_words / count_lines / str_len over the textarea contents and update the three displays through contrib/dom/dom.mere.

chars 0
words 0
lines 0

What's happening

The word counter isn't a hand-written JS function — it's a Mere program compiled to Wasm. count_words walks the string character by character, tracking whether it's inside a word to fold runs of whitespace into one boundary. The three dom_set_text calls update the displays through the same closure-dispatch machinery the counter demo uses.

Source (wordcount.mere)

import "contrib/dom/dom.mere";

let count_words = fn (s: str) ->
  let n = str_len s in
  let st = map_new () in
  let _ = map_set st "i" 0 in
  let _ = map_set st "count" 0 in
  let _ = map_set st "in_word" 0 in
  let _ = while map_get st "i" < n do
    let i = map_get st "i" in
    let c = char_at s i in
    let is_ws = str_eq c " " || str_eq c "\t"
             || str_eq c "\n" || str_eq c "\r" in
    let _ = if is_ws then
              map_set st "in_word" 0
            else if map_get st "in_word" == 0 then
              let _ = map_set st "count" (map_get st "count" + 1) in
              map_set st "in_word" 1
            else () in
    let _ = map_set st "i" (i + 1) in () in
  map_get st "count"
  ;

// … + count_lines + wiring three displays via dom_set_text.