emacs-devel
[Top][All Lists]
Advanced

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

Re: Instead of pcase


From: Emanuel Berg
Subject: Re: Instead of pcase
Date: Fri, 17 Nov 2023 00:41:43 +0100
User-agent: Gnus/5.13 (Gnus v5.13)

T.V Raman wrote:

> RMS' message makes some very valuable points re specialized
> languages like pcase -- pcase has its strengths, but in
> general, it's definitely not easy to understand complex code
> that leverages it -- as an example:
>
> I recently tried to understand some of the completion code;
> -- specifically, completion-at-point, and immediately hit
> the pcase wall and gave up --- my lack of understanding of
> pcase made that code in Emacs Core read like line-noise.

I grepped my own Elisp for pcase and found this.
Interestingly, it uses cl-lib as well. So here we have cl-lib
and pcase in combination, so should be downright
incomprehensible, right?

You will see that the use of pcase is very clear. I'm sure you
can understand it. From there, more complex use should be very
possible for you to understand as well, I'm positive.

But really: code that is difficult to understand can be
written using non-cl-lib Elisp and non-pcase Elisp as well.
Good use of cl-lib and good use of pcase reduces complexity.
Complex use of cl-lib and pcase will be complex, sure, but
there are no guarantees solving that complexity in any other
way will be any less complex.

If everyone did that instead of using libraries we would end
up with much more code, and code that essentially - but not
quite, and that would be a big problem - did the same thing
over and over.

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

(require 'cl-lib)
(require 'pcase)

(defun days (y1 m1 d1 y2 m2 d2)
  (let*((then (float-time (encode-time 0 0 0 d1 m1 y1)))
        (now  (float-time (encode-time 0 0 0 d2 m2 y2)))
        (diff (- now then)) )
    (string-to-number (format-seconds "%d" diff)) ))

;; (days 1958 04 13 1958 08 30) ; 139 days between Tahiti Nui 2 & 3

(defun days-date (date1 date2)
  (pcase-let*(
      (sep "-")
      (`(,y1 ,m1 ,d1) (cl-map 'list #'string-to-number (split-string date1 
sep)))
      (`(,y2 ,m2 ,d2) (cl-map 'list #'string-to-number (split-string date2 
sep))) )
    (days y1 m1 d1 y2 m2 d2) ))

;; (days-date "2021-03-19" "2021-04-20") ;     31
;; (days-date "1964-07-26" "2021-03-22") ; 20 693

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




reply via email to

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