Trie based word search demo code for 8th

Below is a simple Trie based 8th word search demo.

needs nk/gui
needs utils/help

22 font:system font:new "font1" font:atlas! drop
22 1.5 n:* constant ROW-HEIGHT
22 1.2 n:* constant LIST-ROW-HEIGHT

var numwords
var trie

a:new constant list-data

\ Get number of items inside the list
: getcount \ -- n
  list-data a:len' n:1- ;

\ Get item number n for list from array
: getitem \ n -- s
  list-data swap a:_@ ;

\ Query all 8th words from internal 8th help db and
\ return as array of strings
: all-words \ -- a
  help_db @
  "SELECT cls||\":\"||nm FROM words ORDER BY cls,nm" db:exec[] nip
  a:squash ;

\ Build Trie from wordlist, return also number of words
: build-trie \ -- tree numwords
  null false tree:trie all-words a:len >r ' tree:add a:each! drop r> ;

: new-win
  {
    name: "main",
    title: "8th Word Search",           
    wide: 0.5,                     
    high: 0.5,                   
    bg: "white"
  }
  nk:win
  "list" nk:list-new nk:m! ;

\ Display list item
: list-item  \ s --
  nk:TEXT_LEFT "black" nk:label-colored ;

\ Search a word from Trie and fill results
\ into list-data array.
: search
  list-data a:clear
  trie @
  "word" nk:get
  numwords @ tree:search nip
  a:+ drop ;

: main-render
  {
    bg: "white",
    font: "f1",
    flags: [ @nk:WINDOW_NO_SCROLLBAR ],
    word: ""
  }

  nk:begin
    null  { rows: [@ROW-HEIGHT, -1], cols: 1, rgap: 4 } nk:layout-grid-begin
      0 1 0 1 nk:grid rect>local nk:grid-push
        "word" nk:get 31 nk:EDIT_FIELD nk:PLUGIN_FILTER_DEFAULT nk:edit-string if
          "word" swap nk:set
          search
        else
          drop
        then
      1 1 0 1 nk:grid nk:rect>local nk:grid-push
        "list" nk:TEXT_LEFT LIST-ROW-HEIGHT getcount "list" nk:m@ dup>r nk:list-begin if
          LIST-ROW-HEIGHT 1 nk:layout-row-dynamic
            (
              getitem list-item
            ) r@ nk:list-range drop loop
          r@ nk:list-end
        then rdrop
    nk:layout-grid-end
  nk:end ;

: app:main
  build-trie numwords ! trie !
  new-win ' main-render -1 nk:render-loop ;

I love ‘simple’

This could be a program to play a Rick Astley video, launch WW3, or quietly crash for all I can make out here.
Definitely “needs utils/help”

Don’t get me wrong - I’m very happy you have a coding tool you can use. No doubt it compiles to less than 1K.
But I personally, am never even going to attempt ‘Hello World’ in it. :slight_smile:

1 Like

I would say if you want to show off a what language is doing and why it’s so cool then comments on what each line/section is doing is a must. Otherwise we are reading a foreign language.

5 Likes

That’s right. For me it is nearly not readable. Go, Rust, C++, Java, even Delphi are readable for me. Yes, also Xojo. Sometimes. Bot this one is a language I can not read really logically. You’ll have to learn it to get what it is.

Comments would help possibly. But hey, we are reading his 8th examples for a long time now. It is okay. If he will one day provide some code it will be okay.

That 8th is like Forth & a stack based language is the initial hurdle
Its like reading RPN all the time

I glance at the code but truly dont understand any of it
Comments would help grok the intent