help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Lisp Live buffer


From: Emanuel Berg
Subject: Re: Lisp Live buffer
Date: Mon, 26 Dec 2022 21:26:12 +0100
User-agent: Gnus/5.13 (Gnus v5.13)

> (setq ttt-board
>   (list
>     '(x o o)
>     '(· x ·)
>     '(· · x) ))

I have tried to do a MVC Elisp demo but the function 'world-set' is
incorrect, I still didn't get past that step. See below, it's line
32-38. The result is visible in the test function and it's
output, last.

What is needed is essentially a multidimensional array and then
getters and setters (based on indexes or coordinates) to operate
on that.

Ideas?

;;; -*- lexical-binding: t -*-
;;
;; this file:
;;   https://dataswamp.org/~incal/emacs-init/mvc/model.el

(require 'cl-lib)

(defun make-world (dim len &optional init)
  (or init (setq init "."))
  (if (= dim 1)
      (make-list len init)
    (make-list len (make-world (1- dim) len init)) ))

(defun world-size (world)
  (length (flatten-list world)) )

(defun make-world-test ()
  (let ((tests (list (list "cell"  (make-world 1 1)  1)
                     (list "ttt"   (make-world 2 3)  9)
                     (list "chess" (make-world 2 8) 64)
                     (list "cube"  (make-world 3 3) 27) )))
    (cl-loop
      for (name world expected) in tests
      and num-tests from 0 do
        (let ((size (world-size world)))
          (unless (= size expected)
            (error "World %s has size %s, expected %s" name size expected) ))
      finally return (= num-tests (length tests)) )))

;; (make-world-test)

(defun world-set (world pos to)
  (if (= 1 (length pos))
      (setcar (nthcdr (car pos) world) to)
    (setcar
      (nthcdr (car pos) world)
      (world-set (car (nthcdr (car pos) world)) (cdr pos) to) ))
  world)

(defun world-set-test ()
  (let ((ttt (make-world 2 3)) )
    (world-set ttt '(0 1) "a") ))

;; (world-set-test) ; (("." "a" ".") ("." "a" ".") ("." "a" "."))

(provide 'model)

-- 
underground experts united
https://dataswamp.org/~incal




reply via email to

[Prev in Thread] Current Thread [Next in Thread]