[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Help with recursive destructive function
From: |
Eric Abrahamsen |
Subject: |
Re: Help with recursive destructive function |
Date: |
Sun, 06 May 2018 11:27:42 -0700 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) |
On 05/05/18 17:41 PM, Michael Heerdegen wrote:
> I wrote:
>
>> I guess I would use an iterator: the definition would still looks
>> recursive, but the execution isn't problematic any more if done right.
>
> Here is an example:
>
> #+begin_src emacs-lisp
> (iter-defun iter-tree-example (tree)
> (cl-loop for thing in-ref tree by #'cdr do
> (if (consp thing)
> (iter-yield-from (iter-tree-example thing))
> (iter-yield
> (ignore
> (when (stringp thing)
> (cl-callf upcase thing)))))))
>
> (let ((tree '("a" 1 ("b" "c" ("d")) "e")))
> (iter-do (_ (iter-tree-example tree)))
> tree)
> ==>
> ("A" 1
> ("B" "C"
> ("D"))
> "E")
Oh hang on, this doesn't work for all cases: the "by #'cdr" prevents it
from hitting the cdr of cons cells:
(let ((tree '("c" (2 ("d" . 3)) (4 . "e") "f")))
(iter-do (_ (iter-tree-example tree)))
tree)
--> ("C" (2 ("D" . 3)) (4 . "e") "F")
The best I've been able to come up with is:
(defun useless (val)
(while (consp val)
(let ((head (car val)))
(cond ((stringp head)
(setcar val (upcase head)))
((listp head)
(useless head)))
(when (stringp (cdr val))
(setcdr val (upcase (cdr val))))
(setq val (cdr val)))))
Recurses on car, and only repeats the test twice. I'd love to know if
that could be simplified...
Thanks again,
Eric
- Help with recursive destructive function, Eric Abrahamsen, 2018/05/04
- Re: Help with recursive destructive function, Stefan Monnier, 2018/05/04
- Re: Help with recursive destructive function,
Eric Abrahamsen <=
- Re: Help with recursive destructive function, Michael Heerdegen, 2018/05/06
- Re: Help with recursive destructive function, Eric Abrahamsen, 2018/05/06
- Re: Help with recursive destructive function, Clément Pit-Claudel, 2018/05/07
- Re: Help with recursive destructive function, Michael Heerdegen, 2018/05/07
- Re: Help with recursive destructive function, Stefan Monnier, 2018/05/07
- Re: Help with recursive destructive function, Eric Abrahamsen, 2018/05/07
- Re: Help with recursive destructive function, Michael Heerdegen, 2018/05/08
- Re: Help with recursive destructive function, Eric Abrahamsen, 2018/05/08
- Re: Help with recursive destructive function, Clément Pit-Claudel, 2018/05/08
- Re: Help with recursive destructive function, Eric Abrahamsen, 2018/05/08