2 ms·
sentence function based on giterate instead of for loop. (defun sentence (map) (let* ((words (cdr [giterate true (op pick-next-word map) :start]))
by kazinator 11y ago
sentence function based on giterate instead of for loop.
(defun sentence (map)
(let* ((words (cdr [giterate true (op pick-next-word map) :start]))
(dot (pos-if (op eq [@1 -1] #\.) words)))
(cat-str [words 0..(succ dot)] " ")))
Requires small weak to pick-next-word
(defun pick-next-word (map word)
(let ((choices [map word]))
(if choices [choices (rand (length choices))])))
giterate returns an infinite list, but we take from it up to the word which ends with dot, ignoring the endless stream of nil objects which follows.
Alternative: partition the list on the ending word, then glue the first two partitions:
(defun sentence (map)
(let* ((words (cdr [giterate true (op pick-next-word map) :start]))
(part (partition-by (op eq [@1 -1] #\.) words)))
(cat-str (append (first part) (second part)) " ")))