Closed12

emacsのseq.elを調べてみる

podhmopodhmo

loopマクロとか忘れてしまったしcl-libのものを利用するのもなにか違う気がする。覚えているのはdolistやmapcar程度だったけれど他にも便利なものがありそうとtab-barの実装あたりを覗いていたらseq.elというものを発見した。これで定義されている関数などを調べてみる。

podhmopodhmo
podhmopodhmo

どのようなものがあるか見てみる

locate-libraryでどこで定義されているかわかるしmapatomsで雑にobarrayの中を覗ける。
あとはseq.elで定義されているものと同様のものを探してみれば良い。

(insert  (locate-library "seq"))
;; -> /usr/share/emacs/27.1/lisp/emacs-lisp/seq.elc

(insert (format "%s" (symbol-file 'seq-each)))
;; -> /usr/share/emacs/27.1/lisp/emacs-lisp/seq.elc

(let ((ans nil))
  (mapatoms (lambda (x)
              (when (string-equal (symbol-file x) "/usr/share/emacs/27.1/lisp/emacs-lisp/seq.elc")
                (push x ans))))
  (insert  (format "%s" ans)))
;; -> (seq-find seq-set-equal-p seq seq-partition seq-filter seq--activate-font-lock-keywords seq-uniq seq-concatenate seq-remove seq--count-successive seq-doseq seq-rest seq-random-elt seq-length seq-first seq--elt-safe seq-position seq-copy seq-do-indexed seq-sort-by seq--into-list seq-contains-p seq--pcase-macroexpander (setf seq-elt) seq-map-indexed seq-take-while seq-each seq--into-string seq-drop seq-some seq-sort seq-take seq-contains seq-mapcat seq-every-p seq-elt seq--into-vector seq-mapn seq-drop-while seq-empty-p seq-reduce seq-intersection seq-reverse seq-let seq--make-pcase-bindings seq-map seq-max seq-min seq-count seq-difference seq-into seq-subseq seq--make-pcase-patterns seq-group-by seq-into-sequence seq-do seqp)
podhmopodhmo

一覧のほうが見やすいか。

(let*  ((fns (cl-remove-if-not #'symbol-function seq-symbols))
        (xs (cl-sort (mapcar #'symbol-name  fns)   #'string-lessp)))
  (dolist (x xs)
    (unless (string-match-p "--" x)
      (let ((doc (documentation (intern x))))
        (insert (format "- %s\n" x))))))
  • seq-concatenate
  • seq-contains
  • seq-contains-p
  • seq-copy
  • seq-count
  • seq-difference
  • seq-do
  • seq-do-indexed
  • seq-doseq
  • seq-drop
  • seq-drop-while
  • seq-each
  • seq-elt
  • seq-empty-p
  • seq-every-p
  • seq-filter
  • seq-find
  • seq-first
  • seq-group-by
  • seq-intersection
  • seq-into
  • seq-into-sequence
  • seq-length
  • seq-let
  • seq-map
  • seq-map-indexed
  • seq-mapcat
  • seq-mapn
  • seq-max
  • seq-min
  • seq-partition
  • seq-position
  • seq-random-elt
  • seq-reduce
  • seq-remove
  • seq-rest
  • seq-reverse
  • seq-set-equal-p
  • seq-some
  • seq-sort
  • seq-sort-by
  • seq-subseq
  • seq-take
  • seq-take-while
  • seq-uniq
  • seqp
podhmopodhmo

seq-concatenate

Concatenate SEQUENCES into a single sequence of type TYPE.
TYPE must be one of following symbols: vector, string or list.

(fn TYPE SEQUENCE...)

(seq-concatenate 'list '(1 2 3) '(10 20 30)) ;; -> (1 2 3 10 20 30)

(seq-concatenate 'vector '(1 2 3) '(10 20 30)) ;; -> [1 2 3 10 20 30]

seq-contains

(fn SEQUENCE ELT &optional TESTFN)

(seq-contains '(1 2 3) '10) ;; -> nil
(seq-contains '(1 2 3) '1) ;; -> 1
(seq-contains '(1 2 3) "1" (lambda (x y) (string-equal (format "%s" x) (format "%s" x)))) ;; -> 1

seq-contains-p

Return non-nil if SEQUENCE contains an element equal to ELT.
Equality is defined by TESTFN if non-nil or by ‘equal’ if nil.

(fn SEQUENCE ELT &optional TESTFN)

(seq-contains-p '(1 2 3) '10) ;; -> nil
(seq-contains-p '(1 2 3) '1) ;; -> t
(seq-contains-p '(1 2 3) "1" (lambda (x y) (string-equal (format "%s" x) (format "%s" x)))) ;; -> t
podhmopodhmo

seq-copy

Return a shallow copy of SEQUENCE.

(fn SEQUENCE)

(seq-copy nil) ;; -> nil
(seq-copy '(1 2 [1 2 3])) ;; -> (1 2 [1 2 3])

seq-count

Return the number of elements for which (PRED element) is non-nil in SEQUENCE.

(fn PRED SEQUENCE)

(seq-count  #'numberp '(10 20 30)) ;; -> 3
(seq-count  #'numberp '(10 20 30 "foo")) ;; -> 3

seq-difference

Return a list of the elements that appear in SEQUENCE1 but not in SEQUENCE2.
Equality is defined by TESTFN if non-nil or by ‘equal’ if nil.

(fn SEQUENCE1 SEQUENCE2 &optional TESTFN)

(seq-difference '(1 2 3 4 5) '(1 3 5 7 9)) ;; -> (2 4)
(seq-difference  '(1 3 5 7 9) [1 2 3 4 5]) ;; -> (7 9)
podhmopodhmo

seq-do

Apply FUNCTION to each element of SEQUENCE, presumably for side effects.
Return SEQUENCE.

(fn FUNCTION SEQUENCE)

(seq-do (lambda (x) (insert (format "%s" (* x x)))) '(1 2 3)) ;; -> (1 2 3)
149

seq-do-indexed

Apply FUNCTION to each element of SEQUENCE and return nil.
Unlike ‘seq-map’, FUNCTION takes two arguments: the element of
the sequence, and its index within the sequence.

(fn FUNCTION SEQUENCE)

(seq-do-indexed (lambda (x i) (insert (format "%s %s\n" i x))) '(10 20 30))
0 10
1 20
2 30

seq-doseq

Loop over a sequence.
Evaluate BODY with VAR bound to each element of SEQUENCE, in turn.

Similar to ‘dolist’ but can be applied to lists, strings, and vectors.

(fn (VAR SEQUENCE) BODY...)

(seq-doseq (x [1 2 3])
  (insert (format "-%s \n" x)))
-1 
-2 
-3 

(seq-doseq (x '(1 2 3))
  (insert (format "-%s \n" x)))
-1 
-2 
-3 
podhmopodhmo

seq-drop

Remove the first N elements of SEQUENCE and return the result.
The result is a sequence of the same type as SEQUENCE.

If N is a negative integer or zero, SEQUENCE is returned.

(fn SEQUENCE N)

(seq-drop '(1 2 3 4 5) 2) ;; -> (3 4 5)
(seq-drop '(1 2 3 4 5) 10) ;; -> nil

seq-drop-while

Remove the successive elements of SEQUENCE for which PRED returns non-nil.
PRED is a function of one argument. The result is a sequence of
the same type as SEQUENCE.

(fn PRED SEQUENCE)

(seq-drop-while (lambda (x) (< x 5)) '(1 2 3 4 5 6 7 8 9 10 1)) ;; -> (5 6 7 8 9 10 1)

seq-each

(fn FUNCTION SEQUENCE)

(seq-each (lambda (x) (insert (format "%s\n" x))) '(10 20 30))
10
20
30

seq-elt

Return Nth element of SEQUENCE.

(fn SEQUENCE N)

(seq-elt  '(10 20 30) 0) ;; -> 10
(seq-elt  '(10 20 30) 1) ;; -> 20
(seq-elt  '(10 20 30) 10) ;; -> nil

seq-empty-p

Return non-nil if the SEQUENCE is empty, nil otherwise.

(fn SEQUENCE)

(seq-empty-p nil) ;; -> t
(seq-empty-p []) ;; -> t
(seq-empty-p [10]) ;; -> nil

seq-every-p

Return non-nil if (PRED element) is non-nil for all elements of SEQUENCE.

(fn PRED SEQUENCE)

(seq-every-p #'numberp '(10 20 30)) ;; -> t

seq-filter

Return a list of all the elements for which (PRED element) is non-nil in SEQUENCE.

(fn PRED SEQUENCE)

(seq-filter (lambda (x) (= (mod x 2) 1)) '(0 1 2 3 4 5)) ;; -> (1 3 5)

seq-find

Return the first element for which (PRED element) is non-nil in SEQUENCE.
If no element is found, return DEFAULT.

Note that ‘seq-find’ has an ambiguity if the found element is
identical to DEFAULT, as it cannot be known if an element was
found or not.

(fn PRED SEQUENCE &optional DEFAULT)

(seq-find #'stringp '(10 20 "foo" "bar" 10)) ;; -> "foo"
(seq-find #'stringp '(10 20 10)) ;; -> nil
(seq-find #'stringp '(10 20 10) -1) ;; -> -1

seq-first

Return the first element of SEQUENCE.

(fn SEQUENCE)

(seq-first  nil) ;; -> nil
(seq-first [10 20 30]) ;; -> 10
podhmopodhmo

seq-group-by

Apply FUNCTION to each element of SEQUENCE.
Separate the elements of SEQUENCE into an alist using the results as
keys. Keys are compared using ‘equal’.

(fn FUNCTION SEQUENCE)

(seq-group-by (lambda (x) (length (format "%s" x)))
              [1 2 3 4 5 10 20 30 40 50 111 123 1111 1 2 3])
;;  -> ((2 10 20 30 40 50) (3 111 123) (4 1111) (1 1 2 3 4 5 1 2 3))

seq-intersection

Return a list of the elements that appear in both SEQUENCE1 and SEQUENCE2.
Equality is defined by TESTFN if non-nil or by ‘equal’ if nil.

(fn SEQUENCE1 SEQUENCE2 &optional TESTFN)

(seq-intersection '( 1 2 3 4 5) '(1 3 5 7 9)) ;; -> (1 3 5)

seq-into

Concatenate the elements of SEQUENCE into a sequence of type TYPE.
TYPE can be one of the following symbols: vector, string or
list.

(fn SEQUENCE TYPE)

(seq-into '(100 200) 'vector); ; -> [100 200]
(seq-into '(100 200) 'list) ;; -> (100 200)
(seq-into '(100 200) 'string) ;; -> "dE"

seq-into-sequence

Convert SEQUENCE into a sequence.

The default implementation is to signal an error if SEQUENCE is not a
sequence, specific functions should be implemented for new types
of sequence.

(fn SEQUENCE)

(seq-into-sequence [10 20]) ;; -> [10 20]

このふたつの関数の使い道があんまりよくわからない。。

seq-length

Return the number of elements of SEQUENCE.

(fn SEQUENCE)

(seq-length [1 2 3]) ;; -> 3
podhmopodhmo

seq-let

Bind the variables in ARGS to the elements of SEQUENCE, then evaluate BODY.

ARGS can also include the ‘&rest’ marker followed by a variable
name to be bound to the rest of SEQUENCE.

(fn ARGS SEQUENCE &rest BODY)

seq-let (x y) [10 20] 
  (insert (format "%s\n" (list x y))))
(10 20)

(seq-let (x y) [10 20 30 40 50]
  (insert (format "%s\n" (list x y))))
(10 20)

(seq-let (x y &rest rest) [10 20 30 40 50]
  (insert (format "%s\n" (list x y rest))))
(10 20 [30 40 50])
podhmopodhmo

[!WARNING]
飽きた!!!

seq-map

Return the result of applying FUNCTION to each element of SEQUENCE.

(fn FUNCTION SEQUENCE)

(seq-map )

seq-map-indexed

Return the result of applying FUNCTION to each element of SEQUENCE.
Unlike ‘seq-map’, FUNCTION takes two arguments: the element of
the sequence, and its index within the sequence.

(fn FUNCTION SEQUENCE)

(seq-map-indexed )

seq-mapcat

Concatenate the result of applying FUNCTION to each element of SEQUENCE.
The result is a sequence of type TYPE, or a list if TYPE is nil.

(fn FUNCTION SEQUENCE &optional TYPE)

(seq-mapcat )

seq-mapn

Like ‘seq-map’ but FUNCTION is mapped over all SEQUENCES.
The arity of FUNCTION must match the number of SEQUENCES, and the
mapping stops on the shortest sequence.
Return a list of the results.

(fn FUNCTION SEQUENCES...)

(seq-mapn )

seq-max

Return the largest element of SEQUENCE.
SEQUENCE must be a sequence of numbers or markers.

(fn SEQUENCE)

(seq-max )

seq-min

Return the smallest element of SEQUENCE.
SEQUENCE must be a sequence of numbers or markers.

(fn SEQUENCE)

(seq-min )

seq-partition

Return a list of the elements of SEQUENCE grouped into sub-sequences of length N.
The last sequence may contain less than N elements. If N is a
negative integer or 0, nil is returned.

(fn SEQUENCE N)

(seq-partition )

seq-position

Return the index of the first element in SEQUENCE that is equal to ELT.
Equality is defined by TESTFN if non-nil or by ‘equal’ if nil.

(fn SEQUENCE ELT &optional TESTFN)

(seq-position )

seq-random-elt

Return a random element from SEQUENCE.
Signal an error if SEQUENCE is empty.

(fn SEQUENCE)

(seq-random-elt )

seq-reduce

Reduce the function FUNCTION across SEQUENCE, starting with INITIAL-VALUE.

Return the result of calling FUNCTION with INITIAL-VALUE and the
first element of SEQUENCE, then calling FUNCTION with that result and
the second element of SEQUENCE, then with that result and the third
element of SEQUENCE, etc.

If SEQUENCE is empty, return INITIAL-VALUE and FUNCTION is not called.

(fn FUNCTION SEQUENCE INITIAL-VALUE)

(seq-reduce )

seq-remove

Return a list of all the elements for which (PRED element) is nil in SEQUENCE.

(fn PRED SEQUENCE)

(seq-remove )

seq-rest

Return a sequence of the elements of SEQUENCE except the first one.

(fn SEQUENCE)

(seq-rest )

seq-reverse

Return a sequence with elements of SEQUENCE in reverse order.

(fn SEQUENCE)

(seq-reverse )

seq-set-equal-p

Return non-nil if SEQUENCE1 and SEQUENCE2 contain the same elements, regardless of order.
Equality is defined by TESTFN if non-nil or by ‘equal’ if nil.

(fn SEQUENCE1 SEQUENCE2 &optional TESTFN)

(seq-set-equal-p )

seq-some

Return non-nil if PRED is satisfied for at least one element of SEQUENCE.
If so, return the first non-nil value returned by PRED.

(fn PRED SEQUENCE)

(seq-some )

seq-sort

Sort SEQUENCE using PRED as comparison function.
The result is a sequence of the same type as SEQUENCE.

(fn PRED SEQUENCE)

(seq-sort )

seq-sort-by

Sort SEQUENCE using PRED as a comparison function.
Elements of SEQUENCE are transformed by FUNCTION before being
sorted. FUNCTION must be a function of one argument.

(fn FUNCTION PRED SEQUENCE)

(seq-sort-by )

seq-subseq

Return the sequence of elements of SEQUENCE from START to END.
END is exclusive.

If END is omitted, it defaults to the length of the sequence. If
START or END is negative, it counts from the end. Signal an
error if START or END are outside of the sequence (i.e too large
if positive or too small if negative).

(fn SEQUENCE START &optional END)

(seq-subseq )

seq-take

Take the first N elements of SEQUENCE and return the result.
The result is a sequence of the same type as SEQUENCE.

If N is a negative integer or zero, an empty sequence is
returned.

(fn SEQUENCE N)

(seq-take )

seq-take-while

Take the successive elements of SEQUENCE for which PRED returns non-nil.
PRED is a function of one argument. The result is a sequence of
the same type as SEQUENCE.

(fn PRED SEQUENCE)

(seq-take-while )

seq-uniq

Return a list of the elements of SEQUENCE with duplicates removed.
TESTFN is used to compare elements, or ‘equal’ if TESTFN is nil.

(fn SEQUENCE &optional TESTFN)

(seq-uniq )

seqp

Return non-nil if OBJECT is a sequence, nil otherwise.

(fn OBJECT)

(seqp )
podhmopodhmo

break?

loopとbreakみたいなものはないのかな?現状はcl-dolistとcl-returnを使っている。

(progn
  (cl-dolist (x '(1 2 3 4 5))
    (insert (format "%s\n" x))
    (when (> x 3)
      (cl-return)))
    (insert "end\n"))
1
2
3
4
end

(cl-block b
  (cl-dolist (x '(1 2 3 4 5))
    (insert (format "%s\n" x))
    (when (> x 3)
      (cl-return b)))
    (insert "never \n"))
1
2
3
4
このスクラップは8日前にクローズされました