3 ms·
I perused a couple of links. This code [0] is pretty much unidiomatic. Closing parens one per line and using setq all over the place. [0]: http://ergoemacs.org
by PuercoPop 11y ago
I perused a couple of links. This code [0] is pretty much unidiomatic. Closing parens one per line and using setq all over the place.
[0]: http://ergoemacs.org/emacs/elisp_grep_string_inside_tag.html http://ergoemacs.org/emacs/elisp_grep_string_inside_tag.html
- kazinator 11y agoThat is pretty atrocious. All the initializing setq's could just be init forms in the let. Now you might think that this is also unidiomatic: (setq totalCnt (1+ totalCnt)) Turns out, though, that Elisp doesn't have modify macros, except in the CL compatibility package. What Xah is doing is the same as the accepted SO answer for incrementing a local variable: http://stackoverflow.com/questions/6858894/how-to-increment-local-variables-in-elisp http://stackoverflow.com/questions/6858894/how-to-increment-...
- PuercoPop 11y agoFirst I forgot to add I Xah Lee's ergo emacs was a valuable resource when learning Emacs. AFAIR it succintly explained emacs base abstractions. On the setq, I what I meant was iterating through setqs and while (and I seem to recall even catch/throw) as agumonkey mentioned. Then again, 'in my mind', idiomatic elisp uses cl-lib :D. so (cl-incf total-count) would be better. snakeCase is unidiomatic as well.
- agumonkey 11y agoUsing lisp to while/set... sigh. This is old age emacs lisp. We need more dash.el. Unfortunately we'd need a more stream/functional buffer API ? I was thinking of porting scheme regexp SRFI because it felt a better fit than re-search-*.
- PuercoPop 11y agoAs far as dash.el goes, I find lispm's critique of dash.el pretty convincing[0]. However a more profilic emacs coder fried of mine is undeterred and keeps using modern emacs lisp. I think much of the community does as well. [0]: https://www.reddit.com/r/lisp/comments/29ufoc/it_is_not_hard_to_read_lisp_code/cioznk5 https://www.reddit.com/r/lisp/comments/29ufoc/it_is_not_hard... About Scheme's regexp SRFI, have you seen rx.el? Not quite but I like it myself.
- agumonkey 11y agoIIUC rx.el is a sexp dsl/interface to build vanilla regex down the line right ? from what I remember about scheme SRFI is not the 'syntax' but the return values, you get streams of match, then you can leverage, folds and maps. lispm point is nice (and no one can argue about evaluation order and count), but beside the battle-tested aspect, `loop` scares the crap out of me. That's even more a reason to prefer things that avoids it in my mind, mentally small building blocks and patterns versus huge macro-based sublanguages. Yes general and probably super-efficiently implemented, but .. nah.
- lispm 11y agoSee this Xah Lee Elisp code: (defvar xah-left-brackets '("(" "{" "[" "<" "〔" "【" "〖" "〈" "《" "「" "『" "“" "‘" "‹" "«" ) "List of left bracket chars.") (progn ;; make xah-left-brackets based on xah-brackets (setq xah-left-brackets '()) (dotimes (x (- (length xah-brackets) 1)) (when (= (% x 2) 0) (push (char-to-string (elt xah-brackets x)) xah-left-brackets))) (setq xah-left-brackets (reverse xah-left-brackets))) (defvar xah-right-brackets '(")" "]" "}" ">" "〕" "】" "〗" "〉" "》" "」" "』" "”" "’" "›" "»") "list of right bracket chars.") (progn (setq xah-right-brackets '()) (dotimes (x (- (length xah-brackets) 1)) (when (= (% x 2) 1) (push (char-to-string (elt xah-brackets x)) xah-right-brackets))) (setq xah-right-brackets (reverse xah-right-brackets))) In CL this is just: (defun every-nth (seq start n &aux (len (length seq))) (loop for i = start then (+ i n) while (< i len) collect (elt seq i))) (defvar xah-left-brackets (every-nth xah-brackets 0 2)) (defvar xah-right-brackets (every-nth xah-brackets 1 2)) Write useful building blocks. Don't duplicate code. Get rid of low level imperative code. Bonus points for a more efficient version for lists.
- agumonkey 11y agoOh I wasn't arguing that CL loop was a far better solution than while loops (unless you process too long lists). I just became very very+ fond of academic recursive style. Here's a singly-linear variant for fixed steps. (defun ab (l) "I'm too lazy to reverse the result back." (defun _ (l a b f) (if (null l) (if f (list a b) (list b a)) (_ (cdr l) b (cons (car l) a) (not f)))) (_ l nil nil t)) (ab '()) (ab '(1)) (ab '(1 2)) (ab '(1 2 3 4 5 6 7 8 9 10)) ;; ((9 7 5 3 1) (10 8 6 4 2)) (ab '(1 2 3 4 5 6 7 8 9 10 11)) ;; ((11 9 7 5 3 1) (10 8 6 4 2)) I don't know how cute or ugly a solution for variable step could be though. `ab` is a nice partitioning building block, less general than every-nth surely. ps: Straight from the land of wishful lisping (defun abn (l n) (defun ono (n) "builds a cons oo cycle of size n" 'unicorn) (defun noo (c) "rotates a cons cycle by one" 'unicorn) (defun cooons (c v) "cons v to the head of c" 'unicorn) (defun _ (l c) (if (null l) c (_ (cdr l) (noo (cooons c (car l)))))) (_ l (ono n)))
- xahlee 11y agohi PuercoPop, that code is meant to be run as a script (emacs --script filename), so i used setq is possibly more proper. But, this is just post-fact defense, as lots my pages began as quick blogs. As you know, CL has controversial status in gnu emacs dev community. e.g. http://ergoemacs.org/emacs/elisp_common_lisp_in_emacs.html http://ergoemacs.org/emacs/elisp_common_lisp_in_emacs.html I myself avoid it, because i don't like CL. I'm going to tuck in the hanging parens in a few days. Thank you for the criticism. (thanks to many others here too)
- kazinator 11y agoBut stuff like (let (var1 (var2 0)) (when condition (setq var1 (var1-initializer-form)) ...))) is just silly. Only the inner scope of the when uses var1, or any of the vars; it could just be: (when condition (let ((var1 (var1-initializer-form)) (var2 0)) ...)) Never do two-step initialization of a variable without an excellent reason; it's just sloppy otherwise.